Commit | Line | Data |
---|---|---|
f83d2997 KH |
1 | ;;; cperl-mode.el --- Perl code editing commands for Emacs |
2 | ||
3 | ;; Copyright (C) 1985, 86, 87, 91, 92, 93, 94, 95, 96, 1997 | |
4 | ;; Free Software Foundation, Inc. | |
5 | ||
6 | ;; Author: Ilya Zakharevich and Bob Olson | |
7 | ;; Maintainer: Ilya Zakharevich <ilya@math.ohio-state.edu> | |
8 | ;; Keywords: languages, Perl | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Corrections made by Ilya Zakharevich ilya@math.mps.ohio-state.edu | |
f83d2997 KH |
28 | |
29 | ;;; Commentary: | |
30 | ||
31 | ;;; You can either fine-tune the bells and whistles of this mode or | |
32 | ;;; bulk enable them by putting | |
33 | ||
34 | ;; (setq cperl-hairy t) | |
35 | ||
36 | ;;; in your .emacs file. (Emacs rulers do not consider it politically | |
37 | ;;; correct to make whistles enabled by default.) | |
38 | ||
39 | ;;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<< | |
40 | ;;; or as help on variables `cperl-tips', `cperl-problems', <<<<<< | |
f94a632a | 41 | ;;; `cperl-praise', `cperl-speed'. <<<<<< |
f83d2997 KH |
42 | |
43 | ;;; The mode information (on C-h m) provides some customization help. | |
44 | ;;; If you use font-lock feature of this mode, it is advisable to use | |
45 | ;;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock. | |
46 | ||
47 | ;;; Faces used now: three faces for first-class and second-class keywords | |
48 | ;;; and control flow words, one for each: comments, string, labels, | |
49 | ;;; functions definitions and packages, arrays, hashes, and variable | |
50 | ;;; definitions. If you do not see all these faces, your font-lock does | |
51 | ;;; not define them, so you need to define them manually. | |
52 | ||
53 | ;;; into your .emacs file. | |
54 | ||
55 | ;;;; This mode supports font-lock, imenu and mode-compile. In the | |
56 | ;;;; hairy version font-lock is on, but you should activate imenu | |
57 | ;;;; yourself (note that mode-compile is not standard yet). Well, you | |
58 | ;;;; can use imenu from keyboard anyway (M-x imenu), but it is better | |
59 | ;;;; to bind it like that: | |
60 | ||
61 | ;; (define-key global-map [M-S-down-mouse-3] 'imenu) | |
62 | ||
63 | ;;; Code: | |
64 | ||
5bd52f0e | 65 | ;; Some macros are needed for `defcustom' |
80585273 DL |
66 | (eval-when-compile |
67 | (require 'font-lock) | |
68 | (defvar msb-menu-cond) | |
69 | (defvar gud-perldb-history) | |
70 | (defvar font-lock-background-mode) ; not in Emacs | |
71 | (defvar font-lock-display-type) ; ditto | |
72 | (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version)) | |
73 | (defmacro cperl-is-face (arg) ; Takes quoted arg | |
74 | (cond ((fboundp 'find-face) | |
75 | `(find-face ,arg)) | |
76 | (;;(and (fboundp 'face-list) | |
77 | ;; (face-list)) | |
78 | (fboundp 'face-list) | |
79 | `(member ,arg (and (fboundp 'face-list) | |
80 | (face-list)))) | |
81 | (t | |
82 | `(boundp ,arg)))) | |
83 | (defmacro cperl-make-face (arg descr) ; Takes unquoted arg | |
84 | (cond ((fboundp 'make-face) | |
85 | `(make-face (quote ,arg))) | |
86 | (t | |
ce22dd53 | 87 | `(defvar ,arg (quote ,arg) ,descr)))) |
80585273 DL |
88 | (defmacro cperl-force-face (arg descr) ; Takes unquoted arg |
89 | `(progn | |
90 | (or (cperl-is-face (quote ,arg)) | |
91 | (cperl-make-face ,arg ,descr)) | |
92 | (or (boundp (quote ,arg)) ; We use unquoted variants too | |
ce22dd53 | 93 | (defvar ,arg (quote ,arg) ,descr)))) |
80585273 DL |
94 | (if cperl-xemacs-p |
95 | (defmacro cperl-etags-snarf-tag (file line) | |
b787fc05 | 96 | `(progn |
80585273 DL |
97 | (beginning-of-line 2) |
98 | (list ,file ,line))) | |
99 | (defmacro cperl-etags-snarf-tag (file line) | |
100 | `(etags-snarf-tag))) | |
101 | (if cperl-xemacs-p | |
102 | (defmacro cperl-etags-goto-tag-location (elt) | |
103 | ;;(progn | |
104 | ;; (switch-to-buffer (get-file-buffer (elt (, elt) 0))) | |
105 | ;; (set-buffer (get-file-buffer (elt (, elt) 0))) | |
106 | ;; Probably will not work due to some save-excursion??? | |
107 | ;; Or save-file-position? | |
108 | ;; (message "Did I get to line %s?" (elt (, elt) 1)) | |
109 | `(goto-line (string-to-int (elt ,elt 1)))) | |
110 | ;;) | |
111 | (defmacro cperl-etags-goto-tag-location (elt) | |
112 | `(etags-goto-tag-location ,elt))) | |
113 | (autoload 'tmm-prompt "tmm")) | |
5bd52f0e RS |
114 | |
115 | (defun cperl-choose-color (&rest list) | |
116 | (let (answer) | |
117 | (while list | |
118 | (or answer | |
119 | (if (or (x-color-defined-p (car list)) | |
120 | (null (cdr list))) | |
121 | (setq answer (car list)))) | |
122 | (setq list (cdr list))) | |
123 | answer)) | |
124 | ||
ccc3ce39 SE |
125 | (defgroup cperl nil |
126 | "Major mode for editing Perl code." | |
127 | :prefix "cperl-" | |
db133cb6 RS |
128 | :group 'languages |
129 | :version "20.3") | |
130 | ||
131 | (defgroup cperl-indentation-details nil | |
132 | "Indentation." | |
133 | :prefix "cperl-" | |
134 | :group 'cperl) | |
135 | ||
136 | (defgroup cperl-affected-by-hairy nil | |
137 | "Variables affected by `cperl-hairy'." | |
138 | :prefix "cperl-" | |
139 | :group 'cperl) | |
140 | ||
141 | (defgroup cperl-autoinsert-details nil | |
142 | "Auto-insert tuneup." | |
143 | :prefix "cperl-" | |
144 | :group 'cperl) | |
145 | ||
146 | (defgroup cperl-faces nil | |
147 | "Fontification colors." | |
148 | :prefix "cperl-" | |
149 | :group 'cperl) | |
150 | ||
151 | (defgroup cperl-speed nil | |
152 | "Speed vs. validity tuneup." | |
153 | :prefix "cperl-" | |
154 | :group 'cperl) | |
155 | ||
156 | (defgroup cperl-help-system nil | |
157 | "Help system tuneup." | |
158 | :prefix "cperl-" | |
159 | :group 'cperl) | |
ccc3ce39 | 160 | |
f83d2997 | 161 | \f |
ccc3ce39 | 162 | (defcustom cperl-extra-newline-before-brace nil |
f83d2997 KH |
163 | "*Non-nil means that if, elsif, while, until, else, for, foreach |
164 | and do constructs look like: | |
165 | ||
166 | if () | |
167 | { | |
168 | } | |
169 | ||
170 | instead of: | |
171 | ||
172 | if () { | |
173 | } | |
ccc3ce39 SE |
174 | " |
175 | :type 'boolean | |
db133cb6 RS |
176 | :group 'cperl-autoinsert-details) |
177 | ||
5c8b7eaf | 178 | (defcustom cperl-extra-newline-before-brace-multiline |
db133cb6 RS |
179 | cperl-extra-newline-before-brace |
180 | "*Non-nil means the same as `cperl-extra-newline-before-brace', but | |
181 | for constructs with multiline if/unless/while/until/for/foreach condition." | |
182 | :type 'boolean | |
183 | :group 'cperl-autoinsert-details) | |
ccc3ce39 SE |
184 | |
185 | (defcustom cperl-indent-level 2 | |
186 | "*Indentation of CPerl statements with respect to containing block." | |
187 | :type 'integer | |
db133cb6 | 188 | :group 'cperl-indentation-details) |
f83d2997 | 189 | |
ccc3ce39 | 190 | (defcustom cperl-lineup-step nil |
f83d2997 | 191 | "*`cperl-lineup' will always lineup at multiple of this number. |
029cb4d5 | 192 | If nil, the value of `cperl-indent-level' will be used." |
ccc3ce39 | 193 | :type '(choice (const nil) integer) |
db133cb6 RS |
194 | :group 'cperl-indentation-details) |
195 | ||
ccc3ce39 | 196 | (defcustom cperl-brace-imaginary-offset 0 |
f83d2997 KH |
197 | "*Imagined indentation of a Perl open brace that actually follows a statement. |
198 | An open brace following other text is treated as if it were this far | |
ccc3ce39 SE |
199 | to the right of the start of its line." |
200 | :type 'integer | |
db133cb6 | 201 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
202 | |
203 | (defcustom cperl-brace-offset 0 | |
204 | "*Extra indentation for braces, compared with other text in same context." | |
205 | :type 'integer | |
db133cb6 | 206 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
207 | (defcustom cperl-label-offset -2 |
208 | "*Offset of CPerl label lines relative to usual indentation." | |
209 | :type 'integer | |
db133cb6 | 210 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
211 | (defcustom cperl-min-label-indent 1 |
212 | "*Minimal offset of CPerl label lines." | |
213 | :type 'integer | |
db133cb6 | 214 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
215 | (defcustom cperl-continued-statement-offset 2 |
216 | "*Extra indent for lines not starting new statements." | |
217 | :type 'integer | |
db133cb6 | 218 | :group 'cperl-indentation-details) |
ccc3ce39 | 219 | (defcustom cperl-continued-brace-offset 0 |
f83d2997 | 220 | "*Extra indent for substatements that start with open-braces. |
ccc3ce39 SE |
221 | This is in addition to cperl-continued-statement-offset." |
222 | :type 'integer | |
db133cb6 | 223 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
224 | (defcustom cperl-close-paren-offset -1 |
225 | "*Extra indent for substatements that start with close-parenthesis." | |
226 | :type 'integer | |
db133cb6 | 227 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
228 | |
229 | (defcustom cperl-auto-newline nil | |
f83d2997 KH |
230 | "*Non-nil means automatically newline before and after braces, |
231 | and after colons and semicolons, inserted in CPerl code. The following | |
232 | \\[cperl-electric-backspace] will remove the inserted whitespace. | |
5c8b7eaf | 233 | Insertion after colons requires both this variable and |
ccc3ce39 SE |
234 | `cperl-auto-newline-after-colon' set." |
235 | :type 'boolean | |
db133cb6 | 236 | :group 'cperl-autoinsert-details) |
f83d2997 | 237 | |
6c389151 SM |
238 | (defcustom cperl-autoindent-on-semi nil |
239 | "*Non-nil means automatically indent after insertion of (semi)colon. | |
240 | Active if `cperl-auto-newline' is false." | |
241 | :type 'boolean | |
242 | :group 'cperl-autoinsert-details) | |
243 | ||
ccc3ce39 | 244 | (defcustom cperl-auto-newline-after-colon nil |
f83d2997 | 245 | "*Non-nil means automatically newline even after colons. |
ccc3ce39 SE |
246 | Subject to `cperl-auto-newline' setting." |
247 | :type 'boolean | |
db133cb6 | 248 | :group 'cperl-autoinsert-details) |
f83d2997 | 249 | |
ccc3ce39 | 250 | (defcustom cperl-tab-always-indent t |
f83d2997 | 251 | "*Non-nil means TAB in CPerl mode should always reindent the current line, |
ccc3ce39 SE |
252 | regardless of where in the line point is when the TAB command is used." |
253 | :type 'boolean | |
db133cb6 | 254 | :group 'cperl-indentation-details) |
f83d2997 | 255 | |
ccc3ce39 | 256 | (defcustom cperl-font-lock nil |
029cb4d5 | 257 | "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'. |
ccc3ce39 | 258 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
259 | :type '(choice (const null) boolean) |
260 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 261 | |
ccc3ce39 | 262 | (defcustom cperl-electric-lbrace-space nil |
029cb4d5 | 263 | "*Non-nil (and non-null) means { after $ should be preceded by ` '. |
ccc3ce39 | 264 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
265 | :type '(choice (const null) boolean) |
266 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 267 | |
ccc3ce39 | 268 | (defcustom cperl-electric-parens-string "({[]})<" |
f83d2997 | 269 | "*String of parentheses that should be electric in CPerl. |
ccc3ce39 SE |
270 | Closing ones are electric only if the region is highlighted." |
271 | :type 'string | |
db133cb6 | 272 | :group 'cperl-affected-by-hairy) |
f83d2997 | 273 | |
ccc3ce39 | 274 | (defcustom cperl-electric-parens nil |
f83d2997 | 275 | "*Non-nil (and non-null) means parentheses should be electric in CPerl. |
ccc3ce39 | 276 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
277 | :type '(choice (const null) boolean) |
278 | :group 'cperl-affected-by-hairy) | |
279 | ||
280 | (defvar zmacs-regions) ; Avoid warning | |
281 | ||
5c8b7eaf | 282 | (defcustom cperl-electric-parens-mark |
f83d2997 KH |
283 | (and window-system |
284 | (or (and (boundp 'transient-mark-mode) ; For Emacs | |
285 | transient-mark-mode) | |
286 | (and (boundp 'zmacs-regions) ; For XEmacs | |
287 | zmacs-regions))) | |
288 | "*Not-nil means that electric parens look for active mark. | |
ccc3ce39 SE |
289 | Default is yes if there is visual feedback on mark." |
290 | :type 'boolean | |
db133cb6 | 291 | :group 'cperl-autoinsert-details) |
f83d2997 | 292 | |
ccc3ce39 | 293 | (defcustom cperl-electric-linefeed nil |
f83d2997 KH |
294 | "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy. |
295 | In any case these two mean plain and hairy linefeeds together. | |
ccc3ce39 | 296 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
297 | :type '(choice (const null) boolean) |
298 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 299 | |
ccc3ce39 | 300 | (defcustom cperl-electric-keywords nil |
f83d2997 | 301 | "*Not-nil (and non-null) means keywords are electric in CPerl. |
ccc3ce39 | 302 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
303 | :type '(choice (const null) boolean) |
304 | :group 'cperl-affected-by-hairy) | |
ccc3ce39 SE |
305 | |
306 | (defcustom cperl-hairy nil | |
db133cb6 | 307 | "*Not-nil means most of the bells and whistles are enabled in CPerl. |
5c8b7eaf | 308 | Affects: `cperl-font-lock', `cperl-electric-lbrace-space', |
db133cb6 RS |
309 | `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords', |
310 | `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings', | |
311 | `cperl-lazy-help-time'." | |
ccc3ce39 | 312 | :type 'boolean |
db133cb6 | 313 | :group 'cperl-affected-by-hairy) |
ccc3ce39 SE |
314 | |
315 | (defcustom cperl-comment-column 32 | |
316 | "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)." | |
317 | :type 'integer | |
db133cb6 | 318 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
319 | |
320 | (defcustom cperl-vc-header-alist '((SCCS "$sccs = '%W\%' ;") | |
321 | (RCS "$rcs = ' $Id\$ ' ;")) | |
322 | "*What to use as `vc-header-alist' in CPerl." | |
323 | :type '(repeat (list symbol string)) | |
324 | :group 'cperl) | |
325 | ||
5c8b7eaf | 326 | (defcustom cperl-clobber-mode-lists |
5bd52f0e RS |
327 | (not |
328 | (and | |
329 | (boundp 'interpreter-mode-alist) | |
330 | (assoc "miniperl" interpreter-mode-alist) | |
331 | (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist))) | |
332 | "*Whether to install us into `interpreter-' and `extension' mode lists." | |
333 | :type 'boolean | |
334 | :group 'cperl) | |
335 | ||
ccc3ce39 | 336 | (defcustom cperl-info-on-command-no-prompt nil |
f83d2997 KH |
337 | "*Not-nil (and non-null) means not to prompt on C-h f. |
338 | The opposite behaviour is always available if prefixed with C-c. | |
ccc3ce39 | 339 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
340 | :type '(choice (const null) boolean) |
341 | :group 'cperl-affected-by-hairy) | |
342 | ||
343 | (defcustom cperl-clobber-lisp-bindings nil | |
344 | "*Not-nil (and non-null) means not overwrite C-h f. | |
345 | The function is available on \\[cperl-info-on-command], \\[cperl-get-help]. | |
346 | Can be overwritten by `cperl-hairy' if nil." | |
347 | :type '(choice (const null) boolean) | |
348 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 349 | |
ccc3ce39 | 350 | (defcustom cperl-lazy-help-time nil |
db133cb6 RS |
351 | "*Not-nil (and non-null) means to show lazy help after given idle time. |
352 | Can be overwritten by `cperl-hairy' to be 5 sec if nil." | |
300f7bb3 | 353 | :type '(choice (const null) (const nil) integer) |
db133cb6 | 354 | :group 'cperl-affected-by-hairy) |
f83d2997 | 355 | |
ccc3ce39 | 356 | (defcustom cperl-pod-face 'font-lock-comment-face |
80585273 | 357 | "*Face for pod highlighting." |
ccc3ce39 | 358 | :type 'face |
db133cb6 | 359 | :group 'cperl-faces) |
f83d2997 | 360 | |
ccc3ce39 | 361 | (defcustom cperl-pod-head-face 'font-lock-variable-name-face |
80585273 | 362 | "*Face for pod highlighting. |
ccc3ce39 SE |
363 | Font for POD headers." |
364 | :type 'face | |
db133cb6 | 365 | :group 'cperl-faces) |
f83d2997 | 366 | |
ccc3ce39 | 367 | (defcustom cperl-here-face 'font-lock-string-face |
80585273 | 368 | "*Face for here-docs highlighting." |
ccc3ce39 | 369 | :type 'face |
db133cb6 | 370 | :group 'cperl-faces) |
f83d2997 | 371 | |
ac6857fb | 372 | (defcustom cperl-invalid-face 'underline |
80585273 DL |
373 | "*Face for highlighting trailing whitespace." |
374 | :type 'face | |
ac6857fb | 375 | :version "21.1" |
5bd52f0e RS |
376 | :group 'cperl-faces) |
377 | ||
ccc3ce39 SE |
378 | (defcustom cperl-pod-here-fontify '(featurep 'font-lock) |
379 | "*Not-nil after evaluation means to highlight pod and here-docs sections." | |
380 | :type 'boolean | |
db133cb6 | 381 | :group 'cperl-faces) |
f83d2997 | 382 | |
5bd52f0e RS |
383 | (defcustom cperl-fontify-m-as-s t |
384 | "*Not-nil means highlight 1arg regular expressions operators same as 2arg." | |
385 | :type 'boolean | |
386 | :group 'cperl-faces) | |
387 | ||
6c389151 SM |
388 | (defcustom cperl-highlight-variables-indiscriminately nil |
389 | "*Non-nil means perform additional highlighting on variables. | |
390 | Currently only changes how scalar variables are highlighted. | |
391 | Note that that variable is only read at initialization time for | |
392 | the variable `cperl-font-lock-keywords-2', so changing it after you've | |
f94a632a | 393 | entered CPerl mode the first time will have no effect." |
6c389151 SM |
394 | :type 'boolean |
395 | :group 'cperl) | |
396 | ||
ccc3ce39 | 397 | (defcustom cperl-pod-here-scan t |
f83d2997 | 398 | "*Not-nil means look for pod and here-docs sections during startup. |
ccc3ce39 SE |
399 | You can always make lookup from menu or using \\[cperl-find-pods-heres]." |
400 | :type 'boolean | |
db133cb6 | 401 | :group 'cperl-speed) |
f83d2997 | 402 | |
6c389151 SM |
403 | (defcustom cperl-regexp-scan t |
404 | "*Not-nil means make marking of regular expression more thorough. | |
405 | Effective only with `cperl-pod-here-scan'. Not implemented yet." | |
406 | :type 'boolean | |
407 | :group 'cperl-speed) | |
408 | ||
ccc3ce39 | 409 | (defcustom cperl-imenu-addback nil |
f83d2997 | 410 | "*Not-nil means add backreferences to generated `imenu's. |
db133cb6 | 411 | May require patched `imenu' and `imenu-go'. Obsolete." |
ccc3ce39 | 412 | :type 'boolean |
db133cb6 | 413 | :group 'cperl-help-system) |
f83d2997 | 414 | |
ccc3ce39 SE |
415 | (defcustom cperl-max-help-size 66 |
416 | "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents." | |
417 | :type '(choice integer (const nil)) | |
db133cb6 | 418 | :group 'cperl-help-system) |
f83d2997 | 419 | |
ccc3ce39 SE |
420 | (defcustom cperl-shrink-wrap-info-frame t |
421 | "*Non-nil means shrink-wrapping of info-buffer-frame allowed." | |
422 | :type 'boolean | |
db133cb6 | 423 | :group 'cperl-help-system) |
f83d2997 | 424 | |
ccc3ce39 | 425 | (defcustom cperl-info-page "perl" |
f83d2997 | 426 | "*Name of the info page containing perl docs. |
ccc3ce39 SE |
427 | Older version of this page was called `perl5', newer `perl'." |
428 | :type 'string | |
db133cb6 | 429 | :group 'cperl-help-system) |
f83d2997 | 430 | |
5c8b7eaf | 431 | (defcustom cperl-use-syntax-table-text-property |
f83d2997 | 432 | (boundp 'parse-sexp-lookup-properties) |
ccc3ce39 SE |
433 | "*Non-nil means CPerl sets up and uses `syntax-table' text property." |
434 | :type 'boolean | |
db133cb6 | 435 | :group 'cperl-speed) |
f83d2997 | 436 | |
5c8b7eaf | 437 | (defcustom cperl-use-syntax-table-text-property-for-tags |
f83d2997 | 438 | cperl-use-syntax-table-text-property |
ccc3ce39 SE |
439 | "*Non-nil means: set up and use `syntax-table' text property generating TAGS." |
440 | :type 'boolean | |
db133cb6 | 441 | :group 'cperl-speed) |
ccc3ce39 SE |
442 | |
443 | (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$" | |
444 | "*Regexp to match files to scan when generating TAGS." | |
445 | :type 'regexp | |
446 | :group 'cperl) | |
447 | ||
448 | (defcustom cperl-noscan-files-regexp "/\\(\\.\\.?\\|SCCS\\|RCS\\|blib\\)$" | |
449 | "*Regexp to match files/dirs to skip when generating TAGS." | |
450 | :type 'regexp | |
451 | :group 'cperl) | |
452 | ||
453 | (defcustom cperl-regexp-indent-step nil | |
454 | "*Indentation used when beautifying regexps. | |
029cb4d5 | 455 | If nil, the value of `cperl-indent-level' will be used." |
ccc3ce39 | 456 | :type '(choice integer (const nil)) |
db133cb6 | 457 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
458 | |
459 | (defcustom cperl-indent-left-aligned-comments t | |
460 | "*Non-nil means that the comment starting in leftmost column should indent." | |
461 | :type 'boolean | |
db133cb6 | 462 | :group 'cperl-indentation-details) |
ccc3ce39 | 463 | |
8f222248 | 464 | (defcustom cperl-under-as-char nil |
ccc3ce39 SE |
465 | "*Non-nil means that the _ (underline) should be treated as word char." |
466 | :type 'boolean | |
467 | :group 'cperl) | |
f83d2997 | 468 | |
db133cb6 RS |
469 | (defcustom cperl-extra-perl-args "" |
470 | "*Extra arguments to use when starting Perl. | |
471 | Currently used with `cperl-check-syntax' only." | |
472 | :type 'string | |
473 | :group 'cperl) | |
474 | ||
475 | (defcustom cperl-message-electric-keyword t | |
476 | "*Non-nil means that the `cperl-electric-keyword' prints a help message." | |
477 | :type 'boolean | |
478 | :group 'cperl-help-system) | |
479 | ||
480 | (defcustom cperl-indent-region-fix-constructs 1 | |
481 | "*Amount of space to insert between `}' and `else' or `elsif' | |
482 | in `cperl-indent-region'. Set to nil to leave as is. Values other | |
483 | than 1 and nil will probably not work." | |
484 | :type '(choice (const nil) (const 1)) | |
485 | :group 'cperl-indentation-details) | |
486 | ||
487 | (defcustom cperl-break-one-line-blocks-when-indent t | |
488 | "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs | |
2022c546 | 489 | need to be reformatted into multiline ones when indenting a region." |
db133cb6 RS |
490 | :type 'boolean |
491 | :group 'cperl-indentation-details) | |
492 | ||
493 | (defcustom cperl-fix-hanging-brace-when-indent t | |
494 | "*Non-nil means that BLOCK-end `}' may be put on a separate line | |
5c8b7eaf | 495 | when indenting a region. |
db133cb6 RS |
496 | Braces followed by else/elsif/while/until are excepted." |
497 | :type 'boolean | |
498 | :group 'cperl-indentation-details) | |
499 | ||
500 | (defcustom cperl-merge-trailing-else t | |
5c8b7eaf | 501 | "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue |
db133cb6 RS |
502 | may be merged to be on the same line when indenting a region." |
503 | :type 'boolean | |
504 | :group 'cperl-indentation-details) | |
505 | ||
6c389151 SM |
506 | (defcustom cperl-indent-parens-as-block nil |
507 | "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks, | |
508 | but for trailing \",\" inside the group, which won't increase indentation. | |
509 | One should tune up `cperl-close-paren-offset' as well." | |
510 | :type 'boolean | |
511 | :group 'cperl-indentation-details) | |
512 | ||
513 | (defcustom cperl-syntaxify-by-font-lock | |
514 | (and window-system | |
5bd52f0e | 515 | (boundp 'parse-sexp-lookup-properties)) |
6c389151 | 516 | "*Non-nil means that CPerl uses `font-lock's routines for syntaxification." |
5bd52f0e RS |
517 | :type '(choice (const message) boolean) |
518 | :group 'cperl-speed) | |
519 | ||
520 | (defcustom cperl-syntaxify-unwind | |
521 | t | |
f94a632a | 522 | "*Non-nil means that CPerl unwinds to a start of a long construction |
5bd52f0e | 523 | when syntaxifying a chunk of buffer." |
db133cb6 RS |
524 | :type 'boolean |
525 | :group 'cperl-speed) | |
526 | ||
5bd52f0e RS |
527 | (defcustom cperl-ps-print-face-properties |
528 | '((font-lock-keyword-face nil nil bold shadow) | |
529 | (font-lock-variable-name-face nil nil bold) | |
530 | (font-lock-function-name-face nil nil bold italic box) | |
531 | (font-lock-constant-face nil "LightGray" bold) | |
532 | (cperl-array-face nil "LightGray" bold underline) | |
533 | (cperl-hash-face nil "LightGray" bold italic underline) | |
534 | (font-lock-comment-face nil "LightGray" italic) | |
535 | (font-lock-string-face nil nil italic underline) | |
536 | (cperl-nonoverridable-face nil nil italic underline) | |
537 | (font-lock-type-face nil nil underline) | |
538 | (underline nil "LightGray" strikeout)) | |
539 | "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'." | |
5c8b7eaf | 540 | :type '(repeat (cons symbol |
5bd52f0e RS |
541 | (cons (choice (const nil) string) |
542 | (cons (choice (const nil) string) | |
543 | (repeat symbol))))) | |
544 | :group 'cperl-faces) | |
545 | ||
546 | (if window-system | |
547 | (progn | |
5c8b7eaf | 548 | (defvar cperl-dark-background |
5bd52f0e | 549 | (cperl-choose-color "navy" "os2blue" "darkgreen")) |
5c8b7eaf | 550 | (defvar cperl-dark-foreground |
5bd52f0e RS |
551 | (cperl-choose-color "orchid1" "orange")) |
552 | ||
553 | (defface cperl-nonoverridable-face | |
b787fc05 | 554 | `((((class grayscale) (background light)) |
58b64ac7 | 555 | (:background "Gray90" :slant italic :underline t)) |
b787fc05 | 556 | (((class grayscale) (background dark)) |
58b64ac7 | 557 | (:foreground "Gray80" :slant italic :underline t :weight bold)) |
5c8b7eaf | 558 | (((class color) (background light)) |
b787fc05 | 559 | (:foreground "chartreuse3")) |
5c8b7eaf | 560 | (((class color) (background dark)) |
b787fc05 | 561 | (:foreground ,cperl-dark-foreground)) |
58b64ac7 | 562 | (t (:weight bold :underline t))) |
5bd52f0e RS |
563 | "Font Lock mode face used to highlight array names." |
564 | :group 'cperl-faces) | |
565 | ||
566 | (defface cperl-array-face | |
b787fc05 | 567 | `((((class grayscale) (background light)) |
58b64ac7 | 568 | (:background "Gray90" :weight bold)) |
b787fc05 | 569 | (((class grayscale) (background dark)) |
58b64ac7 | 570 | (:foreground "Gray80" :weight bold)) |
5c8b7eaf | 571 | (((class color) (background light)) |
58b64ac7 | 572 | (:foreground "Blue" :background "lightyellow2" :weight bold)) |
5c8b7eaf | 573 | (((class color) (background dark)) |
58b64ac7 RS |
574 | (:foreground "yellow" :background ,cperl-dark-background :weight bold)) |
575 | (t (:weight bold))) | |
5bd52f0e RS |
576 | "Font Lock mode face used to highlight array names." |
577 | :group 'cperl-faces) | |
578 | ||
579 | (defface cperl-hash-face | |
b787fc05 | 580 | `((((class grayscale) (background light)) |
58b64ac7 | 581 | (:background "Gray90" :weight bold :slant italic)) |
b787fc05 | 582 | (((class grayscale) (background dark)) |
58b64ac7 | 583 | (:foreground "Gray80" :weight bold :slant italic)) |
5c8b7eaf | 584 | (((class color) (background light)) |
58b64ac7 | 585 | (:foreground "Red" :background "lightyellow2" :weight bold :slant italic)) |
5c8b7eaf | 586 | (((class color) (background dark)) |
58b64ac7 RS |
587 | (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic)) |
588 | (t (:weight bold :slant italic))) | |
5bd52f0e RS |
589 | "Font Lock mode face used to highlight hash names." |
590 | :group 'cperl-faces))) | |
591 | ||
f83d2997 KH |
592 | \f |
593 | ||
594 | ;;; Short extra-docs. | |
595 | ||
596 | (defvar cperl-tips 'please-ignore-this-line | |
597 | "Get newest version of this package from | |
598 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs | |
599 | and/or | |
600 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl | |
db133cb6 RS |
601 | Subdirectory `cperl-mode' may contain yet newer development releases and/or |
602 | patches to related files. | |
f83d2997 | 603 | |
5bd52f0e RS |
604 | For best results apply to an older Emacs the patches from |
605 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches | |
8e3acc66 RS |
606 | \(this upgrades syntax-parsing abilities of Emaxen v19.34 and |
607 | v20.2 up to the level of Emacs v20.3 - a must for a good Perl | |
608 | mode.) | |
5bd52f0e | 609 | |
f83d2997 KH |
610 | Get support packages choose-color.el (or font-lock-extra.el before |
611 | 19.30), imenu-go.el from the same place. \(Look for other files there | |
612 | too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and | |
5c8b7eaf | 613 | later you should use choose-color.el *instead* of font-lock-extra.el |
f83d2997 KH |
614 | \(and you will not get smart highlighting in C :-(). |
615 | ||
616 | Note that to enable Compile choices in the menu you need to install | |
617 | mode-compile.el. | |
618 | ||
5efe6a56 SM |
619 | If your Emacs does not default to `cperl-mode' on Perl files, and you |
620 | want it to: put the following into your .emacs file: | |
621 | ||
622 | (defalias 'perl-mode 'cperl-mode) | |
623 | ||
624 | Get perl5-info from | |
f83d2997 KH |
625 | $CPAN/doc/manual/info/perl-info.tar.gz |
626 | older version was on | |
627 | http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz | |
628 | ||
629 | If you use imenu-go, run imenu on perl5-info buffer (you can do it | |
5bd52f0e RS |
630 | from Perl menu). If many files are related, generate TAGS files from |
631 | Tools/Tags submenu in Perl menu. | |
f83d2997 KH |
632 | |
633 | If some class structure is too complicated, use Tools/Hierarchy-view | |
029cb4d5 | 634 | from Perl menu, or hierarchic view of imenu. The second one uses the |
f83d2997 | 635 | current buffer only, the first one requires generation of TAGS from |
5bd52f0e RS |
636 | Perl/Tools/Tags menu beforehand. |
637 | ||
638 | Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing. | |
639 | ||
640 | Switch auto-help on/off with Perl/Tools/Auto-help. | |
641 | ||
642 | Though with contemporary Emaxen CPerl mode should maintain the correct | |
643 | parsing of Perl even when editing, sometimes it may be lost. Fix this by | |
644 | ||
029cb4d5 | 645 | \\[normal-mode] |
f83d2997 | 646 | |
5bd52f0e | 647 | In cases of more severe confusion sometimes it is helpful to do |
f83d2997 | 648 | |
029cb4d5 SM |
649 | \\[load-library] cperl-mode RET |
650 | \\[normal-mode] | |
f83d2997 | 651 | |
5bd52f0e RS |
652 | Before reporting (non-)problems look in the problem section of online |
653 | micro-docs on what I know about CPerl problems.") | |
f83d2997 KH |
654 | |
655 | (defvar cperl-problems 'please-ignore-this-line | |
f94a632a RS |
656 | "Description of problems in CPerl mode. |
657 | Some faces will not be shown on some versions of Emacs unless you | |
bab27c0c RS |
658 | install choose-color.el, available from |
659 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs/ | |
660 | ||
6c389151 SM |
661 | `fill-paragraph' on a comment may leave the point behind the |
662 | paragraph. Parsing of lines with several <<EOF is not implemented | |
663 | yet. | |
664 | ||
8e3acc66 | 665 | Emacs had a _very_ restricted syntax parsing engine until version |
5bd52f0e | 666 | 20.1. Most problems below are corrected starting from this version of |
8e3acc66 RS |
667 | Emacs, and all of them should be fixed in version 20.3. (Or apply |
668 | patches to Emacs 19.33/34 - see tips.) | |
5bd52f0e | 669 | |
6c389151 SM |
670 | Note that even with newer Emacsen in some very rare cases the details |
671 | of interaction of `font-lock' and syntaxification may be not cleaned | |
672 | up yet. You may get slightly different colors basing on the order of | |
673 | fontification and syntaxification. Say, the initial faces is correct, | |
674 | but editing the buffer breaks this. | |
f83d2997 | 675 | |
db133cb6 RS |
676 | Even with older Emacsen CPerl mode tries to corrects some Emacs |
677 | misunderstandings, however, for efficiency reasons the degree of | |
678 | correction is different for different operations. The partially | |
679 | corrected problems are: POD sections, here-documents, regexps. The | |
680 | operations are: highlighting, indentation, electric keywords, electric | |
681 | braces. | |
f83d2997 KH |
682 | |
683 | This may be confusing, since the regexp s#//#/#\; may be highlighted | |
684 | as a comment, but it will be recognized as a regexp by the indentation | |
685 | code. Or the opposite case, when a pod section is highlighted, but | |
686 | may break the indentation of the following code (though indentation | |
687 | should work if the balance of delimiters is not broken by POD). | |
688 | ||
689 | The main trick (to make $ a \"backslash\") makes constructions like | |
690 | ${aaa} look like unbalanced braces. The only trick I can think of is | |
5c8b7eaf | 691 | to insert it as $ {aaa} (legal in perl5, not in perl4). |
f83d2997 KH |
692 | |
693 | Similar problems arise in regexps, when /(\\s|$)/ should be rewritten | |
db133cb6 RS |
694 | as /($|\\s)/. Note that such a transposition is not always possible. |
695 | ||
5bd52f0e | 696 | The solution is to upgrade your Emacs or patch an older one. Note |
8e3acc66 | 697 | that Emacs 20.2 has some bugs related to `syntax-table' text |
5bd52f0e RS |
698 | properties. Patches are available on the main CPerl download site, |
699 | and on CPAN. | |
db133cb6 RS |
700 | |
701 | If these bugs cannot be fixed on your machine (say, you have an inferior | |
702 | environment and cannot recompile), you may still disable all the fancy stuff | |
703 | via `cperl-use-syntax-table-text-property'." ) | |
f83d2997 | 704 | |
f83d2997 | 705 | (defvar cperl-praise 'please-ignore-this-line |
8e3acc66 | 706 | "Advantages of CPerl mode. |
f83d2997 KH |
707 | |
708 | 0) It uses the newest `syntax-table' property ;-); | |
709 | ||
710 | 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl | |
5c8b7eaf | 711 | mode - but the latter number may have improved too in last years) even |
5bd52f0e RS |
712 | with old Emaxen which do not support `syntax-table' property. |
713 | ||
714 | When using `syntax-table' property for syntax assist hints, it should | |
715 | handle 99.995% of lines correct - or somesuch. It automatically | |
716 | updates syntax assist hints when you edit your script. | |
f83d2997 | 717 | |
bab27c0c | 718 | 2) It is generally believed to be \"the most user-friendly Emacs |
f83d2997 KH |
719 | package\" whatever it may mean (I doubt that the people who say similar |
720 | things tried _all_ the rest of Emacs ;-), but this was not a lonely | |
721 | voice); | |
722 | ||
723 | 3) Everything is customizable, one-by-one or in a big sweep; | |
724 | ||
725 | 4) It has many easily-accessable \"tools\": | |
726 | a) Can run program, check syntax, start debugger; | |
727 | b) Can lineup vertically \"middles\" of rows, like `=' in | |
728 | a = b; | |
729 | cc = d; | |
730 | c) Can insert spaces where this impoves readability (in one | |
731 | interactive sweep over the buffer); | |
732 | d) Has support for imenu, including: | |
733 | 1) Separate unordered list of \"interesting places\"; | |
734 | 2) Separate TOC of POD sections; | |
735 | 3) Separate list of packages; | |
736 | 4) Hierarchical view of methods in (sub)packages; | |
737 | 5) and functions (by the full name - with package); | |
738 | e) Has an interface to INFO docs for Perl; The interface is | |
739 | very flexible, including shrink-wrapping of | |
740 | documentation buffer/frame; | |
741 | f) Has a builtin list of one-line explanations for perl constructs. | |
742 | g) Can show these explanations if you stay long enough at the | |
743 | corresponding place (or on demand); | |
744 | h) Has an enhanced fontification (using 3 or 4 additional faces | |
745 | comparing to font-lock - basically, different | |
746 | namespaces in Perl have different colors); | |
747 | i) Can construct TAGS basing on its knowledge of Perl syntax, | |
748 | the standard menu has 6 different way to generate | |
db133cb6 | 749 | TAGS (if \"by directory\", .xs files - with C-language |
f83d2997 KH |
750 | bindings - are included in the scan); |
751 | j) Can build a hierarchical view of classes (via imenu) basing | |
752 | on generated TAGS file; | |
753 | k) Has electric parentheses, electric newlines, uses Abbrev | |
754 | for electric logical constructs | |
755 | while () {} | |
756 | with different styles of expansion (context sensitive | |
757 | to be not so bothering). Electric parentheses behave | |
758 | \"as they should\" in a presence of a visible region. | |
759 | l) Changes msb.el \"on the fly\" to insert a group \"Perl files\"; | |
db133cb6 RS |
760 | m) Can convert from |
761 | if (A) { B } | |
762 | to | |
763 | B if A; | |
f83d2997 | 764 | |
5bd52f0e | 765 | n) Highlights (by user-choice) either 3-delimiters constructs |
6c389151 SM |
766 | (such as tr/a/b/), or regular expressions and `y/tr'; |
767 | o) Highlights trailing whitespace; | |
768 | p) Is able to manipulate Perl Regular Expressions to ease | |
769 | conversion to a more readable form. | |
5bd52f0e | 770 | |
f83d2997 KH |
771 | 5) The indentation engine was very smart, but most of tricks may be |
772 | not needed anymore with the support for `syntax-table' property. Has | |
773 | progress indicator for indentation (with `imenu' loaded). | |
774 | ||
5c8b7eaf | 775 | 6) Indent-region improves inline-comments as well; also corrects |
db133cb6 | 776 | whitespace *inside* the conditional/loop constructs. |
f83d2997 KH |
777 | |
778 | 7) Fill-paragraph correctly handles multi-line comments; | |
db133cb6 RS |
779 | |
780 | 8) Can switch to different indentation styles by one command, and restore | |
781 | the settings present before the switch. | |
782 | ||
5c8b7eaf | 783 | 9) When doing indentation of control constructs, may correct |
db133cb6 | 784 | line-breaks/spacing between elements of the construct. |
029cb4d5 SM |
785 | |
786 | 10) Uses a linear-time algorith for indentation of regions (on Emaxen with | |
787 | capable syntax engines). | |
db133cb6 RS |
788 | ") |
789 | ||
790 | (defvar cperl-speed 'please-ignore-this-line | |
791 | "This is an incomplete compendium of what is available in other parts | |
792 | of CPerl documentation. (Please inform me if I skept anything.) | |
793 | ||
794 | There is a perception that CPerl is slower than alternatives. This part | |
795 | of documentation is designed to overcome this misconception. | |
796 | ||
797 | *By default* CPerl tries to enable the most comfortable settings. | |
798 | From most points of view, correctly working package is infinitely more | |
799 | comfortable than a non-correctly working one, thus by default CPerl | |
800 | prefers correctness over speed. Below is the guide how to change | |
801 | settings if your preferences are different. | |
802 | ||
803 | A) Speed of loading the file. When loading file, CPerl may perform a | |
804 | scan which indicates places which cannot be parsed by primitive Emacs | |
805 | syntax-parsing routines, and marks them up so that either | |
806 | ||
807 | A1) CPerl may work around these deficiencies (for big chunks, mostly | |
808 | PODs and HERE-documents), or | |
809 | A2) On capable Emaxen CPerl will use improved syntax-handlings | |
810 | which reads mark-up hints directly. | |
811 | ||
812 | The scan in case A2 is much more comprehensive, thus may be slower. | |
813 | ||
814 | User can disable syntax-engine-helping scan of A2 by setting | |
815 | `cperl-use-syntax-table-text-property' | |
816 | variable to nil (if it is set to t). | |
817 | ||
818 | One can disable the scan altogether (both A1 and A2) by setting | |
819 | `cperl-pod-here-scan' | |
820 | to nil. | |
821 | ||
5c8b7eaf | 822 | B) Speed of editing operations. |
db133cb6 RS |
823 | |
824 | One can add a (minor) speedup to editing operations by setting | |
825 | `cperl-use-syntax-table-text-property' | |
826 | variable to nil (if it is set to t). This will disable | |
827 | syntax-engine-helping scan, thus will make many more Perl | |
828 | constructs be wrongly recognized by CPerl, thus may lead to | |
829 | wrongly matched parentheses, wrong indentation, etc. | |
5bd52f0e RS |
830 | |
831 | One can unset `cperl-syntaxify-unwind'. This might speed up editing | |
832 | of, say, long POD sections. | |
f83d2997 KH |
833 | ") |
834 | ||
5bd52f0e RS |
835 | (defvar cperl-tips-faces 'please-ignore-this-line |
836 | "CPerl mode uses following faces for highlighting: | |
837 | ||
8661c643 DL |
838 | `cperl-array-face' Array names |
839 | `cperl-hash-face' Hash names | |
840 | `font-lock-comment-face' Comments, PODs and whatever is considered | |
5bd52f0e | 841 | syntaxically to be not code |
8661c643 | 842 | `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of |
5bd52f0e | 843 | 2-arg operators s/y/tr/ or of RExen, |
8661c643 | 844 | `font-lock-function-name-face' Special-cased m// and s//foo/, _ as |
5bd52f0e RS |
845 | a target of a file tests, file tests, |
846 | subroutine names at the moment of definition | |
847 | (except those conflicting with Perl operators), | |
848 | package names (when recognized), format names | |
8661c643 DL |
849 | `font-lock-keyword-face' Control flow switch constructs, declarators |
850 | `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen | |
851 | `font-lock-string-face' Strings, qw() constructs, RExen, POD sections, | |
5bd52f0e RS |
852 | literal parts and the terminator of formats |
853 | and whatever is syntaxically considered | |
854 | as string literals | |
8661c643 DL |
855 | `font-lock-type-face' Overridable keywords |
856 | `font-lock-variable-name-face' Variable declarations, indirect array and | |
5bd52f0e | 857 | hash names, POD headers/item names |
8661c643 | 858 | `cperl-invalid-face' Trailing whitespace |
5bd52f0e RS |
859 | |
860 | Note that in several situations the highlighting tries to inform about | |
861 | possible confusion, such as different colors for function names in | |
862 | declarations depending on what they (do not) override, or special cases | |
863 | m// and s/// which do not do what one would expect them to do. | |
864 | ||
5c8b7eaf | 865 | Help with best setup of these faces for printout requested (for each of |
5bd52f0e RS |
866 | the faces: please specify bold, italic, underline, shadow and box.) |
867 | ||
868 | \(Not finished.)") | |
869 | ||
f83d2997 KH |
870 | \f |
871 | ||
872 | ;;; Portability stuff: | |
873 | ||
db133cb6 RS |
874 | (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version)) |
875 | ||
f83d2997 | 876 | (defmacro cperl-define-key (emacs-key definition &optional xemacs-key) |
b787fc05 GM |
877 | `(define-key cperl-mode-map |
878 | ,(if xemacs-key | |
879 | `(if cperl-xemacs-p ,xemacs-key ,emacs-key) | |
880 | emacs-key) | |
881 | ,definition)) | |
f83d2997 KH |
882 | |
883 | (defvar cperl-del-back-ch | |
884 | (car (append (where-is-internal 'delete-backward-char) | |
885 | (where-is-internal 'backward-delete-char-untabify))) | |
029cb4d5 | 886 | "Character generated by key bound to `delete-backward-char'.") |
f83d2997 | 887 | |
5c8b7eaf | 888 | (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1) |
f83d2997 KH |
889 | (setq cperl-del-back-ch (aref cperl-del-back-ch 0))) |
890 | ||
db133cb6 | 891 | (defun cperl-mark-active () (mark)) ; Avoid undefined warning |
f83d2997 KH |
892 | (if cperl-xemacs-p |
893 | (progn | |
894 | ;; "Active regions" are on: use region only if active | |
895 | ;; "Active regions" are off: use region unconditionally | |
896 | (defun cperl-use-region-p () | |
db133cb6 | 897 | (if zmacs-regions (mark) t))) |
f83d2997 KH |
898 | (defun cperl-use-region-p () |
899 | (if transient-mark-mode mark-active t)) | |
900 | (defun cperl-mark-active () mark-active)) | |
901 | ||
902 | (defsubst cperl-enable-font-lock () | |
903 | (or cperl-xemacs-p window-system)) | |
904 | ||
db133cb6 RS |
905 | (defun cperl-putback-char (c) ; Emacs 19 |
906 | (set 'unread-command-events (list c))) ; Avoid undefined warning | |
907 | ||
f83d2997 KH |
908 | (if (boundp 'unread-command-events) |
909 | (if cperl-xemacs-p | |
910 | (defun cperl-putback-char (c) ; XEmacs >= 19.12 | |
db133cb6 | 911 | (setq unread-command-events (list (eval '(character-to-event c)))))) |
f83d2997 | 912 | (defun cperl-putback-char (c) ; XEmacs <= 19.11 |
db133cb6 | 913 | (set 'unread-command-event (eval '(character-to-event c))))) ; Avoid warnings |
f83d2997 KH |
914 | |
915 | (or (fboundp 'uncomment-region) | |
916 | (defun uncomment-region (beg end) | |
917 | (interactive "r") | |
918 | (comment-region beg end -1))) | |
919 | ||
920 | (defvar cperl-do-not-fontify | |
921 | (if (string< emacs-version "19.30") | |
922 | 'fontified | |
923 | 'lazy-lock) | |
924 | "Text property which inhibits refontification.") | |
925 | ||
5bd52f0e RS |
926 | (defsubst cperl-put-do-not-fontify (from to &optional post) |
927 | ;; If POST, do not do it with postponed fontification | |
928 | (if (and post cperl-syntaxify-by-font-lock) | |
929 | nil | |
f83d2997 | 930 | (put-text-property (max (point-min) (1- from)) |
5bd52f0e | 931 | to cperl-do-not-fontify t))) |
f83d2997 | 932 | |
ccc3ce39 | 933 | (defcustom cperl-mode-hook nil |
f94a632a | 934 | "Hook run by CPerl mode." |
ccc3ce39 SE |
935 | :type 'hook |
936 | :group 'cperl) | |
f83d2997 | 937 | |
db133cb6 RS |
938 | (defvar cperl-syntax-state nil) |
939 | (defvar cperl-syntax-done-to nil) | |
5bd52f0e | 940 | (defvar cperl-emacs-can-parse (> (length (save-excursion |
ce22dd53 | 941 | (parse-partial-sexp (point) (point)))) 9)) |
db133cb6 RS |
942 | \f |
943 | ;; Make customization possible "in reverse" | |
944 | (defsubst cperl-val (symbol &optional default hairy) | |
945 | (cond | |
946 | ((eq (symbol-value symbol) 'null) default) | |
947 | (cperl-hairy (or hairy t)) | |
948 | (t (symbol-value symbol)))) | |
f83d2997 KH |
949 | \f |
950 | ;;; Probably it is too late to set these guys already, but it can help later: | |
951 | ||
5bd52f0e | 952 | ;;;(and cperl-clobber-mode-lists |
f83d2997 KH |
953 | ;;;(setq auto-mode-alist |
954 | ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist )) | |
955 | ;;;(and (boundp 'interpreter-mode-alist) | |
956 | ;;; (setq interpreter-mode-alist (append interpreter-mode-alist | |
5bd52f0e | 957 | ;;; '(("miniperl" . perl-mode)))))) |
80585273 DL |
958 | (eval-when-compile |
959 | (condition-case nil | |
960 | (require 'imenu) | |
961 | (error nil)) | |
962 | (condition-case nil | |
963 | (require 'easymenu) | |
964 | (error nil)) | |
965 | (condition-case nil | |
966 | (require 'etags) | |
967 | (error nil)) | |
968 | (condition-case nil | |
969 | (require 'timer) | |
970 | (error nil)) | |
971 | (condition-case nil | |
972 | (require 'man) | |
973 | (error nil)) | |
974 | (condition-case nil | |
975 | (require 'info) | |
976 | (error nil)) | |
977 | (if (fboundp 'ps-extend-face-list) | |
978 | (defmacro cperl-ps-extend-face-list (arg) | |
979 | `(ps-extend-face-list ,arg)) | |
980 | (defmacro cperl-ps-extend-face-list (arg) | |
e8af40ee | 981 | `(error "This version of Emacs has no `ps-extend-face-list'"))) |
80585273 DL |
982 | ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs, |
983 | ;; macros instead of defsubsts don't work on Emacs, so we do the | |
984 | ;; expansion manually. Any other suggestions? | |
985 | (require 'cl)) | |
f83d2997 KH |
986 | |
987 | (defvar cperl-mode-abbrev-table nil | |
f94a632a | 988 | "Abbrev table in use in CPerl mode buffers.") |
f83d2997 KH |
989 | |
990 | (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-"))) | |
991 | ||
992 | (defvar cperl-mode-map () "Keymap used in CPerl mode.") | |
993 | ||
994 | (if cperl-mode-map nil | |
995 | (setq cperl-mode-map (make-sparse-keymap)) | |
996 | (cperl-define-key "{" 'cperl-electric-lbrace) | |
997 | (cperl-define-key "[" 'cperl-electric-paren) | |
998 | (cperl-define-key "(" 'cperl-electric-paren) | |
999 | (cperl-define-key "<" 'cperl-electric-paren) | |
1000 | (cperl-define-key "}" 'cperl-electric-brace) | |
1001 | (cperl-define-key "]" 'cperl-electric-rparen) | |
1002 | (cperl-define-key ")" 'cperl-electric-rparen) | |
1003 | (cperl-define-key ";" 'cperl-electric-semi) | |
1004 | (cperl-define-key ":" 'cperl-electric-terminator) | |
1005 | (cperl-define-key "\C-j" 'newline-and-indent) | |
1006 | (cperl-define-key "\C-c\C-j" 'cperl-linefeed) | |
db133cb6 | 1007 | (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless) |
f83d2997 KH |
1008 | (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline) |
1009 | (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev) | |
db133cb6 RS |
1010 | (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix) |
1011 | (cperl-define-key "\C-c\C-f" 'auto-fill-mode) | |
f83d2997 | 1012 | (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric) |
db133cb6 | 1013 | (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp) |
f83d2997 KH |
1014 | (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound |
1015 | (cperl-define-key [?\C-\M-\|] 'cperl-lineup | |
1016 | [(control meta |)]) | |
1017 | ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph) | |
1018 | ;;(cperl-define-key "\e;" 'cperl-indent-for-comment) | |
1019 | (cperl-define-key "\177" 'cperl-electric-backspace) | |
1020 | (cperl-define-key "\t" 'cperl-indent-command) | |
1021 | ;; don't clobber the backspace binding: | |
db133cb6 RS |
1022 | (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command |
1023 | [(control c) (control h) F]) | |
db133cb6 RS |
1024 | (if (cperl-val 'cperl-clobber-lisp-bindings) |
1025 | (progn | |
1026 | (cperl-define-key "\C-hf" | |
1027 | ;;(concat (char-to-string help-char) "f") ; does not work | |
1028 | 'cperl-info-on-command | |
1029 | [(control h) f]) | |
1030 | (cperl-define-key "\C-hv" | |
1031 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1032 | 'cperl-get-help | |
5bd52f0e RS |
1033 | [(control h) v]) |
1034 | (cperl-define-key "\C-c\C-hf" | |
1035 | ;;(concat (char-to-string help-char) "f") ; does not work | |
1036 | (key-binding "\C-hf") | |
1037 | [(control c) (control h) f]) | |
1038 | (cperl-define-key "\C-c\C-hv" | |
1039 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1040 | (key-binding "\C-hv") | |
1041 | [(control c) (control h) v])) | |
1042 | (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command | |
1043 | [(control c) (control h) f]) | |
1044 | (cperl-define-key "\C-c\C-hv" | |
1045 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1046 | 'cperl-get-help | |
1047 | [(control c) (control h) v])) | |
5c8b7eaf | 1048 | (if (and cperl-xemacs-p |
f83d2997 KH |
1049 | (<= emacs-minor-version 11) (<= emacs-major-version 19)) |
1050 | (progn | |
1051 | ;; substitute-key-definition is usefulness-deenhanced... | |
1052 | (cperl-define-key "\M-q" 'cperl-fill-paragraph) | |
1053 | (cperl-define-key "\e;" 'cperl-indent-for-comment) | |
1054 | (cperl-define-key "\e\C-\\" 'cperl-indent-region)) | |
1055 | (substitute-key-definition | |
1056 | 'indent-sexp 'cperl-indent-exp | |
1057 | cperl-mode-map global-map) | |
1058 | (substitute-key-definition | |
1059 | 'fill-paragraph 'cperl-fill-paragraph | |
1060 | cperl-mode-map global-map) | |
1061 | (substitute-key-definition | |
1062 | 'indent-region 'cperl-indent-region | |
1063 | cperl-mode-map global-map) | |
1064 | (substitute-key-definition | |
1065 | 'indent-for-comment 'cperl-indent-for-comment | |
1066 | cperl-mode-map global-map))) | |
1067 | ||
1068 | (defvar cperl-menu) | |
db133cb6 RS |
1069 | (defvar cperl-lazy-installed) |
1070 | (defvar cperl-old-style nil) | |
f83d2997 KH |
1071 | (condition-case nil |
1072 | (progn | |
1073 | (require 'easymenu) | |
1074 | (easy-menu-define cperl-menu cperl-mode-map "Menu for CPerl mode" | |
1075 | '("Perl" | |
1076 | ["Beginning of function" beginning-of-defun t] | |
1077 | ["End of function" end-of-defun t] | |
1078 | ["Mark function" mark-defun t] | |
1079 | ["Indent expression" cperl-indent-exp t] | |
1080 | ["Fill paragraph/comment" cperl-fill-paragraph t] | |
1081 | "----" | |
1082 | ["Line up a construction" cperl-lineup (cperl-use-region-p)] | |
6c389151 | 1083 | ["Invert if/unless/while etc" cperl-invert-if-unless t] |
db133cb6 RS |
1084 | ("Regexp" |
1085 | ["Beautify" cperl-beautify-regexp | |
1086 | cperl-use-syntax-table-text-property] | |
6c389151 SM |
1087 | ["Beautify one level deep" (cperl-beautify-regexp 1) |
1088 | cperl-use-syntax-table-text-property] | |
db133cb6 RS |
1089 | ["Beautify a group" cperl-beautify-level |
1090 | cperl-use-syntax-table-text-property] | |
6c389151 SM |
1091 | ["Beautify a group one level deep" (cperl-beautify-level 1) |
1092 | cperl-use-syntax-table-text-property] | |
db133cb6 RS |
1093 | ["Contract a group" cperl-contract-level |
1094 | cperl-use-syntax-table-text-property] | |
1095 | ["Contract groups" cperl-contract-levels | |
1096 | cperl-use-syntax-table-text-property]) | |
f83d2997 KH |
1097 | ["Refresh \"hard\" constructions" cperl-find-pods-heres t] |
1098 | "----" | |
1099 | ["Indent region" cperl-indent-region (cperl-use-region-p)] | |
1100 | ["Comment region" cperl-comment-region (cperl-use-region-p)] | |
1101 | ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)] | |
1102 | "----" | |
1103 | ["Run" mode-compile (fboundp 'mode-compile)] | |
1104 | ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill) | |
1105 | (get-buffer "*compilation*"))] | |
1106 | ["Next error" next-error (get-buffer "*compilation*")] | |
1107 | ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)] | |
1108 | "----" | |
1109 | ["Debugger" cperl-db t] | |
1110 | "----" | |
1111 | ("Tools" | |
1112 | ["Imenu" imenu (fboundp 'imenu)] | |
1113 | ["Insert spaces if needed" cperl-find-bad-style t] | |
1114 | ["Class Hierarchy from TAGS" cperl-tags-hier-init t] | |
1115 | ;;["Update classes" (cperl-tags-hier-init t) tags-table-list] | |
5c8b7eaf | 1116 | ["CPerl pretty print (exprmntl)" cperl-ps-print |
5bd52f0e | 1117 | (fboundp 'ps-extend-face-list)] |
f83d2997 KH |
1118 | ["Imenu on info" cperl-imenu-on-info (featurep 'imenu)] |
1119 | ("Tags" | |
1120 | ;;; ["Create tags for current file" cperl-etags t] | |
1121 | ;;; ["Add tags for current file" (cperl-etags t) t] | |
1122 | ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t] | |
1123 | ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t] | |
5c8b7eaf | 1124 | ;;; ["Create tags for Perl files in (sub)directories" |
f83d2997 KH |
1125 | ;;; (cperl-etags nil 'recursive) t] |
1126 | ;;; ["Add tags for Perl files in (sub)directories" | |
5c8b7eaf | 1127 | ;;; (cperl-etags t 'recursive) t]) |
f83d2997 KH |
1128 | ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer) |
1129 | ["Create tags for current file" (cperl-write-tags nil t) t] | |
1130 | ["Add tags for current file" (cperl-write-tags) t] | |
5c8b7eaf | 1131 | ["Create tags for Perl files in directory" |
f83d2997 | 1132 | (cperl-write-tags nil t nil t) t] |
5c8b7eaf | 1133 | ["Add tags for Perl files in directory" |
f83d2997 | 1134 | (cperl-write-tags nil nil nil t) t] |
5c8b7eaf | 1135 | ["Create tags for Perl files in (sub)directories" |
f83d2997 KH |
1136 | (cperl-write-tags nil t t t) t] |
1137 | ["Add tags for Perl files in (sub)directories" | |
db133cb6 RS |
1138 | (cperl-write-tags nil nil t t) t])) |
1139 | ("Perl docs" | |
5c8b7eaf | 1140 | ["Define word at point" imenu-go-find-at-position |
f83d2997 KH |
1141 | (fboundp 'imenu-go-find-at-position)] |
1142 | ["Help on function" cperl-info-on-command t] | |
1143 | ["Help on function at point" cperl-info-on-current-command t] | |
1144 | ["Help on symbol at point" cperl-get-help t] | |
db133cb6 RS |
1145 | ["Perldoc" cperl-perldoc t] |
1146 | ["Perldoc on word at point" cperl-perldoc-at-point t] | |
1147 | ["View manpage of POD in this file" cperl-pod-to-manpage t] | |
5c8b7eaf | 1148 | ["Auto-help on" cperl-lazy-install |
db133cb6 RS |
1149 | (and (fboundp 'run-with-idle-timer) |
1150 | (not cperl-lazy-installed))] | |
5c8b7eaf | 1151 | ["Auto-help off" (eval '(cperl-lazy-unstall)) |
db133cb6 RS |
1152 | (and (fboundp 'run-with-idle-timer) |
1153 | cperl-lazy-installed)]) | |
f83d2997 KH |
1154 | ("Toggle..." |
1155 | ["Auto newline" cperl-toggle-auto-newline t] | |
1156 | ["Electric parens" cperl-toggle-electric t] | |
1157 | ["Electric keywords" cperl-toggle-abbrev t] | |
db133cb6 | 1158 | ["Fix whitespace on indent" cperl-toggle-construct-fix t] |
5c8b7eaf | 1159 | ["Auto fill" auto-fill-mode t]) |
f83d2997 | 1160 | ("Indent styles..." |
db133cb6 RS |
1161 | ["CPerl" (cperl-set-style "CPerl") t] |
1162 | ["PerlStyle" (cperl-set-style "PerlStyle") t] | |
f83d2997 KH |
1163 | ["GNU" (cperl-set-style "GNU") t] |
1164 | ["C++" (cperl-set-style "C++") t] | |
1165 | ["FSF" (cperl-set-style "FSF") t] | |
1166 | ["BSD" (cperl-set-style "BSD") t] | |
db133cb6 RS |
1167 | ["Whitesmith" (cperl-set-style "Whitesmith") t] |
1168 | ["Current" (cperl-set-style "Current") t] | |
1169 | ["Memorized" (cperl-set-style-back) cperl-old-style]) | |
f83d2997 KH |
1170 | ("Micro-docs" |
1171 | ["Tips" (describe-variable 'cperl-tips) t] | |
1172 | ["Problems" (describe-variable 'cperl-problems) t] | |
db133cb6 RS |
1173 | ["Speed" (describe-variable 'cperl-speed) t] |
1174 | ["Praise" (describe-variable 'cperl-praise) t] | |
5bd52f0e RS |
1175 | ["Faces" (describe-variable 'cperl-tips-faces) t] |
1176 | ["CPerl mode" (describe-function 'cperl-mode) t] | |
5c8b7eaf | 1177 | ["CPerl version" |
bedf0b73 | 1178 | (message "The version of master-file for this CPerl is %s-emacs" |
5bd52f0e | 1179 | cperl-version) t])))) |
f83d2997 KH |
1180 | (error nil)) |
1181 | ||
1182 | (autoload 'c-macro-expand "cmacexp" | |
1183 | "Display the result of expanding all C macros occurring in the region. | |
1184 | The expansion is entirely correct because it uses the C preprocessor." | |
1185 | t) | |
1186 | ||
95f433f4 RS |
1187 | (defvar cperl-imenu--function-name-regexp-perl |
1188 | (concat | |
1189 | "^\\(" | |
1190 | "[ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\(([^()]*)[ \t]*\\)?" | |
1191 | "\\|" | |
1192 | "=head\\([12]\\)[ \t]+\\([^\n]+\\)$" | |
1193 | "\\)")) | |
1194 | ||
8dd511f6 RS |
1195 | (defvar cperl-outline-regexp |
1196 | (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`")) | |
1197 | ||
f83d2997 | 1198 | (defvar cperl-mode-syntax-table nil |
f94a632a | 1199 | "Syntax table in use in CPerl mode buffers.") |
f83d2997 KH |
1200 | |
1201 | (defvar cperl-string-syntax-table nil | |
f94a632a | 1202 | "Syntax table in use in CPerl mode string-like chunks.") |
f83d2997 KH |
1203 | |
1204 | (if cperl-mode-syntax-table | |
1205 | () | |
1206 | (setq cperl-mode-syntax-table (make-syntax-table)) | |
1207 | (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table) | |
1208 | (modify-syntax-entry ?/ "." cperl-mode-syntax-table) | |
1209 | (modify-syntax-entry ?* "." cperl-mode-syntax-table) | |
1210 | (modify-syntax-entry ?+ "." cperl-mode-syntax-table) | |
1211 | (modify-syntax-entry ?- "." cperl-mode-syntax-table) | |
1212 | (modify-syntax-entry ?= "." cperl-mode-syntax-table) | |
1213 | (modify-syntax-entry ?% "." cperl-mode-syntax-table) | |
1214 | (modify-syntax-entry ?< "." cperl-mode-syntax-table) | |
1215 | (modify-syntax-entry ?> "." cperl-mode-syntax-table) | |
1216 | (modify-syntax-entry ?& "." cperl-mode-syntax-table) | |
1217 | (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table) | |
1218 | (modify-syntax-entry ?\n ">" cperl-mode-syntax-table) | |
1219 | (modify-syntax-entry ?# "<" cperl-mode-syntax-table) | |
1220 | (modify-syntax-entry ?' "\"" cperl-mode-syntax-table) | |
1221 | (modify-syntax-entry ?` "\"" cperl-mode-syntax-table) | |
1222 | (if cperl-under-as-char | |
1223 | (modify-syntax-entry ?_ "w" cperl-mode-syntax-table)) | |
1224 | (modify-syntax-entry ?: "_" cperl-mode-syntax-table) | |
1225 | (modify-syntax-entry ?| "." cperl-mode-syntax-table) | |
1226 | (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table)) | |
1227 | (modify-syntax-entry ?$ "." cperl-string-syntax-table) | |
1228 | (modify-syntax-entry ?# "." cperl-string-syntax-table) ; (?# comment ) | |
1229 | ) | |
1230 | ||
1231 | ||
1232 | \f | |
db133cb6 | 1233 | (defvar cperl-faces-init nil) |
f83d2997 KH |
1234 | ;; Fix for msb.el |
1235 | (defvar cperl-msb-fixed nil) | |
1236 | ;;;###autoload | |
1237 | (defun cperl-mode () | |
1238 | "Major mode for editing Perl code. | |
1239 | Expression and list commands understand all C brackets. | |
1240 | Tab indents for Perl code. | |
1241 | Paragraphs are separated by blank lines only. | |
1242 | Delete converts tabs to spaces as it moves back. | |
1243 | ||
1244 | Various characters in Perl almost always come in pairs: {}, (), [], | |
1245 | sometimes <>. When the user types the first, she gets the second as | |
1246 | well, with optional special formatting done on {}. (Disabled by | |
1247 | default.) You can always quote (with \\[quoted-insert]) the left | |
1248 | \"paren\" to avoid the expansion. The processing of < is special, | |
f94a632a | 1249 | since most the time you mean \"less\". CPerl mode tries to guess |
f83d2997 KH |
1250 | whether you want to type pair <>, and inserts is if it |
1251 | appropriate. You can set `cperl-electric-parens-string' to the string that | |
1252 | contains the parenths from the above list you want to be electrical. | |
1253 | Electricity of parenths is controlled by `cperl-electric-parens'. | |
1254 | You may also set `cperl-electric-parens-mark' to have electric parens | |
1255 | look for active mark and \"embrace\" a region if possible.' | |
1256 | ||
1257 | CPerl mode provides expansion of the Perl control constructs: | |
db133cb6 | 1258 | |
5c8b7eaf | 1259 | if, else, elsif, unless, while, until, continue, do, |
db133cb6 RS |
1260 | for, foreach, formy and foreachmy. |
1261 | ||
1262 | and POD directives (Disabled by default, see `cperl-electric-keywords'.) | |
1263 | ||
1264 | The user types the keyword immediately followed by a space, which | |
1265 | causes the construct to be expanded, and the point is positioned where | |
1266 | she is most likely to want to be. eg. when the user types a space | |
1267 | following \"if\" the following appears in the buffer: if () { or if () | |
1268 | } { } and the cursor is between the parentheses. The user can then | |
1269 | type some boolean expression within the parens. Having done that, | |
1270 | typing \\[cperl-linefeed] places you - appropriately indented - on a | |
1271 | new line between the braces (if you typed \\[cperl-linefeed] in a POD | |
5c8b7eaf | 1272 | directive line, then appropriate number of new lines is inserted). |
db133cb6 RS |
1273 | |
1274 | If CPerl decides that you want to insert \"English\" style construct like | |
1275 | ||
f83d2997 | 1276 | bite if angry; |
db133cb6 RS |
1277 | |
1278 | it will not do any expansion. See also help on variable | |
1279 | `cperl-extra-newline-before-brace'. (Note that one can switch the | |
1280 | help message on expansion by setting `cperl-message-electric-keyword' | |
1281 | to nil.) | |
f83d2997 KH |
1282 | |
1283 | \\[cperl-linefeed] is a convenience replacement for typing carriage | |
1284 | return. It places you in the next line with proper indentation, or if | |
1285 | you type it inside the inline block of control construct, like | |
db133cb6 | 1286 | |
f83d2997 | 1287 | foreach (@lines) {print; print} |
db133cb6 | 1288 | |
f83d2997 KH |
1289 | and you are on a boundary of a statement inside braces, it will |
1290 | transform the construct into a multiline and will place you into an | |
5c8b7eaf SS |
1291 | appropriately indented blank line. If you need a usual |
1292 | `newline-and-indent' behaviour, it is on \\[newline-and-indent], | |
f83d2997 KH |
1293 | see documentation on `cperl-electric-linefeed'. |
1294 | ||
db133cb6 RS |
1295 | Use \\[cperl-invert-if-unless] to change a construction of the form |
1296 | ||
1297 | if (A) { B } | |
1298 | ||
1299 | into | |
1300 | ||
1301 | B if A; | |
1302 | ||
f83d2997 KH |
1303 | \\{cperl-mode-map} |
1304 | ||
db133cb6 RS |
1305 | Setting the variable `cperl-font-lock' to t switches on font-lock-mode |
1306 | \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches | |
1307 | on electric space between $ and {, `cperl-electric-parens-string' is | |
1308 | the string that contains parentheses that should be electric in CPerl | |
1309 | \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'), | |
f83d2997 KH |
1310 | setting `cperl-electric-keywords' enables electric expansion of |
1311 | control structures in CPerl. `cperl-electric-linefeed' governs which | |
1312 | one of two linefeed behavior is preferable. You can enable all these | |
1313 | options simultaneously (recommended mode of use) by setting | |
1314 | `cperl-hairy' to t. In this case you can switch separate options off | |
db133cb6 RS |
1315 | by setting them to `null'. Note that one may undo the extra |
1316 | whitespace inserted by semis and braces in `auto-newline'-mode by | |
1317 | consequent \\[cperl-electric-backspace]. | |
f83d2997 KH |
1318 | |
1319 | If your site has perl5 documentation in info format, you can use commands | |
1320 | \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it. | |
1321 | These keys run commands `cperl-info-on-current-command' and | |
1322 | `cperl-info-on-command', which one is which is controlled by variable | |
5c8b7eaf | 1323 | `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings' |
db133cb6 | 1324 | \(in turn affected by `cperl-hairy'). |
f83d2997 KH |
1325 | |
1326 | Even if you have no info-format documentation, short one-liner-style | |
db133cb6 RS |
1327 | help is available on \\[cperl-get-help], and one can run perldoc or |
1328 | man via menu. | |
f83d2997 | 1329 | |
db133cb6 RS |
1330 | It is possible to show this help automatically after some idle time. |
1331 | This is regulated by variable `cperl-lazy-help-time'. Default with | |
1332 | `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5 | |
1333 | secs idle time . It is also possible to switch this on/off from the | |
1334 | menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'. | |
f83d2997 KH |
1335 | |
1336 | Use \\[cperl-lineup] to vertically lineup some construction - put the | |
1337 | beginning of the region at the start of construction, and make region | |
1338 | span the needed amount of lines. | |
1339 | ||
1340 | Variables `cperl-pod-here-scan', `cperl-pod-here-fontify', | |
1341 | `cperl-pod-face', `cperl-pod-head-face' control processing of pod and | |
db133cb6 RS |
1342 | here-docs sections. With capable Emaxen results of scan are used |
1343 | for indentation too, otherwise they are used for highlighting only. | |
f83d2997 KH |
1344 | |
1345 | Variables controlling indentation style: | |
1346 | `cperl-tab-always-indent' | |
1347 | Non-nil means TAB in CPerl mode should always reindent the current line, | |
1348 | regardless of where in the line point is when the TAB command is used. | |
db133cb6 RS |
1349 | `cperl-indent-left-aligned-comments' |
1350 | Non-nil means that the comment starting in leftmost column should indent. | |
f83d2997 KH |
1351 | `cperl-auto-newline' |
1352 | Non-nil means automatically newline before and after braces, | |
1353 | and after colons and semicolons, inserted in Perl code. The following | |
1354 | \\[cperl-electric-backspace] will remove the inserted whitespace. | |
5c8b7eaf SS |
1355 | Insertion after colons requires both this variable and |
1356 | `cperl-auto-newline-after-colon' set. | |
f83d2997 KH |
1357 | `cperl-auto-newline-after-colon' |
1358 | Non-nil means automatically newline even after colons. | |
1359 | Subject to `cperl-auto-newline' setting. | |
1360 | `cperl-indent-level' | |
1361 | Indentation of Perl statements within surrounding block. | |
1362 | The surrounding block's indentation is the indentation | |
1363 | of the line on which the open-brace appears. | |
1364 | `cperl-continued-statement-offset' | |
1365 | Extra indentation given to a substatement, such as the | |
1366 | then-clause of an if, or body of a while, or just a statement continuation. | |
1367 | `cperl-continued-brace-offset' | |
1368 | Extra indentation given to a brace that starts a substatement. | |
1369 | This is in addition to `cperl-continued-statement-offset'. | |
1370 | `cperl-brace-offset' | |
1371 | Extra indentation for line if it starts with an open brace. | |
1372 | `cperl-brace-imaginary-offset' | |
1373 | An open brace following other text is treated as if it the line started | |
1374 | this far to the right of the actual line indentation. | |
1375 | `cperl-label-offset' | |
1376 | Extra indentation for line that is a label. | |
1377 | `cperl-min-label-indent' | |
1378 | Minimal indentation for line that is a label. | |
1379 | ||
1380 | Settings for K&R and BSD indentation styles are | |
1381 | `cperl-indent-level' 5 8 | |
1382 | `cperl-continued-statement-offset' 5 8 | |
1383 | `cperl-brace-offset' -5 -8 | |
1384 | `cperl-label-offset' -5 -8 | |
1385 | ||
db133cb6 RS |
1386 | CPerl knows several indentation styles, and may bulk set the |
1387 | corresponding variables. Use \\[cperl-set-style] to do this. Use | |
1388 | \\[cperl-set-style-back] to restore the memorized preexisting values | |
1389 | \(both available from menu). | |
1390 | ||
1391 | If `cperl-indent-level' is 0, the statement after opening brace in | |
5c8b7eaf | 1392 | column 0 is indented on |
db133cb6 | 1393 | `cperl-brace-offset'+`cperl-continued-statement-offset'. |
f83d2997 KH |
1394 | |
1395 | Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook' | |
db133cb6 RS |
1396 | with no args. |
1397 | ||
1398 | DO NOT FORGET to read micro-docs (available from `Perl' menu) | |
1399 | or as help on variables `cperl-tips', `cperl-problems', | |
f94a632a | 1400 | `cperl-praise', `cperl-speed'." |
f83d2997 KH |
1401 | (interactive) |
1402 | (kill-all-local-variables) | |
f83d2997 KH |
1403 | (use-local-map cperl-mode-map) |
1404 | (if (cperl-val 'cperl-electric-linefeed) | |
1405 | (progn | |
1406 | (local-set-key "\C-J" 'cperl-linefeed) | |
1407 | (local-set-key "\C-C\C-J" 'newline-and-indent))) | |
db133cb6 RS |
1408 | (if (and |
1409 | (cperl-val 'cperl-clobber-lisp-bindings) | |
1410 | (cperl-val 'cperl-info-on-command-no-prompt)) | |
f83d2997 KH |
1411 | (progn |
1412 | ;; don't clobber the backspace binding: | |
1413 | (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f]) | |
1414 | (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command | |
1415 | [(control c) (control h) f]))) | |
029cb4d5 | 1416 | (setq major-mode 'cperl-mode) |
f83d2997 KH |
1417 | (setq mode-name "CPerl") |
1418 | (if (not cperl-mode-abbrev-table) | |
1419 | (let ((prev-a-c abbrevs-changed)) | |
1420 | (define-abbrev-table 'cperl-mode-abbrev-table '( | |
1421 | ("if" "if" cperl-electric-keyword 0) | |
1422 | ("elsif" "elsif" cperl-electric-keyword 0) | |
1423 | ("while" "while" cperl-electric-keyword 0) | |
1424 | ("until" "until" cperl-electric-keyword 0) | |
1425 | ("unless" "unless" cperl-electric-keyword 0) | |
1426 | ("else" "else" cperl-electric-else 0) | |
db133cb6 | 1427 | ("continue" "continue" cperl-electric-else 0) |
f83d2997 KH |
1428 | ("for" "for" cperl-electric-keyword 0) |
1429 | ("foreach" "foreach" cperl-electric-keyword 0) | |
db133cb6 RS |
1430 | ("formy" "formy" cperl-electric-keyword 0) |
1431 | ("foreachmy" "foreachmy" cperl-electric-keyword 0) | |
1432 | ("do" "do" cperl-electric-keyword 0) | |
6c389151 SM |
1433 | ("=pod" "=pod" cperl-electric-pod 0) |
1434 | ("=over" "=over" cperl-electric-pod 0) | |
1435 | ("=head1" "=head1" cperl-electric-pod 0) | |
1436 | ("=head2" "=head2" cperl-electric-pod 0) | |
db133cb6 RS |
1437 | ("pod" "pod" cperl-electric-pod 0) |
1438 | ("over" "over" cperl-electric-pod 0) | |
1439 | ("head1" "head1" cperl-electric-pod 0) | |
1440 | ("head2" "head2" cperl-electric-pod 0))) | |
f83d2997 KH |
1441 | (setq abbrevs-changed prev-a-c))) |
1442 | (setq local-abbrev-table cperl-mode-abbrev-table) | |
1443 | (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0)) | |
1444 | (set-syntax-table cperl-mode-syntax-table) | |
6c389151 SM |
1445 | (make-local-variable 'outline-regexp) |
1446 | ;; (setq outline-regexp imenu-example--function-name-regexp-perl) | |
1447 | (setq outline-regexp cperl-outline-regexp) | |
1448 | (make-local-variable 'outline-level) | |
1449 | (setq outline-level 'cperl-outline-level) | |
f83d2997 KH |
1450 | (make-local-variable 'paragraph-start) |
1451 | (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
1452 | (make-local-variable 'paragraph-separate) | |
1453 | (setq paragraph-separate paragraph-start) | |
1454 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
1455 | (setq paragraph-ignore-fill-prefix t) | |
1456 | (make-local-variable 'indent-line-function) | |
1457 | (setq indent-line-function 'cperl-indent-line) | |
1458 | (make-local-variable 'require-final-newline) | |
1459 | (setq require-final-newline t) | |
1460 | (make-local-variable 'comment-start) | |
1461 | (setq comment-start "# ") | |
1462 | (make-local-variable 'comment-end) | |
1463 | (setq comment-end "") | |
1464 | (make-local-variable 'comment-column) | |
1465 | (setq comment-column cperl-comment-column) | |
1466 | (make-local-variable 'comment-start-skip) | |
1467 | (setq comment-start-skip "#+ *") | |
1468 | (make-local-variable 'defun-prompt-regexp) | |
1469 | (setq defun-prompt-regexp "^[ \t]*sub[ \t]+\\([^ \t\n{(;]+\\)[ \t]*") | |
1470 | (make-local-variable 'comment-indent-function) | |
1471 | (setq comment-indent-function 'cperl-comment-indent) | |
1472 | (make-local-variable 'parse-sexp-ignore-comments) | |
1473 | (setq parse-sexp-ignore-comments t) | |
1474 | (make-local-variable 'indent-region-function) | |
1475 | (setq indent-region-function 'cperl-indent-region) | |
1476 | ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off! | |
1477 | (make-local-variable 'imenu-create-index-function) | |
1478 | (setq imenu-create-index-function | |
80585273 | 1479 | (function cperl-imenu--create-perl-index)) |
f83d2997 KH |
1480 | (make-local-variable 'imenu-sort-function) |
1481 | (setq imenu-sort-function nil) | |
1482 | (make-local-variable 'vc-header-alist) | |
db133cb6 | 1483 | (set 'vc-header-alist cperl-vc-header-alist) ; Avoid warning |
f83d2997 KH |
1484 | (make-local-variable 'font-lock-defaults) |
1485 | (setq font-lock-defaults | |
db133cb6 RS |
1486 | (cond |
1487 | ((string< emacs-version "19.30") | |
5efe6a56 | 1488 | '(cperl-font-lock-keywords-2)) |
db133cb6 | 1489 | ((string< emacs-version "19.33") ; Which one to use? |
5efe6a56 SM |
1490 | '((cperl-font-lock-keywords |
1491 | cperl-font-lock-keywords-1 | |
1492 | cperl-font-lock-keywords-2))) | |
db133cb6 RS |
1493 | (t |
1494 | '((cperl-load-font-lock-keywords | |
1495 | cperl-load-font-lock-keywords-1 | |
1496 | cperl-load-font-lock-keywords-2))))) | |
1497 | (make-local-variable 'cperl-syntax-state) | |
f83d2997 KH |
1498 | (if cperl-use-syntax-table-text-property |
1499 | (progn | |
029cb4d5 | 1500 | (make-local-variable 'parse-sexp-lookup-properties) |
f83d2997 | 1501 | ;; Do not introduce variable if not needed, we check it! |
db133cb6 RS |
1502 | (set 'parse-sexp-lookup-properties t) |
1503 | ;; Fix broken font-lock: | |
1504 | (or (boundp 'font-lock-unfontify-region-function) | |
1505 | (set 'font-lock-unfontify-region-function | |
5bd52f0e | 1506 | 'font-lock-default-unfontify-region)) |
029cb4d5 | 1507 | (make-local-variable 'font-lock-unfontify-region-function) |
5c8b7eaf | 1508 | (set 'font-lock-unfontify-region-function |
db133cb6 | 1509 | 'cperl-font-lock-unfontify-region-function) |
029cb4d5 | 1510 | (make-local-variable 'cperl-syntax-done-to) |
db133cb6 RS |
1511 | ;; Another bug: unless font-lock-syntactic-keywords, font-lock |
1512 | ;; ignores syntax-table text-property. (t) is a hack | |
1513 | ;; to make font-lock think that font-lock-syntactic-keywords | |
1514 | ;; are defined | |
029cb4d5 | 1515 | (make-local-variable 'font-lock-syntactic-keywords) |
5c8b7eaf | 1516 | (setq font-lock-syntactic-keywords |
db133cb6 RS |
1517 | (if cperl-syntaxify-by-font-lock |
1518 | '(t (cperl-fontify-syntaxically)) | |
1519 | '(t))))) | |
1520 | (make-local-variable 'cperl-old-style) | |
80585273 | 1521 | (set (make-local-variable 'normal-auto-fill-function) |
072cb6f9 | 1522 | #'cperl-do-auto-fill) |
f83d2997 | 1523 | (if (cperl-enable-font-lock) |
5c8b7eaf | 1524 | (if (cperl-val 'cperl-font-lock) |
f83d2997 KH |
1525 | (progn (or cperl-faces-init (cperl-init-faces)) |
1526 | (font-lock-mode 1)))) | |
1527 | (and (boundp 'msb-menu-cond) | |
1528 | (not cperl-msb-fixed) | |
1529 | (cperl-msb-fix)) | |
1530 | (if (featurep 'easymenu) | |
46c72468 | 1531 | (easy-menu-add cperl-menu)) ; A NOP in Emacs. |
f83d2997 KH |
1532 | (run-hooks 'cperl-mode-hook) |
1533 | ;; After hooks since fontification will break this | |
5c8b7eaf | 1534 | (if cperl-pod-here-scan |
5bd52f0e RS |
1535 | (or ;;(and (boundp 'font-lock-mode) |
1536 | ;; (eval 'font-lock-mode) ; Avoid warning | |
1537 | ;; (boundp 'font-lock-hot-pass) ; Newer font-lock | |
1538 | cperl-syntaxify-by-font-lock ;;) | |
1539 | (progn (or cperl-faces-init (cperl-init-faces-weak)) | |
1540 | (cperl-find-pods-heres))))) | |
f83d2997 KH |
1541 | \f |
1542 | ;; Fix for perldb - make default reasonable | |
1543 | (defun cperl-db () | |
1544 | (interactive) | |
1545 | (require 'gud) | |
1546 | (perldb (read-from-minibuffer "Run perldb (like this): " | |
1547 | (if (consp gud-perldb-history) | |
1548 | (car gud-perldb-history) | |
1549 | (concat "perl " ;;(file-name-nondirectory | |
1550 | ;; I have problems | |
1551 | ;; in OS/2 | |
1552 | ;; otherwise | |
1553 | (buffer-file-name))) | |
1554 | nil nil | |
1555 | '(gud-perldb-history . 1)))) | |
1556 | \f | |
f83d2997 KH |
1557 | (defun cperl-msb-fix () |
1558 | ;; Adds perl files to msb menu, supposes that msb is already loaded | |
1559 | (setq cperl-msb-fixed t) | |
1560 | (let* ((l (length msb-menu-cond)) | |
1561 | (last (nth (1- l) msb-menu-cond)) | |
1562 | (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last | |
1563 | (handle (1- (nth 1 last)))) | |
1564 | (setcdr precdr (list | |
1565 | (list | |
996e2616 | 1566 | '(memq major-mode '(cperl-mode perl-mode)) |
f83d2997 KH |
1567 | handle |
1568 | "Perl Files (%d)") | |
1569 | last)))) | |
1570 | \f | |
1571 | ;; This is used by indent-for-comment | |
1572 | ;; to decide how much to indent a comment in CPerl code | |
1573 | ;; based on its context. Do fallback if comment is found wrong. | |
1574 | ||
1575 | (defvar cperl-wrong-comment) | |
5bd52f0e RS |
1576 | (defvar cperl-st-cfence '(14)) ; Comment-fence |
1577 | (defvar cperl-st-sfence '(15)) ; String-fence | |
1578 | (defvar cperl-st-punct '(1)) | |
1579 | (defvar cperl-st-word '(2)) | |
1580 | (defvar cperl-st-bra '(4 . ?\>)) | |
1581 | (defvar cperl-st-ket '(5 . ?\<)) | |
1582 | ||
f83d2997 KH |
1583 | |
1584 | (defun cperl-comment-indent () | |
5bd52f0e | 1585 | (let ((p (point)) (c (current-column)) was phony) |
f83d2997 KH |
1586 | (if (looking-at "^#") 0 ; Existing comment at bol stays there. |
1587 | ;; Wrong comment found | |
1588 | (save-excursion | |
5bd52f0e RS |
1589 | (setq was (cperl-to-comment-or-eol) |
1590 | phony (eq (get-text-property (point) 'syntax-table) | |
1591 | cperl-st-cfence)) | |
1592 | (if phony | |
1593 | (progn | |
1594 | (re-search-forward "#\\|$") ; Hmm, what about embedded #? | |
1595 | (if (eq (preceding-char) ?\#) | |
1596 | (forward-char -1)) | |
1597 | (setq was nil))) | |
f83d2997 KH |
1598 | (if (= (point) p) |
1599 | (progn | |
1600 | (skip-chars-backward " \t") | |
1601 | (max (1+ (current-column)) ; Else indent at comment column | |
1602 | comment-column)) | |
1603 | (if was nil | |
1604 | (insert comment-start) | |
1605 | (backward-char (length comment-start))) | |
1606 | (setq cperl-wrong-comment t) | |
1607 | (indent-to comment-column 1) ; Indent minimum 1 | |
1608 | c))))) ; except leave at least one space. | |
1609 | ||
1610 | ;;;(defun cperl-comment-indent-fallback () | |
1611 | ;;; "Is called if the standard comment-search procedure fails. | |
1612 | ;;;Point is at start of real comment." | |
1613 | ;;; (let ((c (current-column)) target cnt prevc) | |
1614 | ;;; (if (= c comment-column) nil | |
1615 | ;;; (setq cnt (skip-chars-backward "[ \t]")) | |
5c8b7eaf | 1616 | ;;; (setq target (max (1+ (setq prevc |
f83d2997 KH |
1617 | ;;; (current-column))) ; Else indent at comment column |
1618 | ;;; comment-column)) | |
1619 | ;;; (if (= c comment-column) nil | |
1620 | ;;; (delete-backward-char cnt) | |
1621 | ;;; (while (< prevc target) | |
1622 | ;;; (insert "\t") | |
1623 | ;;; (setq prevc (current-column))) | |
1624 | ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column)))) | |
1625 | ;;; (while (< prevc target) | |
1626 | ;;; (insert " ") | |
1627 | ;;; (setq prevc (current-column))))))) | |
1628 | ||
1629 | (defun cperl-indent-for-comment () | |
1630 | "Substitute for `indent-for-comment' in CPerl." | |
1631 | (interactive) | |
1632 | (let (cperl-wrong-comment) | |
1633 | (indent-for-comment) | |
1634 | (if cperl-wrong-comment | |
1635 | (progn (cperl-to-comment-or-eol) | |
1636 | (forward-char (length comment-start)))))) | |
1637 | ||
1638 | (defun cperl-comment-region (b e arg) | |
1639 | "Comment or uncomment each line in the region in CPerl mode. | |
1640 | See `comment-region'." | |
1641 | (interactive "r\np") | |
1642 | (let ((comment-start "#")) | |
1643 | (comment-region b e arg))) | |
1644 | ||
1645 | (defun cperl-uncomment-region (b e arg) | |
1646 | "Uncomment or comment each line in the region in CPerl mode. | |
1647 | See `comment-region'." | |
1648 | (interactive "r\np") | |
1649 | (let ((comment-start "#")) | |
1650 | (comment-region b e (- arg)))) | |
1651 | ||
1652 | (defvar cperl-brace-recursing nil) | |
1653 | ||
1654 | (defun cperl-electric-brace (arg &optional only-before) | |
1655 | "Insert character and correct line's indentation. | |
1656 | If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the | |
1657 | place (even in empty line), but not after. If after \")\" and the inserted | |
5c8b7eaf | 1658 | char is \"{\", insert extra newline before only if |
f83d2997 KH |
1659 | `cperl-extra-newline-before-brace'." |
1660 | (interactive "P") | |
1661 | (let (insertpos | |
1662 | (other-end (if (and cperl-electric-parens-mark | |
5c8b7eaf | 1663 | (cperl-mark-active) |
f83d2997 | 1664 | (< (mark) (point))) |
5c8b7eaf | 1665 | (mark) |
f83d2997 KH |
1666 | nil))) |
1667 | (if (and other-end | |
1668 | (not cperl-brace-recursing) | |
1669 | (cperl-val 'cperl-electric-parens) | |
1670 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))) | |
1671 | ;; Need to insert a matching pair | |
1672 | (progn | |
1673 | (save-excursion | |
1674 | (setq insertpos (point-marker)) | |
1675 | (goto-char other-end) | |
1676 | (setq last-command-char ?\{) | |
1677 | (cperl-electric-lbrace arg insertpos)) | |
1678 | (forward-char 1)) | |
db133cb6 RS |
1679 | ;: Check whether we close something "usual" with `}' |
1680 | (if (and (eq last-command-char ?\}) | |
5c8b7eaf | 1681 | (not |
db133cb6 RS |
1682 | (condition-case nil |
1683 | (save-excursion | |
1684 | (up-list (- (prefix-numeric-value arg))) | |
1685 | ;;(cperl-after-block-p (point-min)) | |
1686 | (cperl-after-expr-p nil "{;)")) | |
1687 | (error nil)))) | |
1688 | ;; Just insert the guy | |
1689 | (self-insert-command (prefix-numeric-value arg)) | |
1690 | (if (and (not arg) ; No args, end (of empty line or auto) | |
1691 | (eolp) | |
1692 | (or (and (null only-before) | |
1693 | (save-excursion | |
1694 | (skip-chars-backward " \t") | |
1695 | (bolp))) | |
1696 | (and (eq last-command-char ?\{) ; Do not insert newline | |
1697 | ;; if after ")" and `cperl-extra-newline-before-brace' | |
1698 | ;; is nil, do not insert extra newline. | |
1699 | (not cperl-extra-newline-before-brace) | |
1700 | (save-excursion | |
1701 | (skip-chars-backward " \t") | |
1702 | (eq (preceding-char) ?\)))) | |
5c8b7eaf | 1703 | (if cperl-auto-newline |
db133cb6 RS |
1704 | (progn (cperl-indent-line) (newline) t) nil))) |
1705 | (progn | |
1706 | (self-insert-command (prefix-numeric-value arg)) | |
1707 | (cperl-indent-line) | |
1708 | (if cperl-auto-newline | |
1709 | (setq insertpos (1- (point)))) | |
1710 | (if (and cperl-auto-newline (null only-before)) | |
1711 | (progn | |
1712 | (newline) | |
1713 | (cperl-indent-line))) | |
1714 | (save-excursion | |
1715 | (if insertpos (progn (goto-char insertpos) | |
5c8b7eaf | 1716 | (search-forward (make-string |
db133cb6 RS |
1717 | 1 last-command-char)) |
1718 | (setq insertpos (1- (point))))) | |
1719 | (delete-char -1)))) | |
1720 | (if insertpos | |
f83d2997 | 1721 | (save-excursion |
db133cb6 RS |
1722 | (goto-char insertpos) |
1723 | (self-insert-command (prefix-numeric-value arg))) | |
1724 | (self-insert-command (prefix-numeric-value arg))))))) | |
f83d2997 KH |
1725 | |
1726 | (defun cperl-electric-lbrace (arg &optional end) | |
1727 | "Insert character, correct line's indentation, correct quoting by space." | |
1728 | (interactive "P") | |
5c8b7eaf | 1729 | (let (pos after |
f83d2997 KH |
1730 | (cperl-brace-recursing t) |
1731 | (cperl-auto-newline cperl-auto-newline) | |
1732 | (other-end (or end | |
1733 | (if (and cperl-electric-parens-mark | |
1734 | (cperl-mark-active) | |
1735 | (> (mark) (point))) | |
1736 | (save-excursion | |
1737 | (goto-char (mark)) | |
5c8b7eaf | 1738 | (point-marker)) |
f83d2997 KH |
1739 | nil)))) |
1740 | (and (cperl-val 'cperl-electric-lbrace-space) | |
1741 | (eq (preceding-char) ?$) | |
1742 | (save-excursion | |
1743 | (skip-chars-backward "$") | |
1744 | (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)")) | |
1745 | (insert ?\ )) | |
bab27c0c | 1746 | ;; Check whether we are in comment |
5c8b7eaf | 1747 | (if (and |
bab27c0c RS |
1748 | (save-excursion |
1749 | (beginning-of-line) | |
1750 | (not (looking-at "[ \t]*#"))) | |
1751 | (cperl-after-expr-p nil "{;)")) | |
1752 | nil | |
1753 | (setq cperl-auto-newline nil)) | |
f83d2997 KH |
1754 | (cperl-electric-brace arg) |
1755 | (and (cperl-val 'cperl-electric-parens) | |
1756 | (eq last-command-char ?{) | |
5c8b7eaf | 1757 | (memq last-command-char |
f83d2997 KH |
1758 | (append cperl-electric-parens-string nil)) |
1759 | (or (if other-end (goto-char (marker-position other-end))) | |
1760 | t) | |
1761 | (setq last-command-char ?} pos (point)) | |
1762 | (progn (cperl-electric-brace arg t) | |
1763 | (goto-char pos))))) | |
1764 | ||
1765 | (defun cperl-electric-paren (arg) | |
1766 | "Insert a matching pair of parentheses." | |
1767 | (interactive "P") | |
1768 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
1769 | (other-end (if (and cperl-electric-parens-mark | |
5c8b7eaf | 1770 | (cperl-mark-active) |
f83d2997 KH |
1771 | (> (mark) (point))) |
1772 | (save-excursion | |
1773 | (goto-char (mark)) | |
5c8b7eaf | 1774 | (point-marker)) |
f83d2997 KH |
1775 | nil))) |
1776 | (if (and (cperl-val 'cperl-electric-parens) | |
1777 | (memq last-command-char | |
1778 | (append cperl-electric-parens-string nil)) | |
1779 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)) | |
1780 | ;;(not (save-excursion (search-backward "#" beg t))) | |
1781 | (if (eq last-command-char ?<) | |
1782 | (progn | |
1783 | (and abbrev-mode ; later it is too late, may be after `for' | |
1784 | (expand-abbrev)) | |
1785 | (cperl-after-expr-p nil "{;(,:=")) | |
1786 | 1)) | |
1787 | (progn | |
1788 | (self-insert-command (prefix-numeric-value arg)) | |
1789 | (if other-end (goto-char (marker-position other-end))) | |
5c8b7eaf | 1790 | (insert (make-string |
f83d2997 KH |
1791 | (prefix-numeric-value arg) |
1792 | (cdr (assoc last-command-char '((?{ .?}) | |
1793 | (?[ . ?]) | |
1794 | (?( . ?)) | |
1795 | (?< . ?>)))))) | |
1796 | (forward-char (- (prefix-numeric-value arg)))) | |
1797 | (self-insert-command (prefix-numeric-value arg))))) | |
1798 | ||
1799 | (defun cperl-electric-rparen (arg) | |
1800 | "Insert a matching pair of parentheses if marking is active. | |
1801 | If not, or if we are not at the end of marking range, would self-insert." | |
1802 | (interactive "P") | |
1803 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
1804 | (other-end (if (and cperl-electric-parens-mark | |
1805 | (cperl-val 'cperl-electric-parens) | |
1806 | (memq last-command-char | |
1807 | (append cperl-electric-parens-string nil)) | |
5c8b7eaf | 1808 | (cperl-mark-active) |
f83d2997 | 1809 | (< (mark) (point))) |
5c8b7eaf | 1810 | (mark) |
f83d2997 KH |
1811 | nil)) |
1812 | p) | |
1813 | (if (and other-end | |
1814 | (cperl-val 'cperl-electric-parens) | |
1815 | (memq last-command-char '( ?\) ?\] ?\} ?\> )) | |
1816 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)) | |
1817 | ;;(not (save-excursion (search-backward "#" beg t))) | |
1818 | ) | |
1819 | (progn | |
1820 | (self-insert-command (prefix-numeric-value arg)) | |
1821 | (setq p (point)) | |
1822 | (if other-end (goto-char other-end)) | |
1823 | (insert (make-string | |
1824 | (prefix-numeric-value arg) | |
1825 | (cdr (assoc last-command-char '((?\} . ?\{) | |
1826 | (?\] . ?\[) | |
1827 | (?\) . ?\() | |
1828 | (?\> . ?\<)))))) | |
1829 | (goto-char (1+ p))) | |
1830 | (self-insert-command (prefix-numeric-value arg))))) | |
1831 | ||
1832 | (defun cperl-electric-keyword () | |
db133cb6 RS |
1833 | "Insert a construction appropriate after a keyword. |
1834 | Help message may be switched off by setting `cperl-message-electric-keyword' | |
1835 | to nil." | |
5c8b7eaf | 1836 | (let ((beg (save-excursion (beginning-of-line) (point))) |
f83d2997 KH |
1837 | (dollar (and (eq last-command-char ?$) |
1838 | (eq this-command 'self-insert-command))) | |
1839 | (delete (and (memq last-command-char '(?\ ?\n ?\t ?\f)) | |
db133cb6 RS |
1840 | (memq this-command '(self-insert-command newline)))) |
1841 | my do) | |
f83d2997 | 1842 | (and (save-excursion |
db133cb6 RS |
1843 | (condition-case nil |
1844 | (progn | |
1845 | (backward-sexp 1) | |
1846 | (setq do (looking-at "do\\>"))) | |
1847 | (error nil)) | |
f83d2997 | 1848 | (cperl-after-expr-p nil "{;:")) |
5c8b7eaf SS |
1849 | (save-excursion |
1850 | (not | |
f83d2997 | 1851 | (re-search-backward |
5bd52f0e | 1852 | "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>" |
f83d2997 KH |
1853 | beg t))) |
1854 | (save-excursion (or (not (re-search-backward "^=" nil t)) | |
db133cb6 RS |
1855 | (or |
1856 | (looking-at "=cut") | |
1857 | (and cperl-use-syntax-table-text-property | |
1858 | (not (eq (get-text-property (point) | |
1859 | 'syntax-type) | |
1860 | 'pod)))))) | |
f83d2997 | 1861 | (progn |
db133cb6 RS |
1862 | (and (eq (preceding-char) ?y) |
1863 | (progn ; "foreachmy" | |
1864 | (forward-char -2) | |
1865 | (insert " ") | |
1866 | (forward-char 2) | |
5c8b7eaf SS |
1867 | (setq my t dollar t |
1868 | delete | |
db133cb6 | 1869 | (memq this-command '(self-insert-command newline))))) |
f83d2997 KH |
1870 | (and dollar (insert " $")) |
1871 | (cperl-indent-line) | |
1872 | ;;(insert " () {\n}") | |
1873 | (cond | |
1874 | (cperl-extra-newline-before-brace | |
db133cb6 | 1875 | (insert (if do "\n" " ()\n")) |
f83d2997 KH |
1876 | (insert "{") |
1877 | (cperl-indent-line) | |
1878 | (insert "\n") | |
1879 | (cperl-indent-line) | |
db133cb6 RS |
1880 | (insert "\n}") |
1881 | (and do (insert " while ();"))) | |
f83d2997 | 1882 | (t |
db133cb6 | 1883 | (insert (if do " {\n} while ();" " () {\n}"))) |
f83d2997 KH |
1884 | ) |
1885 | (or (looking-at "[ \t]\\|$") (insert " ")) | |
1886 | (cperl-indent-line) | |
1887 | (if dollar (progn (search-backward "$") | |
5c8b7eaf | 1888 | (if my |
db133cb6 RS |
1889 | (forward-char 1) |
1890 | (delete-char 1))) | |
f83d2997 KH |
1891 | (search-backward ")")) |
1892 | (if delete | |
db133cb6 RS |
1893 | (cperl-putback-char cperl-del-back-ch)) |
1894 | (if cperl-message-electric-keyword | |
1895 | (message "Precede char by C-q to avoid expansion")))))) | |
1896 | ||
1897 | (defun cperl-ensure-newlines (n &optional pos) | |
1898 | "Make sure there are N newlines after the point." | |
1899 | (or pos (setq pos (point))) | |
1900 | (if (looking-at "\n") | |
1901 | (forward-char 1) | |
1902 | (insert "\n")) | |
1903 | (if (> n 1) | |
1904 | (cperl-ensure-newlines (1- n) pos) | |
1905 | (goto-char pos))) | |
1906 | ||
1907 | (defun cperl-electric-pod () | |
1908 | "Insert a POD chunk appropriate after a =POD directive." | |
1909 | (let ((delete (and (memq last-command-char '(?\ ?\n ?\t ?\f)) | |
1910 | (memq this-command '(self-insert-command newline)))) | |
1911 | head1 notlast name p really-delete over) | |
1912 | (and (save-excursion | |
6c389151 SM |
1913 | (forward-word -1) |
1914 | (and | |
db133cb6 RS |
1915 | (eq (preceding-char) ?=) |
1916 | (progn | |
6c389151 SM |
1917 | (setq head1 (looking-at "head1\\>[ \t]*$")) |
1918 | (setq over (and (looking-at "over\\>[ \t]*$") | |
1919 | (not (looking-at "over[ \t]*\n\n\n*=item\\>")))) | |
db133cb6 RS |
1920 | (forward-char -1) |
1921 | (bolp)) | |
5c8b7eaf | 1922 | (or |
5bd52f0e | 1923 | (get-text-property (point) 'in-pod) |
db133cb6 RS |
1924 | (cperl-after-expr-p nil "{;:") |
1925 | (and (re-search-backward | |
6c389151 SM |
1926 | ;; "\\(\\`\n?\\|\n\n\\)=\\sw+" |
1927 | "\\(\\`\n?\\|^\n\\)=\\sw+" | |
1928 | (point-min) t) | |
db133cb6 RS |
1929 | (not (or |
1930 | (looking-at "=cut") | |
1931 | (and cperl-use-syntax-table-text-property | |
1932 | (not (eq (get-text-property (point) 'syntax-type) | |
1933 | 'pod))))))))) | |
1934 | (progn | |
1935 | (save-excursion | |
6c389151 | 1936 | (setq notlast (re-search-forward "^\n=" nil t))) |
db133cb6 RS |
1937 | (or notlast |
1938 | (progn | |
1939 | (insert "\n\n=cut") | |
1940 | (cperl-ensure-newlines 2) | |
6c389151 SM |
1941 | (forward-word -2) |
1942 | (if (and head1 | |
1943 | (not | |
db133cb6 RS |
1944 | (save-excursion |
1945 | (forward-char -1) | |
1946 | (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>" | |
1947 | nil t)))) ; Only one | |
6c389151 SM |
1948 | (progn |
1949 | (forward-word 1) | |
db133cb6 RS |
1950 | (setq name (file-name-sans-extension |
1951 | (file-name-nondirectory (buffer-file-name))) | |
1952 | p (point)) | |
5c8b7eaf | 1953 | (insert " NAME\n\n" name |
029cb4d5 | 1954 | " - \n\n=head1 SYNOPSIS\n\n\n\n" |
db133cb6 RS |
1955 | "=head1 DESCRIPTION") |
1956 | (cperl-ensure-newlines 4) | |
1957 | (goto-char p) | |
6c389151 | 1958 | (forward-word 2) |
db133cb6 RS |
1959 | (end-of-line) |
1960 | (setq really-delete t)) | |
6c389151 | 1961 | (forward-word 1)))) |
db133cb6 RS |
1962 | (if over |
1963 | (progn | |
1964 | (setq p (point)) | |
1965 | (insert "\n\n=item \n\n\n\n" | |
1966 | "=back") | |
1967 | (cperl-ensure-newlines 2) | |
1968 | (goto-char p) | |
6c389151 | 1969 | (forward-word 1) |
db133cb6 RS |
1970 | (end-of-line) |
1971 | (setq really-delete t))) | |
1972 | (if (and delete really-delete) | |
f83d2997 KH |
1973 | (cperl-putback-char cperl-del-back-ch)))))) |
1974 | ||
1975 | (defun cperl-electric-else () | |
db133cb6 RS |
1976 | "Insert a construction appropriate after a keyword. |
1977 | Help message may be switched off by setting `cperl-message-electric-keyword' | |
1978 | to nil." | |
f83d2997 KH |
1979 | (let ((beg (save-excursion (beginning-of-line) (point)))) |
1980 | (and (save-excursion | |
1981 | (backward-sexp 1) | |
1982 | (cperl-after-expr-p nil "{;:")) | |
5c8b7eaf SS |
1983 | (save-excursion |
1984 | (not | |
f83d2997 | 1985 | (re-search-backward |
5bd52f0e | 1986 | "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>" |
f83d2997 KH |
1987 | beg t))) |
1988 | (save-excursion (or (not (re-search-backward "^=" nil t)) | |
db133cb6 RS |
1989 | (looking-at "=cut") |
1990 | (and cperl-use-syntax-table-text-property | |
1991 | (not (eq (get-text-property (point) | |
1992 | 'syntax-type) | |
1993 | 'pod))))) | |
f83d2997 KH |
1994 | (progn |
1995 | (cperl-indent-line) | |
1996 | ;;(insert " {\n\n}") | |
1997 | (cond | |
1998 | (cperl-extra-newline-before-brace | |
1999 | (insert "\n") | |
2000 | (insert "{") | |
2001 | (cperl-indent-line) | |
2002 | (insert "\n\n}")) | |
2003 | (t | |
2004 | (insert " {\n\n}")) | |
2005 | ) | |
2006 | (or (looking-at "[ \t]\\|$") (insert " ")) | |
2007 | (cperl-indent-line) | |
2008 | (forward-line -1) | |
2009 | (cperl-indent-line) | |
db133cb6 RS |
2010 | (cperl-putback-char cperl-del-back-ch) |
2011 | (setq this-command 'cperl-electric-else) | |
2012 | (if cperl-message-electric-keyword | |
2013 | (message "Precede char by C-q to avoid expansion")))))) | |
f83d2997 KH |
2014 | |
2015 | (defun cperl-linefeed () | |
db133cb6 RS |
2016 | "Go to end of line, open a new line and indent appropriately. |
2017 | If in POD, insert appropriate lines." | |
f83d2997 KH |
2018 | (interactive) |
2019 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
2020 | (end (save-excursion (end-of-line) (point))) | |
db133cb6 | 2021 | (pos (point)) start over cut res) |
f83d2997 | 2022 | (if (and ; Check if we need to split: |
5c8b7eaf | 2023 | ; i.e., on a boundary and inside "{...}" |
f83d2997 KH |
2024 | (save-excursion (cperl-to-comment-or-eol) |
2025 | (>= (point) pos)) ; Not in a comment | |
2026 | (or (save-excursion | |
2027 | (skip-chars-backward " \t" beg) | |
2028 | (forward-char -1) | |
2029 | (looking-at "[;{]")) ; After { or ; + spaces | |
2030 | (looking-at "[ \t]*}") ; Before } | |
2031 | (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ; | |
2032 | (save-excursion | |
2033 | (and | |
5c8b7eaf | 2034 | (eq (car (parse-partial-sexp pos end -1)) -1) |
f83d2997 KH |
2035 | ; Leave the level of parens |
2036 | (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr | |
2037 | ; Are at end | |
6c389151 | 2038 | (cperl-after-block-p (point-min)) |
f83d2997 KH |
2039 | (progn |
2040 | (backward-sexp 1) | |
2041 | (setq start (point-marker)) | |
db133cb6 | 2042 | (<= start pos))))) ; Redundant? Are after the |
f83d2997 KH |
2043 | ; start of parens group. |
2044 | (progn | |
2045 | (skip-chars-backward " \t") | |
2046 | (or (memq (preceding-char) (append ";{" nil)) | |
2047 | (insert ";")) | |
2048 | (insert "\n") | |
2049 | (forward-line -1) | |
2050 | (cperl-indent-line) | |
2051 | (goto-char start) | |
2052 | (or (looking-at "{[ \t]*$") ; If there is a statement | |
2053 | ; before, move it to separate line | |
2054 | (progn | |
2055 | (forward-char 1) | |
2056 | (insert "\n") | |
2057 | (cperl-indent-line))) | |
2058 | (forward-line 1) ; We are on the target line | |
2059 | (cperl-indent-line) | |
2060 | (beginning-of-line) | |
2061 | (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement | |
2062 | ; after, move it to separate line | |
2063 | (progn | |
2064 | (end-of-line) | |
2065 | (search-backward "}" beg) | |
2066 | (skip-chars-backward " \t") | |
2067 | (or (memq (preceding-char) (append ";{" nil)) | |
2068 | (insert ";")) | |
2069 | (insert "\n") | |
2070 | (cperl-indent-line) | |
2071 | (forward-line -1))) | |
5c8b7eaf | 2072 | (forward-line -1) ; We are on the line before target |
f83d2997 KH |
2073 | (end-of-line) |
2074 | (newline-and-indent)) | |
db133cb6 | 2075 | (end-of-line) ; else - no splitting |
f83d2997 KH |
2076 | (cond |
2077 | ((and (looking-at "\n[ \t]*{$") | |
2078 | (save-excursion | |
2079 | (skip-chars-backward " \t") | |
2080 | (eq (preceding-char) ?\)))) ; Probably if () {} group | |
2081 | ; with an extra newline. | |
2082 | (forward-line 2) | |
2083 | (cperl-indent-line)) | |
db133cb6 RS |
2084 | ((save-excursion ; In POD header |
2085 | (forward-paragraph -1) | |
2086 | ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b") | |
2087 | ;; We are after \n now, so look for the rest | |
2088 | (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+") | |
5c8b7eaf | 2089 | (progn |
db133cb6 RS |
2090 | (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>")) |
2091 | (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>")) | |
2092 | t))) | |
2093 | (if (and over | |
2094 | (progn | |
2095 | (forward-paragraph -1) | |
2096 | (forward-word 1) | |
2097 | (setq pos (point)) | |
2098 | (setq cut (buffer-substring (point) | |
2099 | (save-excursion | |
2100 | (end-of-line) | |
2101 | (point)))) | |
2102 | (delete-char (- (save-excursion (end-of-line) (point)) | |
2103 | (point))) | |
2104 | (setq res (expand-abbrev)) | |
2105 | (save-excursion | |
2106 | (goto-char pos) | |
2107 | (insert cut)) | |
2108 | res)) | |
2109 | nil | |
2110 | (cperl-ensure-newlines (if cut 2 4)) | |
2111 | (forward-line 2))) | |
2112 | ((get-text-property (point) 'in-pod) ; In POD section | |
2113 | (cperl-ensure-newlines 4) | |
2114 | (forward-line 2)) | |
f83d2997 KH |
2115 | ((looking-at "\n[ \t]*$") ; Next line is empty - use it. |
2116 | (forward-line 1) | |
2117 | (cperl-indent-line)) | |
2118 | (t | |
2119 | (newline-and-indent)))))) | |
2120 | ||
2121 | (defun cperl-electric-semi (arg) | |
2122 | "Insert character and correct line's indentation." | |
2123 | (interactive "P") | |
2124 | (if cperl-auto-newline | |
2125 | (cperl-electric-terminator arg) | |
6c389151 SM |
2126 | (self-insert-command (prefix-numeric-value arg)) |
2127 | (if cperl-autoindent-on-semi | |
2128 | (cperl-indent-line)))) | |
f83d2997 KH |
2129 | |
2130 | (defun cperl-electric-terminator (arg) | |
2131 | "Insert character and correct line's indentation." | |
2132 | (interactive "P") | |
5c8b7eaf | 2133 | (let (insertpos (end (point)) |
f83d2997 KH |
2134 | (auto (and cperl-auto-newline |
2135 | (or (not (eq last-command-char ?:)) | |
2136 | cperl-auto-newline-after-colon)))) | |
5c8b7eaf | 2137 | (if (and ;;(not arg) |
f83d2997 KH |
2138 | (eolp) |
2139 | (not (save-excursion | |
2140 | (beginning-of-line) | |
2141 | (skip-chars-forward " \t") | |
2142 | (or | |
2143 | ;; Ignore in comment lines | |
2144 | (= (following-char) ?#) | |
2145 | ;; Colon is special only after a label | |
2146 | ;; So quickly rule out most other uses of colon | |
2147 | ;; and do no indentation for them. | |
2148 | (and (eq last-command-char ?:) | |
2149 | (save-excursion | |
2150 | (forward-word 1) | |
2151 | (skip-chars-forward " \t") | |
2152 | (and (< (point) end) | |
2153 | (progn (goto-char (- end 1)) | |
2154 | (not (looking-at ":")))))) | |
2155 | (progn | |
2156 | (beginning-of-defun) | |
2157 | (let ((pps (parse-partial-sexp (point) end))) | |
2158 | (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
2159 | (progn | |
2160 | (self-insert-command (prefix-numeric-value arg)) | |
2161 | ;;(forward-char -1) | |
2162 | (if auto (setq insertpos (point-marker))) | |
2163 | ;;(forward-char 1) | |
2164 | (cperl-indent-line) | |
2165 | (if auto | |
2166 | (progn | |
2167 | (newline) | |
2168 | (cperl-indent-line))) | |
f83d2997 KH |
2169 | (save-excursion |
2170 | (if insertpos (goto-char (1- (marker-position insertpos))) | |
2171 | (forward-char -1)) | |
2172 | (delete-char 1)))) | |
2173 | (if insertpos | |
2174 | (save-excursion | |
2175 | (goto-char insertpos) | |
2176 | (self-insert-command (prefix-numeric-value arg))) | |
2177 | (self-insert-command (prefix-numeric-value arg))))) | |
2178 | ||
2179 | (defun cperl-electric-backspace (arg) | |
5c8b7eaf | 2180 | "Backspace-untabify, or remove the whitespace around the point inserted |
db133cb6 | 2181 | by an electric key." |
f83d2997 | 2182 | (interactive "p") |
5c8b7eaf SS |
2183 | (if (and cperl-auto-newline |
2184 | (memq last-command '(cperl-electric-semi | |
f83d2997 KH |
2185 | cperl-electric-terminator |
2186 | cperl-electric-lbrace)) | |
2187 | (memq (preceding-char) '(?\ ?\t ?\n))) | |
2188 | (let (p) | |
5c8b7eaf | 2189 | (if (eq last-command 'cperl-electric-lbrace) |
f83d2997 KH |
2190 | (skip-chars-forward " \t\n")) |
2191 | (setq p (point)) | |
2192 | (skip-chars-backward " \t\n") | |
2193 | (delete-region (point) p)) | |
db133cb6 RS |
2194 | (and (eq last-command 'cperl-electric-else) |
2195 | ;; We are removing the whitespace *inside* cperl-electric-else | |
2196 | (setq this-command 'cperl-electric-else-really)) | |
5c8b7eaf | 2197 | (if (and cperl-auto-newline |
db133cb6 RS |
2198 | (eq last-command 'cperl-electric-else-really) |
2199 | (memq (preceding-char) '(?\ ?\t ?\n))) | |
2200 | (let (p) | |
2201 | (skip-chars-forward " \t\n") | |
2202 | (setq p (point)) | |
2203 | (skip-chars-backward " \t\n") | |
2204 | (delete-region (point) p)) | |
2205 | (backward-delete-char-untabify arg)))) | |
f83d2997 KH |
2206 | |
2207 | (defun cperl-inside-parens-p () | |
2208 | (condition-case () | |
2209 | (save-excursion | |
2210 | (save-restriction | |
2211 | (narrow-to-region (point) | |
2212 | (progn (beginning-of-defun) (point))) | |
2213 | (goto-char (point-max)) | |
2214 | (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
2215 | (error nil))) | |
2216 | \f | |
2217 | (defun cperl-indent-command (&optional whole-exp) | |
2218 | "Indent current line as Perl code, or in some cases insert a tab character. | |
5c8b7eaf | 2219 | If `cperl-tab-always-indent' is non-nil (the default), always indent current |
db133cb6 | 2220 | line. Otherwise, indent the current line only if point is at the left margin |
f83d2997 KH |
2221 | or in the line's indentation; otherwise insert a tab. |
2222 | ||
2223 | A numeric argument, regardless of its value, | |
2224 | means indent rigidly all the lines of the expression starting after point | |
2225 | so that this line becomes properly indented. | |
2226 | The relative indentation among the lines of the expression are preserved." | |
2227 | (interactive "P") | |
5bd52f0e | 2228 | (cperl-update-syntaxification (point) (point)) |
f83d2997 KH |
2229 | (if whole-exp |
2230 | ;; If arg, always indent this line as Perl | |
2231 | ;; and shift remaining lines of expression the same amount. | |
2232 | (let ((shift-amt (cperl-indent-line)) | |
2233 | beg end) | |
2234 | (save-excursion | |
2235 | (if cperl-tab-always-indent | |
2236 | (beginning-of-line)) | |
2237 | (setq beg (point)) | |
2238 | (forward-sexp 1) | |
2239 | (setq end (point)) | |
2240 | (goto-char beg) | |
2241 | (forward-line 1) | |
2242 | (setq beg (point))) | |
db133cb6 | 2243 | (if (and shift-amt (> end beg)) |
f83d2997 KH |
2244 | (indent-code-rigidly beg end shift-amt "#"))) |
2245 | (if (and (not cperl-tab-always-indent) | |
2246 | (save-excursion | |
2247 | (skip-chars-backward " \t") | |
2248 | (not (bolp)))) | |
2249 | (insert-tab) | |
2250 | (cperl-indent-line)))) | |
2251 | ||
5bd52f0e | 2252 | (defun cperl-indent-line (&optional parse-data) |
f83d2997 KH |
2253 | "Indent current line as Perl code. |
2254 | Return the amount the indentation changed by." | |
db133cb6 | 2255 | (let (indent i beg shift-amt |
f83d2997 KH |
2256 | (case-fold-search nil) |
2257 | (pos (- (point-max) (point)))) | |
5bd52f0e | 2258 | (setq indent (cperl-calculate-indent parse-data) |
db133cb6 | 2259 | i indent) |
f83d2997 KH |
2260 | (beginning-of-line) |
2261 | (setq beg (point)) | |
2262 | (cond ((or (eq indent nil) (eq indent t)) | |
db133cb6 | 2263 | (setq indent (current-indentation) i nil)) |
f83d2997 KH |
2264 | ;;((eq indent t) ; Never? |
2265 | ;; (setq indent (cperl-calculate-indent-within-comment))) | |
2266 | ;;((looking-at "[ \t]*#") | |
2267 | ;; (setq indent 0)) | |
2268 | (t | |
2269 | (skip-chars-forward " \t") | |
2270 | (if (listp indent) (setq indent (car indent))) | |
2271 | (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]") | |
2272 | (and (> indent 0) | |
2273 | (setq indent (max cperl-min-label-indent | |
2274 | (+ indent cperl-label-offset))))) | |
2275 | ((= (following-char) ?}) | |
2276 | (setq indent (- indent cperl-indent-level))) | |
2277 | ((memq (following-char) '(?\) ?\])) ; To line up with opening paren. | |
2278 | (setq indent (+ indent cperl-close-paren-offset))) | |
2279 | ((= (following-char) ?{) | |
2280 | (setq indent (+ indent cperl-brace-offset)))))) | |
2281 | (skip-chars-forward " \t") | |
db133cb6 RS |
2282 | (setq shift-amt (and i (- indent (current-column)))) |
2283 | (if (or (not shift-amt) | |
2284 | (zerop shift-amt)) | |
f83d2997 KH |
2285 | (if (> (- (point-max) pos) (point)) |
2286 | (goto-char (- (point-max) pos))) | |
2287 | (delete-region beg (point)) | |
2288 | (indent-to indent) | |
2289 | ;; If initial point was within line's indentation, | |
2290 | ;; position after the indentation. Else stay at same point in text. | |
2291 | (if (> (- (point-max) pos) (point)) | |
2292 | (goto-char (- (point-max) pos)))) | |
2293 | shift-amt)) | |
2294 | ||
2295 | (defun cperl-after-label () | |
2296 | ;; Returns true if the point is after label. Does not do save-excursion. | |
2297 | (and (eq (preceding-char) ?:) | |
2298 | (memq (char-syntax (char-after (- (point) 2))) | |
2299 | '(?w ?_)) | |
2300 | (progn | |
2301 | (backward-sexp) | |
2302 | (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))) | |
2303 | ||
2304 | (defun cperl-get-state (&optional parse-start start-state) | |
5bd52f0e RS |
2305 | ;; returns list (START STATE DEPTH PRESTART), |
2306 | ;; START is a good place to start parsing, or equal to | |
5c8b7eaf | 2307 | ;; PARSE-START if preset, |
5bd52f0e RS |
2308 | ;; STATE is what is returned by `parse-partial-sexp'. |
2309 | ;; DEPTH is true is we are immediately after end of block | |
2310 | ;; which contains START. | |
2311 | ;; PRESTART is the position basing on which START was found. | |
f83d2997 KH |
2312 | (save-excursion |
2313 | (let ((start-point (point)) depth state start prestart) | |
5bd52f0e RS |
2314 | (if (and parse-start |
2315 | (<= parse-start start-point)) | |
f83d2997 | 2316 | (goto-char parse-start) |
5bd52f0e RS |
2317 | (beginning-of-defun) |
2318 | (setq start-state nil)) | |
f83d2997 KH |
2319 | (setq prestart (point)) |
2320 | (if start-state nil | |
2321 | ;; Try to go out, if sub is not on the outermost level | |
2322 | (while (< (point) start-point) | |
2323 | (setq start (point) parse-start start depth nil | |
2324 | state (parse-partial-sexp start start-point -1)) | |
2325 | (if (> (car state) -1) nil | |
2326 | ;; The current line could start like }}}, so the indentation | |
2327 | ;; corresponds to a different level than what we reached | |
2328 | (setq depth t) | |
2329 | (beginning-of-line 2))) ; Go to the next line. | |
2330 | (if start (goto-char start))) ; Not at the start of file | |
2331 | (setq start (point)) | |
f83d2997 KH |
2332 | (or state (setq state (parse-partial-sexp start start-point -1 nil start-state))) |
2333 | (list start state depth prestart)))) | |
2334 | ||
2335 | (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" ! | |
2336 | ;; Positions is before ?\{. Checks whether it starts a block. | |
2337 | ;; No save-excursion! | |
2338 | (cperl-backward-to-noncomment (point-min)) | |
f83d2997 KH |
2339 | (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp |
2340 | ; Label may be mixed up with `$blah :' | |
2341 | (save-excursion (cperl-after-label)) | |
2342 | (and (memq (char-syntax (preceding-char)) '(?w ?_)) | |
2343 | (progn | |
2344 | (backward-sexp) | |
2345 | ;; Need take into account `bless', `return', `tr',... | |
2346 | (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax | |
5bd52f0e | 2347 | (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>"))) |
f83d2997 KH |
2348 | (progn |
2349 | (skip-chars-backward " \t\n\f") | |
2350 | (and (memq (char-syntax (preceding-char)) '(?w ?_)) | |
2351 | (progn | |
2352 | (backward-sexp) | |
5c8b7eaf | 2353 | (looking-at |
f83d2997 KH |
2354 | "sub[ \t]+[a-zA-Z0-9_:]+[ \t\n\f]*\\(([^()]*)[ \t\n\f]*\\)?[#{]"))))))))) |
2355 | ||
2356 | (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group))) | |
2357 | ||
5bd52f0e | 2358 | (defun cperl-calculate-indent (&optional parse-data) ; was parse-start |
f83d2997 KH |
2359 | "Return appropriate indentation for current line as Perl code. |
2360 | In usual case returns an integer: the column to indent to. | |
5bd52f0e RS |
2361 | Returns nil if line starts inside a string, t if in a comment. |
2362 | ||
2363 | Will not correct the indentation for labels, but will correct it for braces | |
2364 | and closing parentheses and brackets.." | |
f83d2997 KH |
2365 | (save-excursion |
2366 | (if (or | |
6c389151 SM |
2367 | (and (memq (get-text-property (point) 'syntax-type) |
2368 | '(pod here-doc here-doc-delim format)) | |
2369 | (not (get-text-property (point) 'indentable))) | |
f83d2997 KH |
2370 | ;; before start of POD - whitespace found since do not have 'pod! |
2371 | (and (looking-at "[ \t]*\n=") | |
2372 | (error "Spaces before pod section!")) | |
2373 | (and (not cperl-indent-left-aligned-comments) | |
2374 | (looking-at "^#"))) | |
2375 | nil | |
2376 | (beginning-of-line) | |
2377 | (let ((indent-point (point)) | |
2378 | (char-after (save-excursion | |
2379 | (skip-chars-forward " \t") | |
2380 | (following-char))) | |
2381 | (in-pod (get-text-property (point) 'in-pod)) | |
2382 | (pre-indent-point (point)) | |
6c389151 | 2383 | p prop look-prop is-block delim) |
f83d2997 | 2384 | (cond |
5c8b7eaf | 2385 | (in-pod |
f83d2997 KH |
2386 | ;; In the verbatim part, probably code example. What to do??? |
2387 | ) | |
5c8b7eaf | 2388 | (t |
f83d2997 KH |
2389 | (save-excursion |
2390 | ;; Not in pod | |
2391 | (cperl-backward-to-noncomment nil) | |
2392 | (setq p (max (point-min) (1- (point))) | |
2393 | prop (get-text-property p 'syntax-type) | |
2394 | look-prop (or (nth 1 (assoc prop cperl-look-for-prop)) | |
2395 | 'syntax-type)) | |
2396 | (if (memq prop '(pod here-doc format here-doc-delim)) | |
2397 | (progn | |
5c8b7eaf | 2398 | (goto-char (or (previous-single-property-change p look-prop) |
f83d2997 KH |
2399 | (point-min))) |
2400 | (beginning-of-line) | |
2401 | (setq pre-indent-point (point))))))) | |
2402 | (goto-char pre-indent-point) | |
2403 | (let* ((case-fold-search nil) | |
5bd52f0e | 2404 | (s-s (cperl-get-state (car parse-data) (nth 1 parse-data))) |
5c8b7eaf | 2405 | (start (or (nth 2 parse-data) |
5bd52f0e | 2406 | (nth 0 s-s))) |
f83d2997 KH |
2407 | (state (nth 1 s-s)) |
2408 | (containing-sexp (car (cdr state))) | |
f83d2997 | 2409 | old-indent) |
5c8b7eaf | 2410 | (if (and |
5bd52f0e | 2411 | ;;containing-sexp ;; We are buggy at toplevel :-( |
5c8b7eaf | 2412 | parse-data) |
5bd52f0e RS |
2413 | (progn |
2414 | (setcar parse-data pre-indent-point) | |
2415 | (setcar (cdr parse-data) state) | |
2416 | (or (nth 2 parse-data) | |
2417 | (setcar (cddr parse-data) start)) | |
2418 | ;; Before this point: end of statement | |
2419 | (setq old-indent (nth 3 parse-data)))) | |
6c389151 SM |
2420 | (cond ((get-text-property (point) 'indentable) |
2421 | ;; indent to just after the surrounding open, | |
2422 | ;; skip blanks if we do not close the expression. | |
2423 | (goto-char (1+ (previous-single-property-change (point) 'indentable))) | |
2424 | (or (memq char-after (append ")]}" nil)) | |
2425 | (looking-at "[ \t]*\\(#\\|$\\)") | |
2426 | (skip-chars-forward " \t")) | |
2427 | (current-column)) | |
2428 | ((or (nth 3 state) (nth 4 state)) | |
f83d2997 KH |
2429 | ;; return nil or t if should not change this line |
2430 | (nth 4 state)) | |
6c389151 | 2431 | ;; XXXX Do we need to special-case this? |
f83d2997 KH |
2432 | ((null containing-sexp) |
2433 | ;; Line is at top level. May be data or function definition, | |
2434 | ;; or may be function argument declaration. | |
2435 | ;; Indent like the previous top level line | |
2436 | ;; unless that ends in a closeparen without semicolon, | |
2437 | ;; in which case this line is the first argument decl. | |
2438 | (skip-chars-forward " \t") | |
5bd52f0e RS |
2439 | (+ (save-excursion |
2440 | (goto-char start) | |
2441 | (- (current-indentation) | |
2442 | (if (nth 2 s-s) cperl-indent-level 0))) | |
2443 | (if (= char-after ?{) cperl-continued-brace-offset 0) | |
f83d2997 | 2444 | (progn |
5bd52f0e | 2445 | (cperl-backward-to-noncomment (or old-indent (point-min))) |
f83d2997 KH |
2446 | ;; Look at previous line that's at column 0 |
2447 | ;; to determine whether we are in top-level decls | |
2448 | ;; or function's arg decls. Set basic-indent accordingly. | |
2449 | ;; Now add a little if this is a continuation line. | |
2450 | (if (or (bobp) | |
5bd52f0e | 2451 | (eq (point) old-indent) ; old-indent was at comment |
db133cb6 RS |
2452 | (eq (preceding-char) ?\;) |
2453 | ;; Had ?\) too | |
2454 | (and (eq (preceding-char) ?\}) | |
5bd52f0e RS |
2455 | (cperl-after-block-and-statement-beg |
2456 | (point-min))) ; Was start - too close | |
f83d2997 KH |
2457 | (memq char-after (append ")]}" nil)) |
2458 | (and (eq (preceding-char) ?\:) ; label | |
2459 | (progn | |
2460 | (forward-sexp -1) | |
2461 | (skip-chars-backward " \t") | |
5c8b7eaf | 2462 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))) |
5bd52f0e RS |
2463 | (progn |
2464 | (if (and parse-data | |
2465 | (not (eq char-after ?\C-j))) | |
2466 | (setcdr (cddr parse-data) | |
2467 | (list pre-indent-point))) | |
2468 | 0) | |
f83d2997 | 2469 | cperl-continued-statement-offset)))) |
6c389151 SM |
2470 | ((not |
2471 | (or (setq is-block | |
2472 | (and (setq delim (= (char-after containing-sexp) ?{)) | |
2473 | (save-excursion ; Is it a hash? | |
2474 | (goto-char containing-sexp) | |
2475 | (cperl-block-p)))) | |
2476 | cperl-indent-parens-as-block)) | |
2477 | ;; group is an expression, not a block: | |
2478 | ;; indent to just after the surrounding open parens, | |
f83d2997 KH |
2479 | ;; skip blanks if we do not close the expression. |
2480 | (goto-char (1+ containing-sexp)) | |
2481 | (or (memq char-after (append ")]}" nil)) | |
2482 | (looking-at "[ \t]*\\(#\\|$\\)") | |
2483 | (skip-chars-forward " \t")) | |
2484 | (current-column)) | |
2485 | ((progn | |
2486 | ;; Containing-expr starts with \{. Check whether it is a hash. | |
2487 | (goto-char containing-sexp) | |
2488 | (not (cperl-block-p))) | |
2489 | (goto-char (1+ containing-sexp)) | |
6c389151 SM |
2490 | (or (memq char-after |
2491 | (append (if delim "}" ")]}") nil)) | |
f83d2997 KH |
2492 | (looking-at "[ \t]*\\(#\\|$\\)") |
2493 | (skip-chars-forward " \t")) | |
6c389151 SM |
2494 | (+ (current-column) |
2495 | (if (and delim | |
2496 | (eq char-after ?\})) | |
2497 | ;; Correct indentation of trailing ?\} | |
2498 | (+ cperl-indent-level cperl-close-paren-offset) | |
f83d2997 | 2499 | 0))) |
6c389151 SM |
2500 | ;;; ((and (/= (char-after containing-sexp) ?{) |
2501 | ;;; (not cperl-indent-parens-as-block)) | |
2502 | ;;; ;; line is expression, not statement: | |
2503 | ;;; ;; indent to just after the surrounding open, | |
2504 | ;;; ;; skip blanks if we do not close the expression. | |
2505 | ;;; (goto-char (1+ containing-sexp)) | |
2506 | ;;; (or (memq char-after (append ")]}" nil)) | |
2507 | ;;; (looking-at "[ \t]*\\(#\\|$\\)") | |
2508 | ;;; (skip-chars-forward " \t")) | |
2509 | ;;; (current-column)) | |
2510 | ;;; ((progn | |
2511 | ;;; ;; Containing-expr starts with \{. Check whether it is a hash. | |
2512 | ;;; (goto-char containing-sexp) | |
2513 | ;;; (and (not (cperl-block-p)) | |
2514 | ;;; (not cperl-indent-parens-as-block))) | |
2515 | ;;; (goto-char (1+ containing-sexp)) | |
2516 | ;;; (or (eq char-after ?\}) | |
2517 | ;;; (looking-at "[ \t]*\\(#\\|$\\)") | |
2518 | ;;; (skip-chars-forward " \t")) | |
2519 | ;;; (+ (current-column) ; Correct indentation of trailing ?\} | |
2520 | ;;; (if (eq char-after ?\}) (+ cperl-indent-level | |
2521 | ;;; cperl-close-paren-offset) | |
2522 | ;;; 0))) | |
f83d2997 KH |
2523 | (t |
2524 | ;; Statement level. Is it a continuation or a new statement? | |
2525 | ;; Find previous non-comment character. | |
2526 | (goto-char pre-indent-point) | |
2527 | (cperl-backward-to-noncomment containing-sexp) | |
2528 | ;; Back up over label lines, since they don't | |
2529 | ;; affect whether our line is a continuation. | |
5bd52f0e RS |
2530 | ;; (Had \, too) |
2531 | (while ;;(or (eq (preceding-char) ?\,) | |
f83d2997 KH |
2532 | (and (eq (preceding-char) ?:) |
2533 | (or;;(eq (char-after (- (point) 2)) ?\') ; ???? | |
2534 | (memq (char-syntax (char-after (- (point) 2))) | |
5bd52f0e RS |
2535 | '(?w ?_)))) |
2536 | ;;) | |
f83d2997 KH |
2537 | (if (eq (preceding-char) ?\,) |
2538 | ;; Will go to beginning of line, essentially. | |
2539 | ;; Will ignore embedded sexpr XXXX. | |
2540 | (cperl-backward-to-start-of-continued-exp containing-sexp)) | |
2541 | (beginning-of-line) | |
2542 | (cperl-backward-to-noncomment containing-sexp)) | |
2543 | ;; Now we get the answer. | |
6c389151 SM |
2544 | (if (not (or (eq (1- (point)) containing-sexp) |
2545 | (memq (preceding-char) | |
2546 | (append (if is-block " ;{" " ,;{") '(nil))) | |
db133cb6 | 2547 | (and (eq (preceding-char) ?\}) |
6c389151 SM |
2548 | (cperl-after-block-and-statement-beg |
2549 | containing-sexp)))) | |
f83d2997 KH |
2550 | ;; This line is continuation of preceding line's statement; |
2551 | ;; indent `cperl-continued-statement-offset' more than the | |
2552 | ;; previous line of the statement. | |
5bd52f0e RS |
2553 | ;; |
2554 | ;; There might be a label on this line, just | |
2555 | ;; consider it bad style and ignore it. | |
f83d2997 KH |
2556 | (progn |
2557 | (cperl-backward-to-start-of-continued-exp containing-sexp) | |
2558 | (+ (if (memq char-after (append "}])" nil)) | |
2559 | 0 ; Closing parenth | |
2560 | cperl-continued-statement-offset) | |
6c389151 SM |
2561 | (if (or is-block |
2562 | (not delim) | |
2563 | (not (eq char-after ?\}))) | |
2564 | 0 | |
2565 | ;; Now it is a hash reference | |
2566 | (+ cperl-indent-level cperl-close-paren-offset)) | |
5bd52f0e RS |
2567 | (if (looking-at "\\w+[ \t]*:") |
2568 | (if (> (current-indentation) cperl-min-label-indent) | |
2569 | (- (current-indentation) cperl-label-offset) | |
2570 | ;; Do not move `parse-data', this should | |
5c8b7eaf | 2571 | ;; be quick anyway (this comment comes |
f94a632a | 2572 | ;; from different location): |
5bd52f0e RS |
2573 | (cperl-calculate-indent)) |
2574 | (current-column)) | |
f83d2997 KH |
2575 | (if (eq char-after ?\{) |
2576 | cperl-continued-brace-offset 0))) | |
2577 | ;; This line starts a new statement. | |
2578 | ;; Position following last unclosed open. | |
2579 | (goto-char containing-sexp) | |
2580 | ;; Is line first statement after an open-brace? | |
2581 | (or | |
2582 | ;; If no, find that first statement and indent like | |
2583 | ;; it. If the first statement begins with label, do | |
2584 | ;; not believe when the indentation of the label is too | |
2585 | ;; small. | |
2586 | (save-excursion | |
2587 | (forward-char 1) | |
2588 | (setq old-indent (current-indentation)) | |
2589 | (let ((colon-line-end 0)) | |
2590 | (while (progn (skip-chars-forward " \t\n") | |
2591 | (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]")) | |
2592 | ;; Skip over comments and labels following openbrace. | |
2593 | (cond ((= (following-char) ?\#) | |
2594 | (forward-line 1)) | |
2595 | ;; label: | |
2596 | (t | |
2597 | (save-excursion (end-of-line) | |
2598 | (setq colon-line-end (point))) | |
2599 | (search-forward ":")))) | |
2600 | ;; The first following code counts | |
2601 | ;; if it is before the line we want to indent. | |
2602 | (and (< (point) indent-point) | |
2603 | (if (> colon-line-end (point)) ; After label | |
5c8b7eaf | 2604 | (if (> (current-indentation) |
f83d2997 KH |
2605 | cperl-min-label-indent) |
2606 | (- (current-indentation) cperl-label-offset) | |
2607 | ;; Do not believe: `max' is involved | |
2608 | (+ old-indent cperl-indent-level)) | |
2609 | (current-column))))) | |
2610 | ;; If no previous statement, | |
2611 | ;; indent it relative to line brace is on. | |
2612 | ;; For open brace in column zero, don't let statement | |
2613 | ;; start there too. If cperl-indent-level is zero, | |
2614 | ;; use cperl-brace-offset + cperl-continued-statement-offset instead. | |
2615 | ;; For open-braces not the first thing in a line, | |
2616 | ;; add in cperl-brace-imaginary-offset. | |
2617 | ||
2618 | ;; If first thing on a line: ????? | |
2619 | (+ (if (and (bolp) (zerop cperl-indent-level)) | |
2620 | (+ cperl-brace-offset cperl-continued-statement-offset) | |
2621 | cperl-indent-level) | |
6c389151 SM |
2622 | (if (or is-block |
2623 | (not delim) | |
2624 | (not (eq char-after ?\}))) | |
2625 | 0 | |
2626 | ;; Now it is a hash reference | |
2627 | (+ cperl-indent-level cperl-close-paren-offset)) | |
f83d2997 KH |
2628 | ;; Move back over whitespace before the openbrace. |
2629 | ;; If openbrace is not first nonwhite thing on the line, | |
2630 | ;; add the cperl-brace-imaginary-offset. | |
2631 | (progn (skip-chars-backward " \t") | |
2632 | (if (bolp) 0 cperl-brace-imaginary-offset)) | |
2633 | ;; If the openbrace is preceded by a parenthesized exp, | |
2634 | ;; move to the beginning of that; | |
2635 | ;; possibly a different line | |
2636 | (progn | |
2637 | (if (eq (preceding-char) ?\)) | |
2638 | (forward-sexp -1)) | |
2639 | ;; In the case it starts a subroutine, indent with | |
110c171f | 2640 | ;; respect to `sub', not with respect to the |
f83d2997 KH |
2641 | ;; first thing on the line, say in the case of |
2642 | ;; anonymous sub in a hash. | |
2643 | ;; | |
2644 | (skip-chars-backward " \t") | |
2645 | (if (and (eq (preceding-char) ?b) | |
2646 | (progn | |
2647 | (forward-sexp -1) | |
2648 | (looking-at "sub\\>")) | |
5c8b7eaf SS |
2649 | (setq old-indent |
2650 | (nth 1 | |
2651 | (parse-partial-sexp | |
2652 | (save-excursion (beginning-of-line) (point)) | |
f83d2997 KH |
2653 | (point))))) |
2654 | (progn (goto-char (1+ old-indent)) | |
2655 | (skip-chars-forward " \t") | |
2656 | (current-column)) | |
2657 | ;; Get initial indentation of the line we are on. | |
2658 | ;; If line starts with label, calculate label indentation | |
2659 | (if (save-excursion | |
2660 | (beginning-of-line) | |
2661 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]")) | |
2662 | (if (> (current-indentation) cperl-min-label-indent) | |
2663 | (- (current-indentation) cperl-label-offset) | |
5bd52f0e RS |
2664 | ;; Do not move `parse-data', this should |
2665 | ;; be quick anyway: | |
2666 | (cperl-calculate-indent)) | |
f83d2997 KH |
2667 | (current-indentation)))))))))))))) |
2668 | ||
2669 | (defvar cperl-indent-alist | |
2670 | '((string nil) | |
2671 | (comment nil) | |
2672 | (toplevel 0) | |
2673 | (toplevel-after-parenth 2) | |
2674 | (toplevel-continued 2) | |
2675 | (expression 1)) | |
2676 | "Alist of indentation rules for CPerl mode. | |
2677 | The values mean: | |
2678 | nil: do not indent; | |
db133cb6 RS |
2679 | number: add this amount of indentation. |
2680 | ||
2681 | Not finished, not used.") | |
f83d2997 KH |
2682 | |
2683 | (defun cperl-where-am-i (&optional parse-start start-state) | |
2684 | ;; Unfinished | |
2685 | "Return a list of lists ((TYPE POS)...) of good points before the point. | |
db133cb6 RS |
2686 | POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'. |
2687 | ||
2688 | Not finished, not used." | |
f83d2997 KH |
2689 | (save-excursion |
2690 | (let* ((start-point (point)) | |
2691 | (s-s (cperl-get-state)) | |
2692 | (start (nth 0 s-s)) | |
2693 | (state (nth 1 s-s)) | |
2694 | (prestart (nth 3 s-s)) | |
2695 | (containing-sexp (car (cdr state))) | |
2696 | (case-fold-search nil) | |
2697 | (res (list (list 'parse-start start) (list 'parse-prestart prestart)))) | |
2698 | (cond ((nth 3 state) ; In string | |
2699 | (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string | |
2700 | ((nth 4 state) ; In comment | |
2701 | (setq res (cons '(comment) res))) | |
2702 | ((null containing-sexp) | |
5c8b7eaf | 2703 | ;; Line is at top level. |
f83d2997 KH |
2704 | ;; Indent like the previous top level line |
2705 | ;; unless that ends in a closeparen without semicolon, | |
2706 | ;; in which case this line is the first argument decl. | |
2707 | (cperl-backward-to-noncomment (or parse-start (point-min))) | |
2708 | ;;(skip-chars-backward " \t\f\n") | |
2709 | (cond | |
2710 | ((or (bobp) | |
2711 | (memq (preceding-char) (append ";}" nil))) | |
2712 | (setq res (cons (list 'toplevel start) res))) | |
2713 | ((eq (preceding-char) ?\) ) | |
2714 | (setq res (cons (list 'toplevel-after-parenth start) res))) | |
5c8b7eaf | 2715 | (t |
f83d2997 KH |
2716 | (setq res (cons (list 'toplevel-continued start) res))))) |
2717 | ((/= (char-after containing-sexp) ?{) | |
2718 | ;; line is expression, not statement: | |
2719 | ;; indent to just after the surrounding open. | |
2720 | ;; skip blanks if we do not close the expression. | |
2721 | (setq res (cons (list 'expression-blanks | |
2722 | (progn | |
2723 | (goto-char (1+ containing-sexp)) | |
2724 | (or (looking-at "[ \t]*\\(#\\|$\\)") | |
2725 | (skip-chars-forward " \t")) | |
2726 | (point))) | |
2727 | (cons (list 'expression containing-sexp) res)))) | |
2728 | ((progn | |
2729 | ;; Containing-expr starts with \{. Check whether it is a hash. | |
2730 | (goto-char containing-sexp) | |
2731 | (not (cperl-block-p))) | |
2732 | (setq res (cons (list 'expression-blanks | |
2733 | (progn | |
2734 | (goto-char (1+ containing-sexp)) | |
2735 | (or (looking-at "[ \t]*\\(#\\|$\\)") | |
2736 | (skip-chars-forward " \t")) | |
2737 | (point))) | |
2738 | (cons (list 'expression containing-sexp) res)))) | |
2739 | (t | |
2740 | ;; Statement level. | |
2741 | (setq res (cons (list 'in-block containing-sexp) res)) | |
2742 | ;; Is it a continuation or a new statement? | |
2743 | ;; Find previous non-comment character. | |
2744 | (cperl-backward-to-noncomment containing-sexp) | |
2745 | ;; Back up over label lines, since they don't | |
2746 | ;; affect whether our line is a continuation. | |
2747 | ;; Back up comma-delimited lines too ????? | |
2748 | (while (or (eq (preceding-char) ?\,) | |
2749 | (save-excursion (cperl-after-label))) | |
2750 | (if (eq (preceding-char) ?\,) | |
2751 | ;; Will go to beginning of line, essentially | |
2752 | ;; Will ignore embedded sexpr XXXX. | |
2753 | (cperl-backward-to-start-of-continued-exp containing-sexp)) | |
2754 | (beginning-of-line) | |
2755 | (cperl-backward-to-noncomment containing-sexp)) | |
2756 | ;; Now we get the answer. | |
2757 | (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\, | |
2758 | ;; This line is continuation of preceding line's statement. | |
2759 | (list (list 'statement-continued containing-sexp)) | |
2760 | ;; This line starts a new statement. | |
2761 | ;; Position following last unclosed open. | |
2762 | (goto-char containing-sexp) | |
2763 | ;; Is line first statement after an open-brace? | |
2764 | (or | |
2765 | ;; If no, find that first statement and indent like | |
2766 | ;; it. If the first statement begins with label, do | |
2767 | ;; not believe when the indentation of the label is too | |
2768 | ;; small. | |
2769 | (save-excursion | |
2770 | (forward-char 1) | |
2771 | (let ((colon-line-end 0)) | |
2772 | (while (progn (skip-chars-forward " \t\n" start-point) | |
2773 | (and (< (point) start-point) | |
2774 | (looking-at | |
2775 | "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))) | |
2776 | ;; Skip over comments and labels following openbrace. | |
2777 | (cond ((= (following-char) ?\#) | |
2778 | ;;(forward-line 1) | |
2779 | (end-of-line)) | |
2780 | ;; label: | |
2781 | (t | |
2782 | (save-excursion (end-of-line) | |
2783 | (setq colon-line-end (point))) | |
2784 | (search-forward ":")))) | |
5c8b7eaf | 2785 | ;; Now at the point, after label, or at start |
f83d2997 KH |
2786 | ;; of first statement in the block. |
2787 | (and (< (point) start-point) | |
5c8b7eaf | 2788 | (if (> colon-line-end (point)) |
f83d2997 | 2789 | ;; Before statement after label |
5c8b7eaf | 2790 | (if (> (current-indentation) |
f83d2997 KH |
2791 | cperl-min-label-indent) |
2792 | (list (list 'label-in-block (point))) | |
2793 | ;; Do not believe: `max' is involved | |
2794 | (list | |
2795 | (list 'label-in-block-min-indent (point)))) | |
2796 | ;; Before statement | |
2797 | (list 'statement-in-block (point)))))) | |
2798 | ;; If no previous statement, | |
2799 | ;; indent it relative to line brace is on. | |
2800 | ;; For open brace in column zero, don't let statement | |
2801 | ;; start there too. If cperl-indent-level is zero, | |
2802 | ;; use cperl-brace-offset + cperl-continued-statement-offset instead. | |
2803 | ;; For open-braces not the first thing in a line, | |
2804 | ;; add in cperl-brace-imaginary-offset. | |
2805 | ||
2806 | ;; If first thing on a line: ????? | |
2807 | (+ (if (and (bolp) (zerop cperl-indent-level)) | |
2808 | (+ cperl-brace-offset cperl-continued-statement-offset) | |
2809 | cperl-indent-level) | |
2810 | ;; Move back over whitespace before the openbrace. | |
2811 | ;; If openbrace is not first nonwhite thing on the line, | |
2812 | ;; add the cperl-brace-imaginary-offset. | |
2813 | (progn (skip-chars-backward " \t") | |
2814 | (if (bolp) 0 cperl-brace-imaginary-offset)) | |
2815 | ;; If the openbrace is preceded by a parenthesized exp, | |
2816 | ;; move to the beginning of that; | |
2817 | ;; possibly a different line | |
2818 | (progn | |
2819 | (if (eq (preceding-char) ?\)) | |
2820 | (forward-sexp -1)) | |
2821 | ;; Get initial indentation of the line we are on. | |
2822 | ;; If line starts with label, calculate label indentation | |
2823 | (if (save-excursion | |
2824 | (beginning-of-line) | |
2825 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]")) | |
2826 | (if (> (current-indentation) cperl-min-label-indent) | |
2827 | (- (current-indentation) cperl-label-offset) | |
5bd52f0e | 2828 | (cperl-calculate-indent)) |
f83d2997 KH |
2829 | (current-indentation)))))))) |
2830 | res))) | |
2831 | ||
2832 | (defun cperl-calculate-indent-within-comment () | |
2833 | "Return the indentation amount for line, assuming that | |
2834 | the current line is to be regarded as part of a block comment." | |
2835 | (let (end star-start) | |
2836 | (save-excursion | |
2837 | (beginning-of-line) | |
2838 | (skip-chars-forward " \t") | |
2839 | (setq end (point)) | |
2840 | (and (= (following-char) ?#) | |
2841 | (forward-line -1) | |
2842 | (cperl-to-comment-or-eol) | |
2843 | (setq end (point))) | |
2844 | (goto-char end) | |
2845 | (current-column)))) | |
2846 | ||
2847 | ||
2848 | (defun cperl-to-comment-or-eol () | |
029cb4d5 | 2849 | "Go to position before comment on the current line, or to end of line. |
f83d2997 KH |
2850 | Returns true if comment is found." |
2851 | (let (state stop-in cpoint (lim (progn (end-of-line) (point)))) | |
2852 | (beginning-of-line) | |
5c8b7eaf | 2853 | (if (or |
f83d2997 KH |
2854 | (eq (get-text-property (point) 'syntax-type) 'pod) |
2855 | (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)) | |
2856 | (if (eq (preceding-char) ?\#) (progn (backward-char 1) t)) | |
2857 | ;; Else | |
2858 | (while (not stop-in) | |
2859 | (setq state (parse-partial-sexp (point) lim nil nil nil t)) | |
2860 | ; stop at comment | |
2861 | ;; If fails (beginning-of-line inside sexp), then contains not-comment | |
f83d2997 KH |
2862 | (if (nth 4 state) ; After `#'; |
2863 | ; (nth 2 state) can be | |
2864 | ; beginning of m,s,qq and so | |
2865 | ; on | |
2866 | (if (nth 2 state) | |
2867 | (progn | |
2868 | (setq cpoint (point)) | |
2869 | (goto-char (nth 2 state)) | |
2870 | (cond | |
2871 | ((looking-at "\\(s\\|tr\\)\\>") | |
2872 | (or (re-search-forward | |
2873 | "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*" | |
2874 | lim 'move) | |
2875 | (setq stop-in t))) | |
5bd52f0e | 2876 | ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>") |
f83d2997 KH |
2877 | (or (re-search-forward |
2878 | "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#" | |
2879 | lim 'move) | |
2880 | (setq stop-in t))) | |
2881 | (t ; It was fair comment | |
2882 | (setq stop-in t) ; Finish | |
2883 | (goto-char (1- cpoint))))) | |
2884 | (setq stop-in t) ; Finish | |
2885 | (forward-char -1)) | |
2886 | (setq stop-in t)) ; Finish | |
2887 | ) | |
2888 | (nth 4 state)))) | |
2889 | ||
2890 | (defsubst cperl-1- (p) | |
2891 | (max (point-min) (1- p))) | |
2892 | ||
2893 | (defsubst cperl-1+ (p) | |
2894 | (min (point-max) (1+ p))) | |
2895 | ||
f83d2997 KH |
2896 | (defsubst cperl-modify-syntax-type (at how) |
2897 | (if (< at (point-max)) | |
2898 | (progn | |
2899 | (put-text-property at (1+ at) 'syntax-table how) | |
2900 | (put-text-property at (1+ at) 'rear-nonsticky t)))) | |
2901 | ||
2902 | (defun cperl-protect-defun-start (s e) | |
2903 | ;; C code looks for "^\\s(" to skip comment backward in "hard" situations | |
2904 | (save-excursion | |
2905 | (goto-char s) | |
2906 | (while (re-search-forward "^\\s(" e 'to-end) | |
2907 | (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct)))) | |
2908 | ||
5bd52f0e | 2909 | (defun cperl-commentify (bb e string &optional noface) |
5c8b7eaf | 2910 | (if cperl-use-syntax-table-text-property |
5bd52f0e RS |
2911 | (if (eq noface 'n) ; Only immediate |
2912 | nil | |
f83d2997 KH |
2913 | ;; We suppose that e is _after_ the end of construction, as after eol. |
2914 | (setq string (if string cperl-st-sfence cperl-st-cfence)) | |
6c389151 SM |
2915 | (if (> bb (- e 2)) |
2916 | ;; one-char string/comment?! | |
2917 | (cperl-modify-syntax-type bb cperl-st-punct) | |
2918 | (cperl-modify-syntax-type bb string) | |
2919 | (cperl-modify-syntax-type (1- e) string)) | |
f83d2997 | 2920 | (if (and (eq string cperl-st-sfence) (> (- e 2) bb)) |
5c8b7eaf | 2921 | (put-text-property (1+ bb) (1- e) |
f83d2997 | 2922 | 'syntax-table cperl-string-syntax-table)) |
5bd52f0e RS |
2923 | (cperl-protect-defun-start bb e)) |
2924 | ;; Fontify | |
2925 | (or noface | |
2926 | (not cperl-pod-here-fontify) | |
2927 | (put-text-property bb e 'face (if string 'font-lock-string-face | |
2928 | 'font-lock-comment-face))))) | |
6c389151 | 2929 | |
5bd52f0e RS |
2930 | (defvar cperl-starters '(( ?\( . ?\) ) |
2931 | ( ?\[ . ?\] ) | |
2932 | ( ?\{ . ?\} ) | |
2933 | ( ?\< . ?\> ))) | |
f83d2997 KH |
2934 | |
2935 | (defun cperl-forward-re (lim end is-2arg set-st st-l err-l argument | |
2936 | &optional ostart oend) | |
2937 | ;; Works *before* syntax recognition is done | |
2938 | ;; May modify syntax-type text property if the situation is too hard | |
6c389151 | 2939 | (let (b starter ender st i i2 go-forward reset-st) |
f83d2997 KH |
2940 | (skip-chars-forward " \t") |
2941 | ;; ender means matching-char matcher. | |
5c8b7eaf | 2942 | (setq b (point) |
5bd52f0e RS |
2943 | starter (if (eobp) 0 (char-after b)) |
2944 | ender (cdr (assoc starter cperl-starters))) | |
f83d2997 KH |
2945 | ;; What if starter == ?\\ ???? |
2946 | (if set-st | |
2947 | (if (car st-l) | |
2948 | (setq st (car st-l)) | |
2949 | (setcar st-l (make-syntax-table)) | |
2950 | (setq i 0 st (car st-l)) | |
2951 | (while (< i 256) | |
2952 | (modify-syntax-entry i "." st) | |
2953 | (setq i (1+ i))) | |
2954 | (modify-syntax-entry ?\\ "\\" st))) | |
2955 | (setq set-st t) | |
2956 | ;; Whether we have an intermediate point | |
2957 | (setq i nil) | |
2958 | ;; Prepare the syntax table: | |
2959 | (and set-st | |
2960 | (if (not ender) ; m/blah/, s/x//, s/x/y/ | |
2961 | (modify-syntax-entry starter "$" st) | |
2962 | (modify-syntax-entry starter (concat "(" (list ender)) st) | |
2963 | (modify-syntax-entry ender (concat ")" (list starter)) st))) | |
2964 | (condition-case bb | |
2965 | (progn | |
5bd52f0e RS |
2966 | ;; We use `$' syntax class to find matching stuff, but $$ |
2967 | ;; is recognized the same as $, so we need to check this manually. | |
f83d2997 KH |
2968 | (if (and (eq starter (char-after (cperl-1+ b))) |
2969 | (not ender)) | |
2970 | ;; $ has TeXish matching rules, so $$ equiv $... | |
2971 | (forward-char 2) | |
6c389151 | 2972 | (setq reset-st (syntax-table)) |
f83d2997 KH |
2973 | (set-syntax-table st) |
2974 | (forward-sexp 1) | |
6c389151 SM |
2975 | (if (<= (point) (1+ b)) |
2976 | (error "Unfinished regular expression")) | |
2977 | (set-syntax-table reset-st) | |
2978 | (setq reset-st nil) | |
f83d2997 KH |
2979 | ;; Now the problem is with m;blah;; |
2980 | (and (not ender) | |
2981 | (eq (preceding-char) | |
2982 | (char-after (- (point) 2))) | |
2983 | (save-excursion | |
2984 | (forward-char -2) | |
2985 | (= 0 (% (skip-chars-backward "\\\\") 2))) | |
2986 | (forward-char -1))) | |
5bd52f0e | 2987 | ;; Now we are after the first part. |
f83d2997 KH |
2988 | (and is-2arg ; Have trailing part |
2989 | (not ender) | |
2990 | (eq (following-char) starter) ; Empty trailing part | |
2991 | (progn | |
2992 | (or (eq (char-syntax (following-char)) ?.) | |
2993 | ;; Make trailing letter into punctuation | |
2994 | (cperl-modify-syntax-type (point) cperl-st-punct)) | |
2995 | (setq is-2arg nil go-forward t))) ; Ignore the tail | |
2996 | (if is-2arg ; Not number => have second part | |
2997 | (progn | |
2998 | (setq i (point) i2 i) | |
2999 | (if ender | |
3000 | (if (memq (following-char) '(?\ ?\t ?\n ?\f)) | |
3001 | (progn | |
3002 | (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+") | |
3003 | (goto-char (match-end 0)) | |
3004 | (skip-chars-forward " \t\n\f")) | |
3005 | (setq i2 (point)))) | |
3006 | (forward-char -1)) | |
3007 | (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st) | |
5c8b7eaf | 3008 | (if ender (modify-syntax-entry ender "." st)) |
f83d2997 | 3009 | (setq set-st nil) |
5bd52f0e RS |
3010 | (setq ender (cperl-forward-re lim end nil t st-l err-l |
3011 | argument starter ender) | |
f83d2997 KH |
3012 | ender (nth 2 ender))))) |
3013 | (error (goto-char lim) | |
3014 | (setq set-st nil) | |
6c389151 SM |
3015 | (if reset-st |
3016 | (set-syntax-table reset-st)) | |
f83d2997 KH |
3017 | (or end |
3018 | (message | |
5bd52f0e | 3019 | "End of `%s%s%c ... %c' string/RE not found: %s" |
f83d2997 KH |
3020 | argument |
3021 | (if ostart (format "%c ... %c" ostart (or oend ostart)) "") | |
3022 | starter (or ender starter) bb) | |
3023 | (or (car err-l) (setcar err-l b))))) | |
3024 | (if set-st | |
3025 | (progn | |
3026 | (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st) | |
3027 | (if ender (modify-syntax-entry ender "." st)))) | |
5bd52f0e RS |
3028 | ;; i: have 2 args, after end of the first arg |
3029 | ;; i2: start of the second arg, if any (before delim iff `ender'). | |
3030 | ;; ender: the last arg bounded by parens-like chars, the second one of them | |
3031 | ;; starter: the starting delimiter of the first arg | |
5efe6a56 | 3032 | ;; go-forward: has 2 args, and the second part is empty |
f83d2997 KH |
3033 | (list i i2 ender starter go-forward))) |
3034 | ||
5c8b7eaf | 3035 | (defsubst cperl-postpone-fontification (b e type val &optional now) |
5bd52f0e RS |
3036 | ;; Do after syntactic fontification? |
3037 | (if cperl-syntaxify-by-font-lock | |
3038 | (or now (put-text-property b e 'cperl-postpone (cons type val))) | |
3039 | (put-text-property b e type val))) | |
3040 | ||
3041 | ;;; Here is how the global structures (those which cannot be | |
3042 | ;;; recognized locally) are marked: | |
5c8b7eaf | 3043 | ;; a) PODs: |
5bd52f0e RS |
3044 | ;; Start-to-end is marked `in-pod' ==> t |
3045 | ;; Each non-literal part is marked `syntax-type' ==> `pod' | |
3046 | ;; Each literal part is marked `syntax-type' ==> `in-pod' | |
5c8b7eaf | 3047 | ;; b) HEREs: |
5bd52f0e RS |
3048 | ;; Start-to-end is marked `here-doc-group' ==> t |
3049 | ;; The body is marked `syntax-type' ==> `here-doc' | |
3050 | ;; The delimiter is marked `syntax-type' ==> `here-doc-delim' | |
5c8b7eaf | 3051 | ;; c) FORMATs: |
5bd52f0e | 3052 | ;; After-initial-line--to-end is marked `syntax-type' ==> `format' |
5c8b7eaf | 3053 | ;; d) 'Q'uoted string: |
5bd52f0e | 3054 | ;; part between markers inclusive is marked `syntax-type' ==> `string' |
6c389151 | 3055 | ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring' |
5bd52f0e RS |
3056 | |
3057 | (defun cperl-unwind-to-safe (before &optional end) | |
3058 | ;; if BEFORE, go to the previous start-of-line on each step of unwinding | |
3059 | (let ((pos (point)) opos) | |
3060 | (setq opos pos) | |
3061 | (while (and pos (get-text-property pos 'syntax-type)) | |
3062 | (setq pos (previous-single-property-change pos 'syntax-type)) | |
3063 | (if pos | |
3064 | (if before | |
3065 | (progn | |
3066 | (goto-char (cperl-1- pos)) | |
3067 | (beginning-of-line) | |
3068 | (setq pos (point))) | |
3069 | (goto-char (setq pos (cperl-1- pos)))) | |
3070 | ;; Up to the start | |
3071 | (goto-char (point-min)))) | |
6c389151 SM |
3072 | ;; Skip empty lines |
3073 | (and (looking-at "\n*=") | |
3074 | (/= 0 (skip-chars-backward "\n")) | |
3075 | (forward-char)) | |
3076 | (setq pos (point)) | |
5bd52f0e RS |
3077 | (if end |
3078 | ;; Do the same for end, going small steps | |
3079 | (progn | |
3080 | (while (and end (get-text-property end 'syntax-type)) | |
3081 | (setq pos end | |
3082 | end (next-single-property-change end 'syntax-type))) | |
3083 | (or end pos))))) | |
3084 | ||
6c389151 SM |
3085 | (defvar cperl-nonoverridable-face) |
3086 | (defvar font-lock-function-name-face) | |
3087 | (defvar font-lock-comment-face) | |
3088 | ||
db133cb6 | 3089 | (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max) |
f83d2997 | 3090 | "Scans the buffer for hard-to-parse Perl constructions. |
5c8b7eaf SS |
3091 | If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify |
3092 | the sections using `cperl-pod-head-face', `cperl-pod-face', | |
f83d2997 KH |
3093 | `cperl-here-face'." |
3094 | (interactive) | |
db133cb6 RS |
3095 | (or min (setq min (point-min) |
3096 | cperl-syntax-state nil | |
3097 | cperl-syntax-done-to min)) | |
f83d2997 | 3098 | (or max (setq max (point-max))) |
5bd52f0e | 3099 | (let* (face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb |
6c389151 SM |
3100 | is-REx is-x-REx REx-comment-start REx-comment-end was-comment i2 |
3101 | (cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend | |
db133cb6 RS |
3102 | (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t) |
3103 | (modified (buffer-modified-p)) | |
3104 | (after-change-functions nil) | |
3105 | (use-syntax-state (and cperl-syntax-state | |
3106 | (>= min (car cperl-syntax-state)))) | |
3107 | (state-point (if use-syntax-state | |
3108 | (car cperl-syntax-state) | |
3109 | (point-min))) | |
3110 | (state (if use-syntax-state | |
3111 | (cdr cperl-syntax-state))) | |
6c389151 SM |
3112 | ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call! |
3113 | (st-l (list nil)) (err-l (list nil)) | |
db133cb6 RS |
3114 | ;; Somehow font-lock may be not loaded yet... |
3115 | (font-lock-string-face (if (boundp 'font-lock-string-face) | |
3116 | font-lock-string-face | |
3117 | 'font-lock-string-face)) | |
5bd52f0e RS |
3118 | (font-lock-constant-face (if (boundp 'font-lock-constant-face) |
3119 | font-lock-constant-face | |
3120 | 'font-lock-constant-face)) | |
5c8b7eaf | 3121 | (font-lock-function-name-face |
5bd52f0e RS |
3122 | (if (boundp 'font-lock-function-name-face) |
3123 | font-lock-function-name-face | |
3124 | 'font-lock-function-name-face)) | |
6c389151 SM |
3125 | (font-lock-comment-face |
3126 | (if (boundp 'font-lock-comment-face) | |
3127 | font-lock-comment-face | |
3128 | 'font-lock-comment-face)) | |
3129 | (cperl-nonoverridable-face | |
5bd52f0e RS |
3130 | (if (boundp 'cperl-nonoverridable-face) |
3131 | cperl-nonoverridable-face | |
3132 | 'cperl-nonoverridable-face)) | |
5c8b7eaf | 3133 | (stop-point (if ignore-max |
db133cb6 RS |
3134 | (point-max) |
3135 | max)) | |
3136 | (search | |
3137 | (concat | |
6c389151 | 3138 | "\\(\\`\n?\\|^\n\\)=" |
db133cb6 RS |
3139 | "\\|" |
3140 | ;; One extra () before this: | |
5c8b7eaf | 3141 | "<<" |
5bd52f0e | 3142 | "\\(" ; 1 + 1 |
db133cb6 | 3143 | ;; First variant "BLAH" or just ``. |
6c389151 SM |
3144 | "[ \t]*" ; Yes, whitespace is allowed! |
3145 | "\\([\"'`]\\)" ; 2 + 1 = 3 | |
5bd52f0e | 3146 | "\\([^\"'`\n]*\\)" ; 3 + 1 |
db133cb6 RS |
3147 | "\\3" |
3148 | "\\|" | |
5bd52f0e RS |
3149 | ;; Second variant: Identifier or \ID or empty |
3150 | "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1 | |
3151 | ;; Do not have <<= or << 30 or <<30 or << $blah. | |
3152 | ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1 | |
3153 | "\\(\\)" ; To preserve count of pars :-( 6 + 1 | |
db133cb6 RS |
3154 | "\\)" |
3155 | "\\|" | |
3156 | ;; 1+6 extra () before this: | |
3157 | "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" | |
3158 | (if cperl-use-syntax-table-text-property | |
3159 | (concat | |
3160 | "\\|" | |
3161 | ;; 1+6+2=9 extra () before this: | |
5bd52f0e | 3162 | "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" |
db133cb6 RS |
3163 | "\\|" |
3164 | ;; 1+6+2+1=10 extra () before this: | |
5bd52f0e | 3165 | "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob> |
db133cb6 RS |
3166 | "\\|" |
3167 | ;; 1+6+2+1+1=11 extra () before this: | |
3168 | "\\<sub\\>[ \t]*\\([a-zA-Z_:'0-9]+[ \t]*\\)?\\(([^()]*)\\)" | |
3169 | "\\|" | |
3170 | ;; 1+6+2+1+1+2=13 extra () before this: | |
3171 | "\\$\\(['{]\\)" | |
3172 | "\\|" | |
3173 | ;; 1+6+2+1+1+2+1=14 extra () before this: | |
3174 | "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'" | |
3175 | ;; 1+6+2+1+1+2+1+1=15 extra () before this: | |
3176 | "\\|" | |
6c389151 SM |
3177 | "__\\(END\\|DATA\\)__" |
3178 | ;; 1+6+2+1+1+2+1+1+1=16 extra () before this: | |
3179 | "\\|" | |
3180 | "\\\\\\(['`\"]\\)" | |
db133cb6 RS |
3181 | ) |
3182 | "")))) | |
f83d2997 KH |
3183 | (unwind-protect |
3184 | (progn | |
3185 | (save-excursion | |
3186 | (or non-inter | |
3187 | (message "Scanning for \"hard\" Perl constructions...")) | |
db133cb6 | 3188 | (and cperl-pod-here-fontify |
f83d2997 KH |
3189 | ;; We had evals here, do not know why... |
3190 | (setq face cperl-pod-face | |
3191 | head-face cperl-pod-head-face | |
3192 | here-face cperl-here-face)) | |
5c8b7eaf | 3193 | (remove-text-properties min max |
5bd52f0e | 3194 | '(syntax-type t in-pod t syntax-table t |
6c389151 SM |
3195 | cperl-postpone t |
3196 | syntax-subtype t | |
3197 | rear-nonsticky t | |
3198 | indentable t)) | |
f83d2997 KH |
3199 | ;; Need to remove face as well... |
3200 | (goto-char min) | |
db133cb6 RS |
3201 | (and (eq system-type 'emx) |
3202 | (looking-at "extproc[ \t]") ; Analogue of #! | |
5c8b7eaf | 3203 | (cperl-commentify min |
db133cb6 RS |
3204 | (save-excursion (end-of-line) (point)) |
3205 | nil)) | |
3206 | (while (and | |
3207 | (< (point) max) | |
3208 | (re-search-forward search max t)) | |
5bd52f0e | 3209 | (setq tmpend nil) ; Valid for most cases |
5c8b7eaf | 3210 | (cond |
f83d2997 | 3211 | ((match-beginning 1) ; POD section |
6c389151 SM |
3212 | ;; "\\(\\`\n?\\|^\n\\)=" |
3213 | (if (looking-at "cut\\>") | |
5bd52f0e RS |
3214 | (if ignore-max |
3215 | nil ; Doing a chunk only | |
f83d2997 KH |
3216 | (message "=cut is not preceded by a POD section") |
3217 | (or (car err-l) (setcar err-l (point)))) | |
3218 | (beginning-of-line) | |
5c8b7eaf SS |
3219 | |
3220 | (setq b (point) | |
5bd52f0e RS |
3221 | bb b |
3222 | tb (match-beginning 0) | |
3223 | b1 nil) ; error condition | |
db133cb6 RS |
3224 | ;; We do not search to max, since we may be called from |
3225 | ;; some hook of fontification, and max is random | |
6c389151 | 3226 | (or (re-search-forward "^\n=cut\\>" stop-point 'toend) |
f83d2997 | 3227 | (progn |
6c389151 SM |
3228 | (goto-char b) |
3229 | (if (re-search-forward "\n=cut\\>" stop-point 'toend) | |
3230 | (progn | |
3231 | (message "=cut is not preceded by an empty line") | |
3232 | (setq b1 t) | |
3233 | (or (car err-l) (setcar err-l b)))))) | |
f83d2997 KH |
3234 | (beginning-of-line 2) ; An empty line after =cut is not POD! |
3235 | (setq e (point)) | |
db133cb6 | 3236 | (and (> e max) |
6c389151 SM |
3237 | (progn |
3238 | (remove-text-properties | |
3239 | max e '(syntax-type t in-pod t syntax-table t | |
3240 | cperl-postpone t | |
3241 | syntax-subtype t | |
3242 | rear-nonsticky t | |
3243 | indentable t)) | |
3244 | (setq tmpend tb))) | |
f83d2997 | 3245 | (put-text-property b e 'in-pod t) |
6c389151 | 3246 | (put-text-property b e 'syntax-type 'in-pod) |
f83d2997 KH |
3247 | (goto-char b) |
3248 | (while (re-search-forward "\n\n[ \t]" e t) | |
3249 | ;; We start 'pod 1 char earlier to include the preceding line | |
3250 | (beginning-of-line) | |
3251 | (put-text-property (cperl-1- b) (point) 'syntax-type 'pod) | |
5efe6a56 SM |
3252 | (cperl-put-do-not-fontify b (point) t) |
3253 | ;; mark the non-literal parts as PODs | |
3254 | (if cperl-pod-here-fontify | |
3255 | (cperl-postpone-fontification b (point) 'face face t)) | |
f83d2997 KH |
3256 | (re-search-forward "\n\n[^ \t\f\n]" e 'toend) |
3257 | (beginning-of-line) | |
3258 | (setq b (point))) | |
3259 | (put-text-property (cperl-1- (point)) e 'syntax-type 'pod) | |
5efe6a56 SM |
3260 | (cperl-put-do-not-fontify (point) e t) |
3261 | (if cperl-pod-here-fontify | |
3262 | (progn | |
3263 | ;; mark the non-literal parts as PODs | |
3264 | (cperl-postpone-fontification (point) e 'face face t) | |
3265 | (goto-char bb) | |
3266 | (if (looking-at | |
3267 | "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$") | |
3268 | ;; mark the headers | |
3269 | (cperl-postpone-fontification | |
3270 | (match-beginning 1) (match-end 1) | |
6c389151 SM |
3271 | 'face head-face)) |
3272 | (while (re-search-forward | |
3273 | ;; One paragraph | |
3274 | "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$" | |
3275 | e 'toend) | |
3276 | ;; mark the headers | |
3277 | (cperl-postpone-fontification | |
5efe6a56 SM |
3278 | (match-beginning 1) (match-end 1) |
3279 | 'face head-face)))) | |
f83d2997 KH |
3280 | (cperl-commentify bb e nil) |
3281 | (goto-char e) | |
3282 | (or (eq e (point-max)) | |
6c389151 | 3283 | (forward-char -1)))) ; Prepare for immediate pod start. |
f83d2997 KH |
3284 | ;; Here document |
3285 | ;; We do only one here-per-line | |
5bd52f0e | 3286 | ;; ;; One extra () before this: |
5c8b7eaf | 3287 | ;;"<<" |
5bd52f0e RS |
3288 | ;; "\\(" ; 1 + 1 |
3289 | ;; ;; First variant "BLAH" or just ``. | |
3290 | ;; "\\([\"'`]\\)" ; 2 + 1 | |
3291 | ;; "\\([^\"'`\n]*\\)" ; 3 + 1 | |
3292 | ;; "\\3" | |
3293 | ;; "\\|" | |
3294 | ;; ;; Second variant: Identifier or \ID or empty | |
3295 | ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1 | |
3296 | ;; ;; Do not have <<= or << 30 or <<30 or << $blah. | |
3297 | ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1 | |
3298 | ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1 | |
3299 | ;; "\\)" | |
f83d2997 KH |
3300 | ((match-beginning 2) ; 1 + 1 |
3301 | ;; Abort in comment: | |
3302 | (setq b (point)) | |
3303 | (setq state (parse-partial-sexp state-point b nil nil state) | |
5bd52f0e RS |
3304 | state-point b |
3305 | tb (match-beginning 0) | |
3306 | i (or (nth 3 state) (nth 4 state))) | |
5c8b7eaf | 3307 | (if i |
5bd52f0e RS |
3308 | (setq c t) |
3309 | (setq c (and | |
3310 | (match-beginning 5) | |
3311 | (not (match-beginning 6)) ; Empty | |
3312 | (looking-at | |
3313 | "[ \t]*[=0-9$@%&(]")))) | |
3314 | (if c ; Not here-doc | |
3315 | nil ; Skip it. | |
f83d2997 KH |
3316 | (if (match-beginning 5) ;4 + 1 |
3317 | (setq b1 (match-beginning 5) ; 4 + 1 | |
3318 | e1 (match-end 5)) ; 4 + 1 | |
3319 | (setq b1 (match-beginning 4) ; 3 + 1 | |
3320 | e1 (match-end 4))) ; 3 + 1 | |
3321 | (setq tag (buffer-substring b1 e1) | |
3322 | qtag (regexp-quote tag)) | |
5c8b7eaf | 3323 | (cond (cperl-pod-here-fontify |
5bd52f0e RS |
3324 | ;; Highlight the starting delimiter |
3325 | (cperl-postpone-fontification b1 e1 'face font-lock-constant-face) | |
3326 | (cperl-put-do-not-fontify b1 e1 t))) | |
f83d2997 KH |
3327 | (forward-line) |
3328 | (setq b (point)) | |
db133cb6 RS |
3329 | ;; We do not search to max, since we may be called from |
3330 | ;; some hook of fontification, and max is random | |
5c8b7eaf | 3331 | (cond ((re-search-forward (concat "^" qtag "$") |
db133cb6 | 3332 | stop-point 'toend) |
5c8b7eaf | 3333 | (if cperl-pod-here-fontify |
f83d2997 | 3334 | (progn |
5bd52f0e | 3335 | ;; Highlight the ending delimiter |
5c8b7eaf | 3336 | (cperl-postpone-fontification (match-beginning 0) (match-end 0) |
883212ce | 3337 | 'face font-lock-constant-face) |
5bd52f0e RS |
3338 | (cperl-put-do-not-fontify b (match-end 0) t) |
3339 | ;; Highlight the HERE-DOC | |
5c8b7eaf | 3340 | (cperl-postpone-fontification b (match-beginning 0) |
f83d2997 KH |
3341 | 'face here-face))) |
3342 | (setq e1 (cperl-1+ (match-end 0))) | |
5c8b7eaf | 3343 | (put-text-property b (match-beginning 0) |
f83d2997 KH |
3344 | 'syntax-type 'here-doc) |
3345 | (put-text-property (match-beginning 0) e1 | |
3346 | 'syntax-type 'here-doc-delim) | |
3347 | (put-text-property b e1 | |
3348 | 'here-doc-group t) | |
3349 | (cperl-commentify b e1 nil) | |
5bd52f0e RS |
3350 | (cperl-put-do-not-fontify b (match-end 0) t) |
3351 | (if (> e1 max) | |
3352 | (setq tmpend tb))) | |
f83d2997 KH |
3353 | (t (message "End of here-document `%s' not found." tag) |
3354 | (or (car err-l) (setcar err-l b)))))) | |
3355 | ;; format | |
3356 | ((match-beginning 8) | |
3357 | ;; 1+6=7 extra () before this: | |
3358 | ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" | |
3359 | (setq b (point) | |
3360 | name (if (match-beginning 8) ; 7 + 1 | |
3361 | (buffer-substring (match-beginning 8) ; 7 + 1 | |
3362 | (match-end 8)) ; 7 + 1 | |
5bd52f0e RS |
3363 | "") |
3364 | tb (match-beginning 0)) | |
f83d2997 | 3365 | (setq argument nil) |
5c8b7eaf | 3366 | (if cperl-pod-here-fontify |
f83d2997 KH |
3367 | (while (and (eq (forward-line) 0) |
3368 | (not (looking-at "^[.;]$"))) | |
3369 | (cond | |
3370 | ((looking-at "^#")) ; Skip comments | |
3371 | ((and argument ; Skip argument multi-lines | |
5c8b7eaf | 3372 | (looking-at "^[ \t]*{")) |
f83d2997 KH |
3373 | (forward-sexp 1) |
3374 | (setq argument nil)) | |
3375 | (argument ; Skip argument lines | |
3376 | (setq argument nil)) | |
3377 | (t ; Format line | |
3378 | (setq b1 (point)) | |
3379 | (setq argument (looking-at "^[^\n]*[@^]")) | |
3380 | (end-of-line) | |
5bd52f0e | 3381 | ;; Highlight the format line |
5c8b7eaf | 3382 | (cperl-postpone-fontification b1 (point) |
f83d2997 KH |
3383 | 'face font-lock-string-face) |
3384 | (cperl-commentify b1 (point) nil) | |
5bd52f0e | 3385 | (cperl-put-do-not-fontify b1 (point) t)))) |
db133cb6 RS |
3386 | ;; We do not search to max, since we may be called from |
3387 | ;; some hook of fontification, and max is random | |
3388 | (re-search-forward "^[.;]$" stop-point 'toend)) | |
f83d2997 | 3389 | (beginning-of-line) |
5bd52f0e | 3390 | (if (looking-at "^\\.$") ; ";" is not supported yet |
f83d2997 | 3391 | (progn |
5bd52f0e RS |
3392 | ;; Highlight the ending delimiter |
3393 | (cperl-postpone-fontification (point) (+ (point) 2) | |
f83d2997 KH |
3394 | 'face font-lock-string-face) |
3395 | (cperl-commentify (point) (+ (point) 2) nil) | |
5bd52f0e | 3396 | (cperl-put-do-not-fontify (point) (+ (point) 2) t)) |
f83d2997 KH |
3397 | (message "End of format `%s' not found." name) |
3398 | (or (car err-l) (setcar err-l b))) | |
3399 | (forward-line) | |
5bd52f0e RS |
3400 | (if (> (point) max) |
3401 | (setq tmpend tb)) | |
db133cb6 | 3402 | (put-text-property b (point) 'syntax-type 'format)) |
f83d2997 KH |
3403 | ;; Regexp: |
3404 | ((or (match-beginning 10) (match-beginning 11)) | |
3405 | ;; 1+6+2=9 extra () before this: | |
5bd52f0e | 3406 | ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" |
f83d2997 | 3407 | ;; "\\|" |
5bd52f0e | 3408 | ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob> |
f83d2997 KH |
3409 | (setq b1 (if (match-beginning 10) 10 11) |
3410 | argument (buffer-substring | |
3411 | (match-beginning b1) (match-end b1)) | |
3412 | b (point) | |
3413 | i b | |
3414 | c (char-after (match-beginning b1)) | |
3415 | bb (char-after (1- (match-beginning b1))) ; tmp holder | |
5bd52f0e RS |
3416 | ;; bb == "Not a stringy" |
3417 | bb (if (eq b1 10) ; user variables/whatever | |
6c389151 SM |
3418 | (or |
3419 | (memq bb '(?\$ ?\@ ?\% ?\* ?\#)) ; $#y | |
3420 | (and (eq bb ?-) (eq c ?s)) ; -s file test | |
3421 | (and (eq bb ?\&) | |
3422 | (not (eq (char-after ; &&m/blah/ | |
3423 | (- (match-beginning b1) 2)) | |
5bd52f0e RS |
3424 | ?\&)))) |
3425 | ;; <file> or <$file> | |
3426 | (and (eq c ?\<) | |
6c389151 | 3427 | ;; Do not stringify <FH>, <$fh> : |
5bd52f0e | 3428 | (save-match-data |
5c8b7eaf | 3429 | (looking-at |
6c389151 | 3430 | "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>")))) |
5bd52f0e | 3431 | tb (match-beginning 0)) |
db133cb6 RS |
3432 | (goto-char (match-beginning b1)) |
3433 | (cperl-backward-to-noncomment (point-min)) | |
f83d2997 | 3434 | (or bb |
5bd52f0e | 3435 | (if (eq b1 11) ; bare /blah/ or ?blah? or <foo> |
f83d2997 | 3436 | (setq argument "" |
db133cb6 RS |
3437 | bb ; Not a regexp? |
3438 | (progn | |
5c8b7eaf | 3439 | (not |
db133cb6 RS |
3440 | ;; What is below: regexp-p? |
3441 | (and | |
3442 | (or (memq (preceding-char) | |
5bd52f0e | 3443 | (append (if (memq c '(?\? ?\<)) |
db133cb6 | 3444 | ;; $a++ ? 1 : 2 |
5bd52f0e RS |
3445 | "~{(=|&*!,;:" |
3446 | "~{(=|&+-*!,;:") nil)) | |
db133cb6 RS |
3447 | (and (eq (preceding-char) ?\}) |
3448 | (cperl-after-block-p (point-min))) | |
3449 | (and (eq (char-syntax (preceding-char)) ?w) | |
3450 | (progn | |
3451 | (forward-sexp -1) | |
6c389151 SM |
3452 | ;; After these keywords `/' starts a RE. One should add all the |
3453 | ;; functions/builtins which expect an argument, but ... | |
5bd52f0e RS |
3454 | (if (eq (preceding-char) ?-) |
3455 | ;; -d ?foo? is a RE | |
3456 | (looking-at "[a-zA-Z]\\>") | |
029cb4d5 SM |
3457 | (and |
3458 | (not (memq (preceding-char) | |
3459 | '(?$ ?@ ?& ?%))) | |
3460 | (looking-at | |
3461 | "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>"))))) | |
db133cb6 RS |
3462 | (and (eq (preceding-char) ?.) |
3463 | (eq (char-after (- (point) 2)) ?.)) | |
3464 | (bobp)) | |
3465 | ;; m|blah| ? foo : bar; | |
3466 | (not | |
3467 | (and (eq c ?\?) | |
5c8b7eaf | 3468 | cperl-use-syntax-table-text-property |
db133cb6 RS |
3469 | (not (bobp)) |
3470 | (progn | |
3471 | (forward-char -1) | |
3472 | (looking-at "\\s|"))))))) | |
3473 | b (1- b)) | |
3474 | ;; s y tr m | |
3475 | ;; Check for $a->y | |
3476 | (if (and (eq (preceding-char) ?>) | |
3477 | (eq (char-after (- (point) 2)) ?-)) | |
3478 | ;; Not a regexp | |
3479 | (setq bb t)))) | |
5c8b7eaf | 3480 | (or bb (setq state (parse-partial-sexp |
f83d2997 KH |
3481 | state-point b nil nil state) |
3482 | state-point b)) | |
3483 | (goto-char b) | |
3484 | (if (or bb (nth 3 state) (nth 4 state)) | |
3485 | (goto-char i) | |
6c389151 | 3486 | ;; Skip whitespace and comments... |
f83d2997 KH |
3487 | (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+") |
3488 | (goto-char (match-end 0)) | |
3489 | (skip-chars-forward " \t\n\f")) | |
6c389151 SM |
3490 | (if (> (point) b) |
3491 | (put-text-property b (point) 'syntax-type 'prestring)) | |
f83d2997 KH |
3492 | ;; qtag means two-arg matcher, may be reset to |
3493 | ;; 2 or 3 later if some special quoting is needed. | |
3494 | ;; e1 means matching-char matcher. | |
3495 | (setq b (point) | |
5bd52f0e RS |
3496 | ;; has 2 args |
3497 | i2 (string-match "^\\([sy]\\|tr\\)$" argument) | |
db133cb6 RS |
3498 | ;; We do not search to max, since we may be called from |
3499 | ;; some hook of fontification, and max is random | |
3500 | i (cperl-forward-re stop-point end | |
5bd52f0e | 3501 | i2 |
db133cb6 | 3502 | t st-l err-l argument) |
5bd52f0e RS |
3503 | ;; Note that if `go', then it is considered as 1-arg |
3504 | b1 (nth 1 i) ; start of the second part | |
5c8b7eaf | 3505 | tag (nth 2 i) ; ender-char, true if second part |
5bd52f0e | 3506 | ; is with matching chars [] |
f83d2997 KH |
3507 | go (nth 4 i) ; There is a 1-char part after the end |
3508 | i (car i) ; intermediate point | |
5c8b7eaf | 3509 | e1 (point) ; end |
5bd52f0e | 3510 | ;; Before end of the second part if non-matching: /// |
5c8b7eaf | 3511 | tail (if (and i (not tag)) |
5bd52f0e RS |
3512 | (1- e1)) |
3513 | e (if i i e1) ; end of the first part | |
6c389151 SM |
3514 | qtag nil ; need to preserve backslashitis |
3515 | is-x-REx nil) ; REx has //x modifier | |
f83d2997 KH |
3516 | ;; Commenting \\ is dangerous, what about ( ? |
3517 | (and i tail | |
3518 | (eq (char-after i) ?\\) | |
5bd52f0e | 3519 | (setq qtag t)) |
6c389151 SM |
3520 | (if (looking-at "\\sw*x") ; qr//x |
3521 | (setq is-x-REx t)) | |
f83d2997 | 3522 | (if (null i) |
5bd52f0e | 3523 | ;; Considered as 1arg form |
f83d2997 KH |
3524 | (progn |
3525 | (cperl-commentify b (point) t) | |
5bd52f0e | 3526 | (put-text-property b (point) 'syntax-type 'string) |
6c389151 SM |
3527 | (if (or is-x-REx |
3528 | ;; ignore other text properties: | |
3529 | (string-match "^qw$" argument)) | |
3530 | (put-text-property b (point) 'indentable t)) | |
5bd52f0e RS |
3531 | (and go |
3532 | (setq e1 (cperl-1+ e1)) | |
3533 | (or (eobp) | |
3534 | (forward-char 1)))) | |
f83d2997 KH |
3535 | (cperl-commentify b i t) |
3536 | (if (looking-at "\\sw*e") ; s///e | |
3537 | (progn | |
3538 | (and | |
3539 | ;; silent: | |
5bd52f0e | 3540 | (cperl-find-pods-heres b1 (1- (point)) t end) |
f83d2997 KH |
3541 | ;; Error |
3542 | (goto-char (1+ max))) | |
5bd52f0e | 3543 | (if (and tag (eq (preceding-char) ?\>)) |
f83d2997 KH |
3544 | (progn |
3545 | (cperl-modify-syntax-type (1- (point)) cperl-st-ket) | |
5bd52f0e | 3546 | (cperl-modify-syntax-type i cperl-st-bra))) |
6c389151 SM |
3547 | (put-text-property b i 'syntax-type 'string) |
3548 | (if is-x-REx | |
3549 | (put-text-property b i 'indentable t))) | |
5bd52f0e RS |
3550 | (cperl-commentify b1 (point) t) |
3551 | (put-text-property b (point) 'syntax-type 'string) | |
6c389151 SM |
3552 | (if is-x-REx |
3553 | (put-text-property b i 'indentable t)) | |
5bd52f0e | 3554 | (if qtag |
db133cb6 | 3555 | (cperl-modify-syntax-type (1+ i) cperl-st-punct)) |
f83d2997 | 3556 | (setq tail nil))) |
5bd52f0e | 3557 | ;; Now: tail: if the second part is non-matching without ///e |
f83d2997 KH |
3558 | (if (eq (char-syntax (following-char)) ?w) |
3559 | (progn | |
3560 | (forward-word 1) ; skip modifiers s///s | |
5bd52f0e | 3561 | (if tail (cperl-commentify tail (point) t)) |
6c389151 SM |
3562 | (cperl-postpone-fontification |
3563 | e1 (point) 'face 'cperl-nonoverridable-face))) | |
5bd52f0e RS |
3564 | ;; Check whether it is m// which means "previous match" |
3565 | ;; and highlight differently | |
6c389151 SM |
3566 | (setq is-REx |
3567 | (and (string-match "^\\([sm]?\\|qr\\)$" argument) | |
3568 | (or (not (= (length argument) 0)) | |
3569 | (not (eq c ?\<))))) | |
3570 | (if (and is-REx | |
3571 | (eq e (+ 2 b)) | |
5bd52f0e RS |
3572 | ;; split // *is* using zero-pattern |
3573 | (save-excursion | |
3574 | (condition-case nil | |
3575 | (progn | |
3576 | (goto-char tb) | |
3577 | (forward-sexp -1) | |
3578 | (not (looking-at "split\\>"))) | |
3579 | (error t)))) | |
5c8b7eaf | 3580 | (cperl-postpone-fontification |
5bd52f0e RS |
3581 | b e 'face font-lock-function-name-face) |
3582 | (if (or i2 ; Has 2 args | |
3583 | (and cperl-fontify-m-as-s | |
3584 | (or | |
3585 | (string-match "^\\(m\\|qr\\)$" argument) | |
3586 | (and (eq 0 (length argument)) | |
3587 | (not (eq ?\< (char-after b))))))) | |
3588 | (progn | |
5c8b7eaf | 3589 | (cperl-postpone-fontification |
5bd52f0e | 3590 | b (cperl-1+ b) 'face font-lock-constant-face) |
5c8b7eaf | 3591 | (cperl-postpone-fontification |
6c389151 SM |
3592 | (1- e) e 'face font-lock-constant-face))) |
3593 | (if (and is-REx cperl-regexp-scan) | |
3594 | ;; Process RExen better | |
3595 | (save-excursion | |
3596 | (goto-char (1+ b)) | |
3597 | (while | |
3598 | (and (< (point) e) | |
3599 | (re-search-forward | |
3600 | (if is-x-REx | |
3601 | (if (eq (char-after b) ?\#) | |
3602 | "\\((\\?\\\\#\\)\\|\\(\\\\#\\)" | |
3603 | "\\((\\?#\\)\\|\\(#\\)") | |
3604 | (if (eq (char-after b) ?\#) | |
3605 | "\\((\\?\\\\#\\)" | |
3606 | "\\((\\?#\\)")) | |
3607 | (1- e) 'to-end)) | |
3608 | (goto-char (match-beginning 0)) | |
3609 | (setq REx-comment-start (point) | |
3610 | was-comment t) | |
3611 | (if (save-excursion | |
3612 | (and | |
3613 | ;; XXX not working if outside delimiter is # | |
3614 | (eq (preceding-char) ?\\) | |
3615 | (= (% (skip-chars-backward "$\\\\") 2) -1))) | |
3616 | ;; Not a comment, avoid loop: | |
3617 | (progn (setq was-comment nil) | |
3618 | (forward-char 1)) | |
3619 | (if (match-beginning 2) | |
3620 | (progn | |
3621 | (beginning-of-line 2) | |
3622 | (if (> (point) e) | |
3623 | (goto-char (1- e)))) | |
3624 | ;; Works also if the outside delimiters are (). | |
3625 | (or (search-forward ")" (1- e) 'toend) | |
3626 | (message | |
3627 | "Couldn't find end of (?#...)-comment in a REx, pos=%s" | |
3628 | REx-comment-start)))) | |
3629 | (if (>= (point) e) | |
3630 | (goto-char (1- e))) | |
3631 | (if was-comment | |
3632 | (progn | |
3633 | (setq REx-comment-end (point)) | |
3634 | (cperl-commentify | |
3635 | REx-comment-start REx-comment-end nil) | |
3636 | (cperl-postpone-fontification | |
3637 | REx-comment-start REx-comment-end | |
3638 | 'face font-lock-comment-face)))))) | |
3639 | (if (and is-REx is-x-REx) | |
3640 | (put-text-property (1+ b) (1- e) | |
3641 | 'syntax-subtype 'x-REx))) | |
5bd52f0e RS |
3642 | (if i2 |
3643 | (progn | |
5c8b7eaf | 3644 | (cperl-postpone-fontification |
5bd52f0e RS |
3645 | (1- e1) e1 'face font-lock-constant-face) |
3646 | (if (assoc (char-after b) cperl-starters) | |
5c8b7eaf | 3647 | (cperl-postpone-fontification |
5bd52f0e RS |
3648 | b1 (1+ b1) 'face font-lock-constant-face)))) |
3649 | (if (> (point) max) | |
3650 | (setq tmpend tb)))) | |
f83d2997 KH |
3651 | ((match-beginning 13) ; sub with prototypes |
3652 | (setq b (match-beginning 0)) | |
3653 | (if (memq (char-after (1- b)) | |
3654 | '(?\$ ?\@ ?\% ?\& ?\*)) | |
3655 | nil | |
5c8b7eaf | 3656 | (setq state (parse-partial-sexp |
5bd52f0e RS |
3657 | state-point b nil nil state) |
3658 | state-point b) | |
f83d2997 KH |
3659 | (if (or (nth 3 state) (nth 4 state)) |
3660 | nil | |
3661 | ;; Mark as string | |
3662 | (cperl-commentify (match-beginning 13) (match-end 13) t)) | |
3663 | (goto-char (match-end 0)))) | |
3664 | ;; 1+6+2+1+1+2=13 extra () before this: | |
3665 | ;; "\\$\\(['{]\\)" | |
3666 | ((and (match-beginning 14) | |
db133cb6 | 3667 | (eq (preceding-char) ?\')) ; $' |
f83d2997 | 3668 | (setq b (1- (point)) |
5c8b7eaf | 3669 | state (parse-partial-sexp |
f83d2997 KH |
3670 | state-point (1- b) nil nil state) |
3671 | state-point (1- b)) | |
3672 | (if (nth 3 state) ; in string | |
3673 | (cperl-modify-syntax-type (1- b) cperl-st-punct)) | |
3674 | (goto-char (1+ b))) | |
3675 | ;; 1+6+2+1+1+2=13 extra () before this: | |
3676 | ;; "\\$\\(['{]\\)" | |
3677 | ((match-beginning 14) ; ${ | |
3678 | (setq bb (match-beginning 0)) | |
3679 | (cperl-modify-syntax-type bb cperl-st-punct)) | |
3680 | ;; 1+6+2+1+1+2+1=14 extra () before this: | |
3681 | ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'") | |
3682 | ((match-beginning 15) ; old $abc'efg syntax | |
3683 | (setq bb (match-end 0) | |
3684 | b (match-beginning 0) | |
5c8b7eaf | 3685 | state (parse-partial-sexp |
f83d2997 KH |
3686 | state-point b nil nil state) |
3687 | state-point b) | |
3688 | (if (nth 3 state) ; in string | |
3689 | nil | |
3690 | (put-text-property (1- bb) bb 'syntax-table cperl-st-word)) | |
3691 | (goto-char bb)) | |
3692 | ;; 1+6+2+1+1+2+1+1=15 extra () before this: | |
3693 | ;; "__\\(END\\|DATA\\)__" | |
6c389151 | 3694 | ((match-beginning 16) ; __END__, __DATA__ |
f83d2997 KH |
3695 | (setq bb (match-end 0) |
3696 | b (match-beginning 0) | |
5c8b7eaf | 3697 | state (parse-partial-sexp |
f83d2997 KH |
3698 | state-point b nil nil state) |
3699 | state-point b) | |
3700 | (if (or (nth 3 state) (nth 4 state)) | |
3701 | nil | |
3702 | ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat | |
3703 | (cperl-commentify b bb nil) | |
3704 | (setq end t)) | |
6c389151 SM |
3705 | (goto-char bb)) |
3706 | ((match-beginning 17) ; "\\\\\\(['`\"]\\)" | |
3707 | (setq bb (match-end 0) | |
3708 | b (match-beginning 0)) | |
3709 | (goto-char b) | |
3710 | (skip-chars-backward "\\\\") | |
3711 | ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1)) | |
3712 | (setq state (parse-partial-sexp | |
3713 | state-point b nil nil state) | |
3714 | state-point b) | |
3715 | (if (or (nth 3 state) (nth 4 state) ) | |
3716 | nil | |
3717 | (cperl-modify-syntax-type b cperl-st-punct)) | |
3718 | (goto-char bb)) | |
3719 | (t (error "Error in regexp of the sniffer"))) | |
db133cb6 | 3720 | (if (> (point) stop-point) |
f83d2997 | 3721 | (progn |
5c8b7eaf | 3722 | (if end |
f83d2997 KH |
3723 | (message "Garbage after __END__/__DATA__ ignored") |
3724 | (message "Unbalanced syntax found while scanning") | |
3725 | (or (car err-l) (setcar err-l b))) | |
db133cb6 RS |
3726 | (goto-char stop-point)))) |
3727 | (setq cperl-syntax-state (cons state-point state) | |
5bd52f0e | 3728 | cperl-syntax-done-to (or tmpend (max (point) max)))) |
f83d2997 | 3729 | (if (car err-l) (goto-char (car err-l)) |
db133cb6 RS |
3730 | (or non-inter |
3731 | (message "Scanning for \"hard\" Perl constructions... done")))) | |
f83d2997 KH |
3732 | (and (buffer-modified-p) |
3733 | (not modified) | |
3734 | (set-buffer-modified-p nil)) | |
3735 | (set-syntax-table cperl-mode-syntax-table)) | |
3736 | (car err-l))) | |
3737 | ||
3738 | (defun cperl-backward-to-noncomment (lim) | |
3739 | ;; Stops at lim or after non-whitespace that is not in comment | |
5bd52f0e | 3740 | (let (stop p pr) |
f83d2997 KH |
3741 | (while (and (not stop) (> (point) (or lim 1))) |
3742 | (skip-chars-backward " \t\n\f" lim) | |
3743 | (setq p (point)) | |
3744 | (beginning-of-line) | |
5bd52f0e RS |
3745 | (if (memq (setq pr (get-text-property (point) 'syntax-type)) |
3746 | '(pod here-doc here-doc-delim)) | |
3747 | (cperl-unwind-to-safe nil) | |
f83d2997 KH |
3748 | (if (or (looking-at "^[ \t]*\\(#\\|$\\)") |
3749 | (progn (cperl-to-comment-or-eol) (bolp))) | |
3750 | nil ; Only comment, skip | |
3751 | ;; Else | |
3752 | (skip-chars-backward " \t") | |
3753 | (if (< p (point)) (goto-char p)) | |
5bd52f0e | 3754 | (setq stop t)))))) |
f83d2997 KH |
3755 | |
3756 | (defun cperl-after-block-p (lim) | |
3757 | ;; We suppose that the preceding char is }. | |
3758 | (save-excursion | |
3759 | (condition-case nil | |
3760 | (progn | |
3761 | (forward-sexp -1) | |
3762 | (cperl-backward-to-noncomment lim) | |
bab27c0c RS |
3763 | (or (eq (point) lim) |
3764 | (eq (preceding-char) ?\) ) ; if () {} sub f () {} | |
db133cb6 RS |
3765 | (if (eq (char-syntax (preceding-char)) ?w) ; else {} |
3766 | (save-excursion | |
3767 | (forward-sexp -1) | |
6c389151 | 3768 | (or (looking-at "\\(else\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>") |
db133cb6 RS |
3769 | ;; sub f {} |
3770 | (progn | |
3771 | (cperl-backward-to-noncomment lim) | |
3772 | (and (eq (char-syntax (preceding-char)) ?w) | |
3773 | (progn | |
3774 | (forward-sexp -1) | |
3775 | (looking-at "sub\\>")))))) | |
3776 | (cperl-after-expr-p lim)))) | |
f83d2997 KH |
3777 | (error nil)))) |
3778 | ||
3779 | (defun cperl-after-expr-p (&optional lim chars test) | |
029cb4d5 | 3780 | "Return true if the position is good for start of expression. |
f83d2997 KH |
3781 | TEST is the expression to evaluate at the found position. If absent, |
3782 | CHARS is a string that contains good characters to have before us (however, | |
3783 | `}' is treated \"smartly\" if it is not in the list)." | |
5c8b7eaf | 3784 | (let (stop p |
f83d2997 KH |
3785 | (lim (or lim (point-min)))) |
3786 | (save-excursion | |
3787 | (while (and (not stop) (> (point) lim)) | |
3788 | (skip-chars-backward " \t\n\f" lim) | |
3789 | (setq p (point)) | |
3790 | (beginning-of-line) | |
3791 | (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip | |
5bd52f0e | 3792 | ;; Else: last iteration, or a label |
5c8b7eaf | 3793 | (cperl-to-comment-or-eol) |
f83d2997 KH |
3794 | (skip-chars-backward " \t") |
3795 | (if (< p (point)) (goto-char p)) | |
5bd52f0e RS |
3796 | (setq p (point)) |
3797 | (if (and (eq (preceding-char) ?:) | |
3798 | (progn | |
3799 | (forward-char -1) | |
3800 | (skip-chars-backward " \t\n\f" lim) | |
3801 | (eq (char-syntax (preceding-char)) ?w))) | |
3802 | (forward-sexp -1) ; Possibly label. Skip it | |
3803 | (goto-char p) | |
3804 | (setq stop t)))) | |
bab27c0c RS |
3805 | (or (bobp) ; ???? Needed |
3806 | (eq (point) lim) | |
029cb4d5 | 3807 | (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes |
f83d2997 KH |
3808 | (progn |
3809 | (if test (eval test) | |
3810 | (or (memq (preceding-char) (append (or chars "{;") nil)) | |
3811 | (and (eq (preceding-char) ?\}) | |
3812 | (cperl-after-block-p lim))))))))) | |
3813 | ||
3814 | (defun cperl-backward-to-start-of-continued-exp (lim) | |
3815 | (if (memq (preceding-char) (append ")]}\"'`" nil)) | |
3816 | (forward-sexp -1)) | |
3817 | (beginning-of-line) | |
3818 | (if (<= (point) lim) | |
3819 | (goto-char (1+ lim))) | |
3820 | (skip-chars-forward " \t")) | |
3821 | ||
db133cb6 RS |
3822 | (defun cperl-after-block-and-statement-beg (lim) |
3823 | ;; We assume that we are after ?\} | |
5c8b7eaf | 3824 | (and |
db133cb6 RS |
3825 | (cperl-after-block-p lim) |
3826 | (save-excursion | |
3827 | (forward-sexp -1) | |
3828 | (cperl-backward-to-noncomment (point-min)) | |
3829 | (or (bobp) | |
bab27c0c | 3830 | (eq (point) lim) |
db133cb6 RS |
3831 | (not (= (char-syntax (preceding-char)) ?w)) |
3832 | (progn | |
3833 | (forward-sexp -1) | |
5c8b7eaf | 3834 | (not |
db133cb6 RS |
3835 | (looking-at |
3836 | "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>"))))))) | |
3837 | ||
f83d2997 | 3838 | \f |
f83d2997 KH |
3839 | (defun cperl-indent-exp () |
3840 | "Simple variant of indentation of continued-sexp. | |
5bd52f0e RS |
3841 | |
3842 | Will not indent comment if it starts at `comment-indent' or looks like | |
3843 | continuation of the comment on the previous line. | |
db133cb6 | 3844 | |
5c8b7eaf | 3845 | If `cperl-indent-region-fix-constructs', will improve spacing on |
db133cb6 | 3846 | conditional/loop constructs." |
f83d2997 KH |
3847 | (interactive) |
3848 | (save-excursion | |
3849 | (let ((tmp-end (progn (end-of-line) (point))) top done) | |
3850 | (save-excursion | |
3851 | (beginning-of-line) | |
3852 | (while (null done) | |
3853 | (setq top (point)) | |
3854 | (while (= (nth 0 (parse-partial-sexp (point) tmp-end | |
3855 | -1)) -1) | |
3856 | (setq top (point))) ; Get the outermost parenths in line | |
3857 | (goto-char top) | |
3858 | (while (< (point) tmp-end) | |
3859 | (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol | |
3860 | (or (eolp) (forward-sexp 1))) | |
5bd52f0e RS |
3861 | (if (> (point) tmp-end) |
3862 | (save-excursion | |
3863 | (end-of-line) | |
3864 | (setq tmp-end (point))) | |
f83d2997 KH |
3865 | (setq done t))) |
3866 | (goto-char tmp-end) | |
3867 | (setq tmp-end (point-marker))) | |
db133cb6 RS |
3868 | (if cperl-indent-region-fix-constructs |
3869 | (cperl-fix-line-spacing tmp-end)) | |
f83d2997 KH |
3870 | (cperl-indent-region (point) tmp-end)))) |
3871 | ||
5bd52f0e RS |
3872 | (defun cperl-fix-line-spacing (&optional end parse-data) |
3873 | "Improve whitespace in a conditional/loop construct. | |
3874 | Returns some position at the last line." | |
db133cb6 RS |
3875 | (interactive) |
3876 | (or end | |
3877 | (setq end (point-max))) | |
5bd52f0e RS |
3878 | (let (p pp ml have-brace ret |
3879 | (ee (save-excursion (end-of-line) (point))) | |
db133cb6 RS |
3880 | (cperl-indent-region-fix-constructs |
3881 | (or cperl-indent-region-fix-constructs 1))) | |
3882 | (save-excursion | |
3883 | (beginning-of-line) | |
5bd52f0e | 3884 | (setq ret (point)) |
5c8b7eaf | 3885 | ;; }? continue |
5bd52f0e | 3886 | ;; blah; } |
5c8b7eaf | 3887 | (if (not |
5bd52f0e RS |
3888 | (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)") |
3889 | (setq have-brace (save-excursion (search-forward "}" ee t))))) | |
3890 | nil ; Do not need to do anything | |
db133cb6 | 3891 | ;; Looking at: |
5c8b7eaf | 3892 | ;; } |
db133cb6 RS |
3893 | ;; else |
3894 | (if (and cperl-merge-trailing-else | |
3895 | (looking-at | |
3896 | "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")) | |
3897 | (progn | |
3898 | (search-forward "}") | |
3899 | (setq p (point)) | |
3900 | (skip-chars-forward " \t\n") | |
3901 | (delete-region p (point)) | |
3902 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3903 | (beginning-of-line))) | |
3904 | ;; Looking at: | |
3905 | ;; } else | |
3906 | (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>") | |
3907 | (progn | |
3908 | (search-forward "}") | |
3909 | (delete-horizontal-space) | |
3910 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3911 | (beginning-of-line))) | |
3912 | ;; Looking at: | |
3913 | ;; else { | |
5c8b7eaf | 3914 | (if (looking-at |
5bd52f0e | 3915 | "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]") |
db133cb6 RS |
3916 | (progn |
3917 | (forward-word 1) | |
3918 | (delete-horizontal-space) | |
3919 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3920 | (beginning-of-line))) | |
3921 | ;; Looking at: | |
3922 | ;; foreach my $var | |
6c389151 SM |
3923 | (if (looking-at |
3924 | "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]") | |
db133cb6 RS |
3925 | (progn |
3926 | (forward-word 2) | |
3927 | (delete-horizontal-space) | |
3928 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3929 | (beginning-of-line))) | |
3930 | ;; Looking at: | |
3931 | ;; foreach my $var ( | |
6c389151 SM |
3932 | (if (looking-at |
3933 | "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]") | |
db133cb6 RS |
3934 | (progn |
3935 | (forward-word 3) | |
3936 | (delete-horizontal-space) | |
3937 | (insert | |
3938 | (make-string cperl-indent-region-fix-constructs ?\ )) | |
3939 | (beginning-of-line))) | |
3940 | ;; Looking at: | |
3941 | ;; } foreach my $var () { | |
6c389151 | 3942 | (if (looking-at |
ce22dd53 | 3943 | "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{") |
db133cb6 RS |
3944 | (progn |
3945 | (setq ml (match-beginning 8)) | |
3946 | (re-search-forward "[({]") | |
3947 | (forward-char -1) | |
3948 | (setq p (point)) | |
3949 | (if (eq (following-char) ?\( ) | |
3950 | (progn | |
3951 | (forward-sexp 1) | |
3952 | (setq pp (point))) | |
3953 | ;; after `else' or nothing | |
3954 | (if ml ; after `else' | |
3955 | (skip-chars-backward " \t\n") | |
3956 | (beginning-of-line)) | |
3957 | (setq pp nil)) | |
3958 | ;; Now after the sexp before the brace | |
3959 | ;; Multiline expr should be special | |
3960 | (setq ml (and pp (save-excursion (goto-char p) | |
3961 | (search-forward "\n" pp t)))) | |
3962 | (if (and (or (not pp) (< pp end)) | |
3963 | (looking-at "[ \t\n]*{")) | |
3964 | (progn | |
5c8b7eaf | 3965 | (cond |
db133cb6 RS |
3966 | ((bolp) ; Were before `{', no if/else/etc |
3967 | nil) | |
3968 | ((looking-at "\\(\t*\\| [ \t]+\\){") | |
3969 | (delete-horizontal-space) | |
5c8b7eaf | 3970 | (if (if ml |
db133cb6 RS |
3971 | cperl-extra-newline-before-brace-multiline |
3972 | cperl-extra-newline-before-brace) | |
3973 | (progn | |
3974 | (delete-horizontal-space) | |
3975 | (insert "\n") | |
5bd52f0e RS |
3976 | (setq ret (point)) |
3977 | (if (cperl-indent-line parse-data) | |
5c8b7eaf | 3978 | (progn |
5bd52f0e RS |
3979 | (cperl-fix-line-spacing end parse-data) |
3980 | (setq ret (point))))) | |
db133cb6 RS |
3981 | (insert |
3982 | (make-string cperl-indent-region-fix-constructs ?\ )))) | |
3983 | ((and (looking-at "[ \t]*\n") | |
5c8b7eaf | 3984 | (not (if ml |
db133cb6 RS |
3985 | cperl-extra-newline-before-brace-multiline |
3986 | cperl-extra-newline-before-brace))) | |
3987 | (setq pp (point)) | |
3988 | (skip-chars-forward " \t\n") | |
3989 | (delete-region pp (point)) | |
3990 | (insert | |
3991 | (make-string cperl-indent-region-fix-constructs ?\ )))) | |
3992 | ;; Now we are before `{' | |
3993 | (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]") | |
3994 | (progn | |
3995 | (skip-chars-forward " \t\n") | |
3996 | (setq pp (point)) | |
3997 | (forward-sexp 1) | |
3998 | (setq p (point)) | |
3999 | (goto-char pp) | |
4000 | (setq ml (search-forward "\n" p t)) | |
4001 | (if (or cperl-break-one-line-blocks-when-indent ml) | |
4002 | ;; not good: multi-line BLOCK | |
4003 | (progn | |
4004 | (goto-char (1+ pp)) | |
4005 | (delete-horizontal-space) | |
4006 | (insert "\n") | |
5bd52f0e RS |
4007 | (setq ret (point)) |
4008 | (if (cperl-indent-line parse-data) | |
4009 | (setq ret (cperl-fix-line-spacing end parse-data))))))))))) | |
db133cb6 | 4010 | (beginning-of-line) |
5bd52f0e | 4011 | (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee. |
db133cb6 RS |
4012 | ;; Now check whether there is a hanging `}' |
4013 | ;; Looking at: | |
4014 | ;; } blah | |
5c8b7eaf | 4015 | (if (and |
db133cb6 | 4016 | cperl-fix-hanging-brace-when-indent |
5bd52f0e | 4017 | have-brace |
db133cb6 RS |
4018 | (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)")) |
4019 | (condition-case nil | |
4020 | (progn | |
4021 | (up-list 1) | |
5c8b7eaf | 4022 | (if (and (<= (point) pp) |
db133cb6 | 4023 | (eq (preceding-char) ?\} ) |
5c8b7eaf | 4024 | (cperl-after-block-and-statement-beg (point-min))) |
db133cb6 RS |
4025 | t |
4026 | (goto-char p) | |
4027 | nil)) | |
4028 | (error nil))) | |
4029 | (progn | |
4030 | (forward-char -1) | |
4031 | (skip-chars-backward " \t") | |
4032 | (if (bolp) | |
4033 | ;; `}' was the first thing on the line, insert NL *after* it. | |
4034 | (progn | |
5bd52f0e | 4035 | (cperl-indent-line parse-data) |
db133cb6 RS |
4036 | (search-forward "}") |
4037 | (delete-horizontal-space) | |
4038 | (insert "\n")) | |
4039 | (delete-horizontal-space) | |
4040 | (or (eq (preceding-char) ?\;) | |
4041 | (bolp) | |
4042 | (and (eq (preceding-char) ?\} ) | |
4043 | (cperl-after-block-p (point-min))) | |
4044 | (insert ";")) | |
5bd52f0e RS |
4045 | (insert "\n") |
4046 | (setq ret (point))) | |
4047 | (if (cperl-indent-line parse-data) | |
4048 | (setq ret (cperl-fix-line-spacing end parse-data))) | |
4049 | (beginning-of-line))))) | |
4050 | ret)) | |
4051 | ||
4052 | (defvar cperl-update-start) ; Do not need to make them local | |
4053 | (defvar cperl-update-end) | |
4054 | (defun cperl-delay-update-hook (beg end old-len) | |
4055 | (setq cperl-update-start (min beg (or cperl-update-start (point-max)))) | |
4056 | (setq cperl-update-end (max end (or cperl-update-end (point-min))))) | |
db133cb6 | 4057 | |
f83d2997 KH |
4058 | (defun cperl-indent-region (start end) |
4059 | "Simple variant of indentation of region in CPerl mode. | |
5c8b7eaf | 4060 | Should be slow. Will not indent comment if it starts at `comment-indent' |
f83d2997 | 4061 | or looks like continuation of the comment on the previous line. |
5c8b7eaf SS |
4062 | Indents all the lines whose first character is between START and END |
4063 | inclusive. | |
db133cb6 | 4064 | |
5c8b7eaf | 4065 | If `cperl-indent-region-fix-constructs', will improve spacing on |
db133cb6 | 4066 | conditional/loop constructs." |
f83d2997 | 4067 | (interactive "r") |
5bd52f0e | 4068 | (cperl-update-syntaxification end end) |
f83d2997 | 4069 | (save-excursion |
5bd52f0e RS |
4070 | (let (cperl-update-start cperl-update-end (h-a-c after-change-functions)) |
4071 | (let (st comm old-comm-indent new-comm-indent p pp i empty | |
4072 | (indent-info (if cperl-emacs-can-parse | |
4073 | (list nil nil nil) ; Cannot use '(), since will modify | |
4074 | nil)) | |
4075 | after-change-functions ; Speed it up! | |
f83d2997 | 4076 | (pm 0) (imenu-scanning-message "Indenting... (%3d%%)")) |
5bd52f0e | 4077 | (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook)) |
f83d2997 KH |
4078 | (goto-char start) |
4079 | (setq old-comm-indent (and (cperl-to-comment-or-eol) | |
4080 | (current-column)) | |
4081 | new-comm-indent old-comm-indent) | |
4082 | (goto-char start) | |
bab27c0c | 4083 | (setq end (set-marker (make-marker) end)) ; indentation changes pos |
f83d2997 KH |
4084 | (or (bolp) (beginning-of-line 2)) |
4085 | (or (fboundp 'imenu-progress-message) | |
4086 | (message "Indenting... For feedback load `imenu'...")) | |
4087 | (while (and (<= (point) end) (not (eobp))) ; bol to check start | |
4088 | (and (fboundp 'imenu-progress-message) | |
5c8b7eaf | 4089 | (imenu-progress-message |
f83d2997 | 4090 | pm (/ (* 100 (- (point) start)) (- end start -1)))) |
5bd52f0e RS |
4091 | (setq st (point)) |
4092 | (if (or | |
4093 | (setq empty (looking-at "[ \t]*\n")) | |
4094 | (and (setq comm (looking-at "[ \t]*#")) | |
5c8b7eaf | 4095 | (or (eq (current-indentation) (or old-comm-indent |
f83d2997 | 4096 | comment-column)) |
5bd52f0e | 4097 | (setq old-comm-indent nil)))) |
f83d2997 | 4098 | (if (and old-comm-indent |
5bd52f0e | 4099 | (not empty) |
f83d2997 | 4100 | (= (current-indentation) old-comm-indent) |
5bd52f0e RS |
4101 | (not (eq (get-text-property (point) 'syntax-type) 'pod)) |
4102 | (not (eq (get-text-property (point) 'syntax-table) | |
4103 | cperl-st-cfence))) | |
f83d2997 KH |
4104 | (let ((comment-column new-comm-indent)) |
4105 | (indent-for-comment))) | |
5c8b7eaf | 4106 | (progn |
5bd52f0e | 4107 | (setq i (cperl-indent-line indent-info)) |
f83d2997 | 4108 | (or comm |
db133cb6 | 4109 | (not i) |
f83d2997 | 4110 | (progn |
db133cb6 | 4111 | (if cperl-indent-region-fix-constructs |
5bd52f0e | 4112 | (goto-char (cperl-fix-line-spacing end indent-info))) |
5c8b7eaf | 4113 | (if (setq old-comm-indent |
f83d2997 | 4114 | (and (cperl-to-comment-or-eol) |
5c8b7eaf | 4115 | (not (memq (get-text-property (point) |
f83d2997 KH |
4116 | 'syntax-type) |
4117 | '(pod here-doc))) | |
5c8b7eaf | 4118 | (not (eq (get-text-property (point) |
5bd52f0e RS |
4119 | 'syntax-table) |
4120 | cperl-st-cfence)) | |
f83d2997 KH |
4121 | (current-column))) |
4122 | (progn (indent-for-comment) | |
4123 | (skip-chars-backward " \t") | |
4124 | (skip-chars-backward "#") | |
4125 | (setq new-comm-indent (current-column)))))))) | |
4126 | (beginning-of-line 2)) | |
4127 | (if (fboundp 'imenu-progress-message) | |
4128 | (imenu-progress-message pm 100) | |
5bd52f0e RS |
4129 | (message nil))) |
4130 | ;; Now run the update hooks | |
4131 | (if after-change-functions | |
4132 | (save-excursion | |
4133 | (if cperl-update-end | |
4134 | (progn | |
4135 | (goto-char cperl-update-end) | |
4136 | (insert " ") | |
4137 | (delete-char -1) | |
4138 | (goto-char cperl-update-start) | |
4139 | (insert " ") | |
4140 | (delete-char -1)))))))) | |
f83d2997 | 4141 | |
f83d2997 KH |
4142 | ;; Stolen from lisp-mode with a lot of improvements |
4143 | ||
4144 | (defun cperl-fill-paragraph (&optional justify iteration) | |
4145 | "Like \\[fill-paragraph], but handle CPerl comments. | |
4146 | If any of the current line is a comment, fill the comment or the | |
4147 | block of it that point is in, preserving the comment's initial | |
4148 | indentation and initial hashes. Behaves usually outside of comment." | |
4149 | (interactive "P") | |
4150 | (let ( | |
4151 | ;; Non-nil if the current line contains a comment. | |
4152 | has-comment | |
4153 | ||
4154 | ;; If has-comment, the appropriate fill-prefix for the comment. | |
4155 | comment-fill-prefix | |
4156 | ;; Line that contains code and comment (or nil) | |
4157 | start | |
4158 | c spaces len dc (comment-column comment-column)) | |
4159 | ;; Figure out what kind of comment we are looking at. | |
4160 | (save-excursion | |
4161 | (beginning-of-line) | |
4162 | (cond | |
4163 | ||
4164 | ;; A line with nothing but a comment on it? | |
4165 | ((looking-at "[ \t]*#[# \t]*") | |
4166 | (setq has-comment t | |
4167 | comment-fill-prefix (buffer-substring (match-beginning 0) | |
4168 | (match-end 0)))) | |
4169 | ||
4170 | ;; A line with some code, followed by a comment? Remember that the | |
4171 | ;; semi which starts the comment shouldn't be part of a string or | |
4172 | ;; character. | |
4173 | ((cperl-to-comment-or-eol) | |
4174 | (setq has-comment t) | |
4175 | (looking-at "#+[ \t]*") | |
5c8b7eaf | 4176 | (setq start (point) c (current-column) |
f83d2997 KH |
4177 | comment-fill-prefix |
4178 | (concat (make-string (current-column) ?\ ) | |
4179 | (buffer-substring (match-beginning 0) (match-end 0))) | |
5c8b7eaf | 4180 | spaces (progn (skip-chars-backward " \t") |
f83d2997 | 4181 | (buffer-substring (point) start)) |
5c8b7eaf | 4182 | dc (- c (current-column)) len (- start (point)) |
f83d2997 KH |
4183 | start (point-marker)) |
4184 | (delete-char len) | |
4185 | (insert (make-string dc ?-))))) | |
4186 | (if (not has-comment) | |
4187 | (fill-paragraph justify) ; Do the usual thing outside of comment | |
4188 | ;; Narrow to include only the comment, and then fill the region. | |
4189 | (save-restriction | |
4190 | (narrow-to-region | |
4191 | ;; Find the first line we should include in the region to fill. | |
4192 | (if start (progn (beginning-of-line) (point)) | |
4193 | (save-excursion | |
4194 | (while (and (zerop (forward-line -1)) | |
4195 | (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]"))) | |
4196 | ;; We may have gone to far. Go forward again. | |
4197 | (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]") | |
4198 | (forward-line 1)) | |
4199 | (point))) | |
4200 | ;; Find the beginning of the first line past the region to fill. | |
4201 | (save-excursion | |
4202 | (while (progn (forward-line 1) | |
4203 | (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]"))) | |
4204 | (point))) | |
4205 | ;; Remove existing hashes | |
4206 | (goto-char (point-min)) | |
4207 | (while (progn (forward-line 1) (< (point) (point-max))) | |
4208 | (skip-chars-forward " \t") | |
5c8b7eaf | 4209 | (and (looking-at "#+") |
f83d2997 KH |
4210 | (delete-char (- (match-end 0) (match-beginning 0))))) |
4211 | ||
4212 | ;; Lines with only hashes on them can be paragraph boundaries. | |
4213 | (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$")) | |
4214 | (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$")) | |
4215 | (fill-prefix comment-fill-prefix)) | |
4216 | (fill-paragraph justify))) | |
4217 | (if (and start) | |
5c8b7eaf | 4218 | (progn |
f83d2997 KH |
4219 | (goto-char start) |
4220 | (if (> dc 0) | |
4221 | (progn (delete-char dc) (insert spaces))) | |
4222 | (if (or (= (current-column) c) iteration) nil | |
4223 | (setq comment-column c) | |
4224 | (indent-for-comment) | |
4225 | ;; Repeat once more, flagging as iteration | |
4226 | (cperl-fill-paragraph justify t))))))) | |
4227 | ||
4228 | (defun cperl-do-auto-fill () | |
4229 | ;; Break out if the line is short enough | |
4230 | (if (> (save-excursion | |
4231 | (end-of-line) | |
4232 | (current-column)) | |
4233 | fill-column) | |
4234 | (let ((c (save-excursion (beginning-of-line) | |
4235 | (cperl-to-comment-or-eol) (point))) | |
4236 | (s (memq (following-char) '(?\ ?\t))) marker) | |
4237 | (if (>= c (point)) nil | |
4238 | (setq marker (point-marker)) | |
4239 | (cperl-fill-paragraph) | |
4240 | (goto-char marker) | |
4241 | ;; Is not enough, sometimes marker is a start of line | |
5c8b7eaf | 4242 | (if (bolp) (progn (re-search-forward "#+[ \t]*") |
f83d2997 KH |
4243 | (goto-char (match-end 0)))) |
4244 | ;; Following space could have gone: | |
4245 | (if (or (not s) (memq (following-char) '(?\ ?\t))) nil | |
4246 | (insert " ") | |
4247 | (backward-char 1)) | |
4248 | ;; Previous space could have gone: | |
4249 | (or (memq (preceding-char) '(?\ ?\t)) (insert " ")))))) | |
4250 | ||
f83d2997 KH |
4251 | (defun cperl-imenu-addback (lst &optional isback name) |
4252 | ;; We suppose that the lst is a DAG, unless the first element only | |
4253 | ;; loops back, and ISBACK is set. Thus this function cannot be | |
4254 | ;; applied twice without ISBACK set. | |
4255 | (cond ((not cperl-imenu-addback) lst) | |
4256 | (t | |
5c8b7eaf | 4257 | (or name |
f83d2997 KH |
4258 | (setq name "+++BACK+++")) |
4259 | (mapcar (function (lambda (elt) | |
4260 | (if (and (listp elt) (listp (cdr elt))) | |
4261 | (progn | |
4262 | ;; In the other order it goes up | |
4263 | ;; one level only ;-( | |
4264 | (setcdr elt (cons (cons name lst) | |
4265 | (cdr elt))) | |
4266 | (cperl-imenu-addback (cdr elt) t name) | |
4267 | )))) | |
4268 | (if isback (cdr lst) lst)) | |
4269 | lst))) | |
4270 | ||
80585273 | 4271 | (defun cperl-imenu--create-perl-index (&optional regexp) |
f83d2997 | 4272 | (require 'imenu) ; May be called from TAGS creator |
5c8b7eaf | 4273 | (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '()) |
f83d2997 KH |
4274 | (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function)) |
4275 | (index-meth-alist '()) meth | |
6c389151 | 4276 | packages ends-ranges p marker |
f83d2997 KH |
4277 | (prev-pos 0) char fchar index index1 name (end-range 0) package) |
4278 | (goto-char (point-min)) | |
4279 | (if noninteractive | |
4280 | (message "Scanning Perl for index") | |
4281 | (imenu-progress-message prev-pos 0)) | |
6c389151 | 4282 | (cperl-update-syntaxification (point-max) (point-max)) |
f83d2997 KH |
4283 | ;; Search for the function |
4284 | (progn ;;save-match-data | |
4285 | (while (re-search-forward | |
80585273 | 4286 | (or regexp cperl-imenu--function-name-regexp-perl) |
f83d2997 KH |
4287 | nil t) |
4288 | (or noninteractive | |
4289 | (imenu-progress-message prev-pos)) | |
f83d2997 KH |
4290 | (cond |
4291 | ((and ; Skip some noise if building tags | |
4292 | (match-beginning 2) ; package or sub | |
4293 | (eq (char-after (match-beginning 2)) ?p) ; package | |
4294 | (not (save-match-data | |
4295 | (looking-at "[ \t\n]*;")))) ; Plain text word 'package' | |
4296 | nil) | |
4297 | ((and | |
4298 | (match-beginning 2) ; package or sub | |
6c389151 | 4299 | ;; Skip if quoted (will not skip multi-line ''-strings :-(): |
f83d2997 KH |
4300 | (null (get-text-property (match-beginning 1) 'syntax-table)) |
4301 | (null (get-text-property (match-beginning 1) 'syntax-type)) | |
4302 | (null (get-text-property (match-beginning 1) 'in-pod))) | |
4303 | (save-excursion | |
4304 | (goto-char (match-beginning 2)) | |
4305 | (setq fchar (following-char)) | |
4306 | ) | |
4307 | ;; (if (looking-at "([^()]*)[ \t\n\f]*") | |
4308 | ;; (goto-char (match-end 0))) ; Messes what follows | |
6c389151 | 4309 | (setq char (following-char) ; ?\; for "sub foo () ;" |
f83d2997 KH |
4310 | meth nil |
4311 | p (point)) | |
4312 | (while (and ends-ranges (>= p (car ends-ranges))) | |
4313 | ;; delete obsolete entries | |
4314 | (setq ends-ranges (cdr ends-ranges) packages (cdr packages))) | |
4315 | (setq package (or (car packages) "") | |
4316 | end-range (or (car ends-ranges) 0)) | |
4317 | (if (eq fchar ?p) | |
4318 | (setq name (buffer-substring (match-beginning 3) (match-end 3)) | |
4319 | name (progn | |
4320 | (set-text-properties 0 (length name) nil name) | |
4321 | name) | |
5c8b7eaf | 4322 | package (concat name "::") |
f83d2997 | 4323 | name (concat "package " name) |
5c8b7eaf | 4324 | end-range |
f83d2997 KH |
4325 | (save-excursion |
4326 | (parse-partial-sexp (point) (point-max) -1) (point)) | |
4327 | ends-ranges (cons end-range ends-ranges) | |
4328 | packages (cons package packages))) | |
4329 | ;; ) | |
4330 | ;; Skip this function name if it is a prototype declaration. | |
4331 | (if (and (eq fchar ?s) (eq char ?\;)) nil | |
6c389151 SM |
4332 | (setq name (buffer-substring (match-beginning 3) (match-end 3)) |
4333 | marker (make-marker)) | |
4334 | (set-text-properties 0 (length name) nil name) | |
4335 | (set-marker marker (match-end 3)) | |
4336 | (if (eq fchar ?p) | |
4337 | (setq name (concat "package " name)) | |
f83d2997 KH |
4338 | (cond ((string-match "[:']" name) |
4339 | (setq meth t)) | |
4340 | ((> p end-range) nil) | |
5c8b7eaf | 4341 | (t |
f83d2997 | 4342 | (setq name (concat package name) meth t)))) |
6c389151 SM |
4343 | (setq index (cons name marker)) |
4344 | (if (eq fchar ?p) | |
f83d2997 KH |
4345 | (push index index-pack-alist) |
4346 | (push index index-alist)) | |
4347 | (if meth (push index index-meth-alist)) | |
4348 | (push index index-unsorted-alist))) | |
4349 | ((match-beginning 5) ; Pod section | |
4350 | ;; (beginning-of-line) | |
4351 | (setq index (imenu-example--name-and-position) | |
4352 | name (buffer-substring (match-beginning 6) (match-end 6))) | |
4353 | (set-text-properties 0 (length name) nil name) | |
4354 | (if (eq (char-after (match-beginning 5)) ?2) | |
4355 | (setq name (concat " " name))) | |
4356 | (setcar index name) | |
4357 | (setq index1 (cons (concat "=" name) (cdr index))) | |
4358 | (push index index-pod-alist) | |
4359 | (push index1 index-unsorted-alist))))) | |
4360 | (or noninteractive | |
4361 | (imenu-progress-message prev-pos 100)) | |
5c8b7eaf | 4362 | (setq index-alist |
f83d2997 KH |
4363 | (if (default-value 'imenu-sort-function) |
4364 | (sort index-alist (default-value 'imenu-sort-function)) | |
4365 | (nreverse index-alist))) | |
4366 | (and index-pod-alist | |
4367 | (push (cons "+POD headers+..." | |
4368 | (nreverse index-pod-alist)) | |
4369 | index-alist)) | |
4370 | (and (or index-pack-alist index-meth-alist) | |
4371 | (let ((lst index-pack-alist) hier-list pack elt group name) | |
4372 | ;; Remove "package ", reverse and uniquify. | |
4373 | (while lst | |
4374 | (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8)) | |
4375 | (if (assoc name hier-list) nil | |
4376 | (setq hier-list (cons (cons name (cdr elt)) hier-list)))) | |
4377 | (setq lst index-meth-alist) | |
4378 | (while lst | |
4379 | (setq elt (car lst) lst (cdr lst)) | |
4380 | (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt)) | |
4381 | (setq pack (substring (car elt) 0 (match-beginning 0))) | |
5c8b7eaf | 4382 | (if (setq group (assoc pack hier-list)) |
f83d2997 KH |
4383 | (if (listp (cdr group)) |
4384 | ;; Have some functions already | |
5c8b7eaf SS |
4385 | (setcdr group |
4386 | (cons (cons (substring | |
f83d2997 KH |
4387 | (car elt) |
4388 | (+ 2 (match-beginning 0))) | |
4389 | (cdr elt)) | |
4390 | (cdr group))) | |
5c8b7eaf | 4391 | (setcdr group (list (cons (substring |
f83d2997 KH |
4392 | (car elt) |
4393 | (+ 2 (match-beginning 0))) | |
4394 | (cdr elt))))) | |
5c8b7eaf SS |
4395 | (setq hier-list |
4396 | (cons (cons pack | |
4397 | (list (cons (substring | |
f83d2997 KH |
4398 | (car elt) |
4399 | (+ 2 (match-beginning 0))) | |
4400 | (cdr elt)))) | |
4401 | hier-list)))))) | |
4402 | (push (cons "+Hierarchy+..." | |
4403 | hier-list) | |
4404 | index-alist))) | |
4405 | (and index-pack-alist | |
4406 | (push (cons "+Packages+..." | |
4407 | (nreverse index-pack-alist)) | |
4408 | index-alist)) | |
5c8b7eaf | 4409 | (and (or index-pack-alist index-pod-alist |
f83d2997 KH |
4410 | (default-value 'imenu-sort-function)) |
4411 | index-unsorted-alist | |
4412 | (push (cons "+Unsorted List+..." | |
4413 | (nreverse index-unsorted-alist)) | |
4414 | index-alist)) | |
4415 | (cperl-imenu-addback index-alist))) | |
4416 | ||
6c389151 | 4417 | \f |
6c389151 SM |
4418 | ;; Suggested by Mark A. Hershberger |
4419 | (defun cperl-outline-level () | |
4420 | (looking-at outline-regexp) | |
4421 | (cond ((not (match-beginning 1)) 0) ; beginning-of-file | |
4422 | ((match-beginning 2) | |
4423 | (if (eq (char-after (match-beginning 2)) ?p) | |
4424 | 0 ; package | |
4425 | 1)) ; sub | |
4426 | ((match-beginning 5) | |
4427 | (if (eq (char-after (match-beginning 5)) ?1) | |
4428 | 1 ; head1 | |
4429 | 2)) ; head2 | |
4430 | (t 3))) ; should not happen | |
4431 | ||
4432 | \f | |
4433 | (defvar cperl-compilation-error-regexp-alist | |
f83d2997 KH |
4434 | ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK). |
4435 | '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]" | |
4436 | 2 3)) | |
4437 | "Alist that specifies how to match errors in perl output.") | |
4438 | ||
4439 | (if (fboundp 'eval-after-load) | |
4440 | (eval-after-load | |
4441 | "mode-compile" | |
4442 | '(setq perl-compilation-error-regexp-alist | |
4443 | cperl-compilation-error-regexp-alist))) | |
4444 | ||
4445 | ||
f83d2997 KH |
4446 | (defun cperl-windowed-init () |
4447 | "Initialization under windowed version." | |
db133cb6 RS |
4448 | (if (or (featurep 'ps-print) cperl-faces-init) |
4449 | ;; Need to init anyway: | |
4450 | (or cperl-faces-init (cperl-init-faces)) | |
4451 | (add-hook 'font-lock-mode-hook | |
4452 | (function | |
4453 | (lambda () | |
996e2616 | 4454 | (if (memq major-mode '(perl-mode cperl-mode)) |
db133cb6 RS |
4455 | (progn |
4456 | (or cperl-faces-init (cperl-init-faces))))))) | |
4457 | (if (fboundp 'eval-after-load) | |
4458 | (eval-after-load | |
4459 | "ps-print" | |
4460 | '(or cperl-faces-init (cperl-init-faces)))))) | |
4461 | ||
5efe6a56 | 4462 | (defvar cperl-font-lock-keywords-1 nil |
80585273 | 4463 | "Additional expressions to highlight in Perl mode. Minimal set.") |
5efe6a56 | 4464 | (defvar cperl-font-lock-keywords nil |
80585273 | 4465 | "Additional expressions to highlight in Perl mode. Default set.") |
5efe6a56 | 4466 | (defvar cperl-font-lock-keywords-2 nil |
80585273 DL |
4467 | "Additional expressions to highlight in Perl mode. Maximal set") |
4468 | ||
db133cb6 RS |
4469 | (defun cperl-load-font-lock-keywords () |
4470 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4471 | cperl-font-lock-keywords) |
db133cb6 RS |
4472 | |
4473 | (defun cperl-load-font-lock-keywords-1 () | |
4474 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4475 | cperl-font-lock-keywords-1) |
db133cb6 RS |
4476 | |
4477 | (defun cperl-load-font-lock-keywords-2 () | |
4478 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4479 | cperl-font-lock-keywords-2) |
f83d2997 | 4480 | |
5bd52f0e RS |
4481 | (defun cperl-init-faces-weak () |
4482 | ;; Allow `cperl-find-pods-heres' to run. | |
4483 | (or (boundp 'font-lock-constant-face) | |
4484 | (cperl-force-face font-lock-constant-face | |
4485 | "Face for constant and label names") | |
4486 | ;;(setq font-lock-constant-face 'font-lock-constant-face) | |
4487 | )) | |
4488 | ||
f83d2997 | 4489 | (defun cperl-init-faces () |
5bd52f0e | 4490 | (condition-case errs |
f83d2997 KH |
4491 | (progn |
4492 | (require 'font-lock) | |
4493 | (and (fboundp 'font-lock-fontify-anchored-keywords) | |
4494 | (featurep 'font-lock-extra) | |
4495 | (message "You have an obsolete package `font-lock-extra'. Install `choose-color'.")) | |
4496 | (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored) | |
f83d2997 KH |
4497 | (if (fboundp 'font-lock-fontify-anchored-keywords) |
4498 | (setq font-lock-anchored t)) | |
5c8b7eaf | 4499 | (setq |
f83d2997 KH |
4500 | t-font-lock-keywords |
4501 | (list | |
ac6857fb | 4502 | `("[ \t]+$" 0 ',cperl-invalid-face t) |
f83d2997 KH |
4503 | (cons |
4504 | (concat | |
4505 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
4506 | (mapconcat | |
4507 | 'identity | |
4508 | '("if" "until" "while" "elsif" "else" "unless" "for" | |
4509 | "foreach" "continue" "exit" "die" "last" "goto" "next" | |
4510 | "redo" "return" "local" "exec" "sub" "do" "dump" "use" | |
6c389151 | 4511 | "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT") |
f83d2997 KH |
4512 | "\\|") ; Flow control |
4513 | "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]" | |
4514 | ; In what follows we use `type' style | |
4515 | ; for overwritable builtins | |
4516 | (list | |
4517 | (concat | |
4518 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
4519 | ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm" | |
4520 | ;; "and" "atan2" "bind" "binmode" "bless" "caller" | |
4521 | ;; "chdir" "chmod" "chown" "chr" "chroot" "close" | |
4522 | ;; "closedir" "cmp" "connect" "continue" "cos" "crypt" | |
4523 | ;; "dbmclose" "dbmopen" "die" "dump" "endgrent" | |
4524 | ;; "endhostent" "endnetent" "endprotoent" "endpwent" | |
4525 | ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl" | |
4526 | ;; "fileno" "flock" "fork" "formline" "ge" "getc" | |
4527 | ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr" | |
4528 | ;; "gethostbyname" "gethostent" "getlogin" | |
4529 | ;; "getnetbyaddr" "getnetbyname" "getnetent" | |
4530 | ;; "getpeername" "getpgrp" "getppid" "getpriority" | |
4531 | ;; "getprotobyname" "getprotobynumber" "getprotoent" | |
4532 | ;; "getpwent" "getpwnam" "getpwuid" "getservbyname" | |
4533 | ;; "getservbyport" "getservent" "getsockname" | |
4534 | ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int" | |
4535 | ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length" | |
5bd52f0e | 4536 | ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt" |
f83d2997 KH |
4537 | ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne" |
4538 | ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe" | |
4539 | ;; "quotemeta" "rand" "read" "readdir" "readline" | |
4540 | ;; "readlink" "readpipe" "recv" "ref" "rename" "require" | |
4541 | ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek" | |
4542 | ;; "seekdir" "select" "semctl" "semget" "semop" "send" | |
4543 | ;; "setgrent" "sethostent" "setnetent" "setpgrp" | |
4544 | ;; "setpriority" "setprotoent" "setpwent" "setservent" | |
4545 | ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite" | |
4546 | ;; "shutdown" "sin" "sleep" "socket" "socketpair" | |
4547 | ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink" | |
6c389151 | 4548 | ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell" |
f83d2997 KH |
4549 | ;; "telldir" "time" "times" "truncate" "uc" "ucfirst" |
4550 | ;; "umask" "unlink" "unpack" "utime" "values" "vec" | |
4551 | ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor" | |
5c8b7eaf | 4552 | "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|" |
f83d2997 KH |
4553 | "b\\(in\\(d\\|mode\\)\\|less\\)\\|" |
4554 | "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|" | |
4555 | "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|" | |
4556 | "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|" | |
4557 | "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|" | |
4558 | "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|" | |
4559 | "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|" | |
4560 | "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|" | |
4561 | "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w" | |
4562 | "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|" | |
4563 | "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|" | |
4564 | "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|" | |
4565 | "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|" | |
4566 | "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|" | |
4567 | "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e" | |
5bd52f0e | 4568 | "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|" |
f83d2997 KH |
4569 | "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|" |
4570 | "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|" | |
4571 | "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin" | |
4572 | "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name" | |
4573 | "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r" | |
4574 | "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|" | |
4575 | "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|" | |
4576 | "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|" | |
6c389151 | 4577 | "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|" |
f83d2997 KH |
4578 | "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|" |
4579 | "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|" | |
4580 | "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|" | |
4581 | "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|" | |
4582 | "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)" | |
4583 | "\\)\\>") 2 'font-lock-type-face) | |
4584 | ;; In what follows we use `other' style | |
4585 | ;; for nonoverwritable builtins | |
4586 | ;; Somehow 's', 'm' are not auto-generated??? | |
4587 | (list | |
4588 | (concat | |
4589 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
6c389151 | 4590 | ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp" |
f83d2997 KH |
4591 | ;; "chop" "defined" "delete" "do" "each" "else" "elsif" |
4592 | ;; "eval" "exists" "for" "foreach" "format" "goto" | |
4593 | ;; "grep" "if" "keys" "last" "local" "map" "my" "next" | |
4594 | ;; "no" "package" "pop" "pos" "print" "printf" "push" | |
4595 | ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift" | |
4596 | ;; "sort" "splice" "split" "study" "sub" "tie" "tr" | |
4597 | ;; "undef" "unless" "unshift" "untie" "until" "use" | |
4598 | ;; "while" "y" | |
6c389151 | 4599 | "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|" |
f83d2997 | 4600 | "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|" |
6c389151 SM |
4601 | "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|" |
4602 | "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|" | |
f83d2997 | 4603 | "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|" |
5bd52f0e | 4604 | "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|" |
f83d2997 KH |
4605 | "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|" |
4606 | "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|" | |
4607 | "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually | |
4608 | "\\|[sm]" ; Added manually | |
5bd52f0e | 4609 | "\\)\\>") 2 'cperl-nonoverridable-face) |
f83d2997 KH |
4610 | ;; (mapconcat 'identity |
4611 | ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if" | |
4612 | ;; "#include" "#define" "#undef") | |
4613 | ;; "\\|") | |
4614 | '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0 | |
4615 | font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]" | |
5bd52f0e | 4616 | '("\\<sub[ \t]+\\([^ \t{;()]+\\)[ \t]*\\(([^()]*)[ \t]*\\)?[#{\n]" 1 |
f83d2997 KH |
4617 | font-lock-function-name-face) |
4618 | '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B; | |
4619 | 2 font-lock-function-name-face) | |
4620 | '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$" | |
4621 | 1 font-lock-function-name-face) | |
4622 | (cond ((featurep 'font-lock-extra) | |
5c8b7eaf | 4623 | '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" |
f83d2997 KH |
4624 | (2 font-lock-string-face t) |
4625 | (0 '(restart 2 t)))) ; To highlight $a{bc}{ef} | |
4626 | (font-lock-anchored | |
4627 | '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4628 | (2 font-lock-string-face t) | |
4629 | ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4630 | nil nil | |
4631 | (1 font-lock-string-face t)))) | |
4632 | (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4633 | 2 font-lock-string-face t))) | |
db133cb6 | 4634 | '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1 |
f83d2997 | 4635 | font-lock-string-face t) |
5c8b7eaf | 4636 | '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1 |
883212ce | 4637 | font-lock-constant-face) ; labels |
f83d2997 | 4638 | '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets |
883212ce | 4639 | 2 font-lock-constant-face) |
6c389151 SM |
4640 | ;; Uncomment to get perl-mode-like vars |
4641 | ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face) | |
4642 | ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)" | |
4643 | ;;; (2 (cons font-lock-variable-name-face '(underline)))) | |
f83d2997 | 4644 | (cond ((featurep 'font-lock-extra) |
6c389151 | 4645 | '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?" |
f83d2997 KH |
4646 | (3 font-lock-variable-name-face) |
4647 | (4 '(another 4 nil | |
4648 | ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?" | |
4649 | (1 font-lock-variable-name-face) | |
5c8b7eaf | 4650 | (2 '(restart 2 nil) nil t))) |
f83d2997 KH |
4651 | nil t))) ; local variables, multiple |
4652 | (font-lock-anchored | |
6c389151 | 4653 | '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)" |
f83d2997 KH |
4654 | (3 font-lock-variable-name-face) |
4655 | ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)" | |
4656 | nil nil | |
4657 | (1 font-lock-variable-name-face)))) | |
6c389151 | 4658 | (t '("^[ \t{}]*\\(my\\|local\\our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)" |
f83d2997 | 4659 | 3 font-lock-variable-name-face))) |
6c389151 SM |
4660 | '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*(" |
4661 | 4 font-lock-variable-name-face))) | |
4662 | (setq | |
f83d2997 KH |
4663 | t-font-lock-keywords-1 |
4664 | (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock | |
4665 | (not cperl-xemacs-p) ; not yet as of XEmacs 19.12 | |
4666 | '( | |
4667 | ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1 | |
4668 | (if (eq (char-after (match-beginning 2)) ?%) | |
5bd52f0e RS |
4669 | cperl-hash-face |
4670 | cperl-array-face) | |
f83d2997 KH |
4671 | t) ; arrays and hashes |
4672 | ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)" | |
4673 | 1 | |
5c8b7eaf | 4674 | (if (= (- (match-end 2) (match-beginning 2)) 1) |
f83d2997 | 4675 | (if (eq (char-after (match-beginning 3)) ?{) |
5bd52f0e RS |
4676 | cperl-hash-face |
4677 | cperl-array-face) ; arrays and hashes | |
f83d2997 KH |
4678 | font-lock-variable-name-face) ; Just to put something |
4679 | t) | |
4680 | ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2") | |
4681 | ;;; Too much noise from \s* @s[ and friends | |
5c8b7eaf | 4682 | ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)" |
f83d2997 KH |
4683 | ;;(3 font-lock-function-name-face t t) |
4684 | ;;(4 | |
4685 | ;; (if (cperl-slash-is-regexp) | |
4686 | ;; font-lock-function-name-face 'default) nil t)) | |
4687 | ))) | |
6c389151 SM |
4688 | (if cperl-highlight-variables-indiscriminately |
4689 | (setq t-font-lock-keywords-1 | |
4690 | (append t-font-lock-keywords-1 | |
4691 | (list '("[$*]{?\\(\\sw+\\)" 1 | |
4692 | font-lock-variable-name-face))))) | |
4693 | (setq cperl-font-lock-keywords-1 | |
5bd52f0e RS |
4694 | (if cperl-syntaxify-by-font-lock |
4695 | (cons 'cperl-fontify-update | |
4696 | t-font-lock-keywords) | |
4697 | t-font-lock-keywords) | |
5efe6a56 SM |
4698 | cperl-font-lock-keywords cperl-font-lock-keywords-1 |
4699 | cperl-font-lock-keywords-2 (append | |
6c389151 SM |
4700 | cperl-font-lock-keywords-1 |
4701 | t-font-lock-keywords-1))) | |
f83d2997 KH |
4702 | (if (fboundp 'ps-print-buffer) (cperl-ps-print-init)) |
4703 | (if (or (featurep 'choose-color) (featurep 'font-lock-extra)) | |
db133cb6 RS |
4704 | (eval ; Avoid a warning |
4705 | '(font-lock-require-faces | |
f83d2997 KH |
4706 | (list |
4707 | ;; Color-light Color-dark Gray-light Gray-dark Mono | |
4708 | (list 'font-lock-comment-face | |
4709 | ["Firebrick" "OrangeRed" "DimGray" "Gray80"] | |
4710 | nil | |
4711 | [nil nil t t t] | |
4712 | [nil nil t t t] | |
4713 | nil) | |
4714 | (list 'font-lock-string-face | |
4715 | ["RosyBrown" "LightSalmon" "Gray50" "LightGray"] | |
4716 | nil | |
4717 | nil | |
4718 | [nil nil t t t] | |
4719 | nil) | |
f83d2997 KH |
4720 | (list 'font-lock-function-name-face |
4721 | (vector | |
4722 | "Blue" "LightSkyBlue" "Gray50" "LightGray" | |
4723 | (cdr (assq 'background-color ; if mono | |
4724 | (frame-parameters)))) | |
4725 | (vector | |
4726 | nil nil nil nil | |
4727 | (cdr (assq 'foreground-color ; if mono | |
4728 | (frame-parameters)))) | |
4729 | [nil nil t t t] | |
4730 | nil | |
4731 | nil) | |
4732 | (list 'font-lock-variable-name-face | |
4733 | ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"] | |
4734 | nil | |
4735 | [nil nil t t t] | |
4736 | [nil nil t t t] | |
4737 | nil) | |
4738 | (list 'font-lock-type-face | |
4739 | ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"] | |
4740 | nil | |
4741 | [nil nil t t t] | |
4742 | nil | |
4743 | [nil nil t t t] | |
4744 | ) | |
883212ce | 4745 | (list 'font-lock-constant-face |
f83d2997 KH |
4746 | ["CadetBlue" "Aquamarine" "Gray50" "LightGray"] |
4747 | nil | |
4748 | [nil nil t t t] | |
4749 | nil | |
4750 | [nil nil t t t] | |
4751 | ) | |
5bd52f0e | 4752 | (list 'cperl-nonoverridable-face |
f83d2997 KH |
4753 | ["chartreuse3" ("orchid1" "orange") |
4754 | nil "Gray80"] | |
4755 | [nil nil "gray90"] | |
4756 | [nil nil nil t t] | |
4757 | [nil nil t t] | |
4758 | [nil nil t t t] | |
4759 | ) | |
5bd52f0e | 4760 | (list 'cperl-array-face |
f83d2997 KH |
4761 | ["blue" "yellow" nil "Gray80"] |
4762 | ["lightyellow2" ("navy" "os2blue" "darkgreen") | |
4763 | "gray90"] | |
4764 | t | |
4765 | nil | |
4766 | nil) | |
5bd52f0e | 4767 | (list 'cperl-hash-face |
f83d2997 KH |
4768 | ["red" "red" nil "Gray80"] |
4769 | ["lightyellow2" ("navy" "os2blue" "darkgreen") | |
4770 | "gray90"] | |
4771 | t | |
4772 | t | |
db133cb6 | 4773 | nil)))) |
5bd52f0e | 4774 | ;; Do it the dull way, without choose-color |
f83d2997 KH |
4775 | (defvar cperl-guessed-background nil |
4776 | "Display characteristics as guessed by cperl.") | |
5bd52f0e | 4777 | ;; (or (fboundp 'x-color-defined-p) |
5c8b7eaf | 4778 | ;; (defalias 'x-color-defined-p |
5bd52f0e RS |
4779 | ;; (cond ((fboundp 'color-defined-p) 'color-defined-p) |
4780 | ;; ;; XEmacs >= 19.12 | |
4781 | ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p) | |
4782 | ;; ;; XEmacs 19.11 | |
4783 | ;; (t 'x-valid-color-name-p)))) | |
5c8b7eaf | 4784 | (cperl-force-face font-lock-constant-face |
5bd52f0e RS |
4785 | "Face for constant and label names") |
4786 | (cperl-force-face font-lock-variable-name-face | |
4787 | "Face for variable names") | |
4788 | (cperl-force-face font-lock-type-face | |
4789 | "Face for data types") | |
4790 | (cperl-force-face cperl-nonoverridable-face | |
4791 | "Face for data types from another group") | |
4792 | (cperl-force-face font-lock-comment-face | |
4793 | "Face for comments") | |
4794 | (cperl-force-face font-lock-function-name-face | |
4795 | "Face for function names") | |
4796 | (cperl-force-face cperl-hash-face | |
4797 | "Face for hashes") | |
4798 | (cperl-force-face cperl-array-face | |
4799 | "Face for arrays") | |
4800 | ;;(defvar font-lock-constant-face 'font-lock-constant-face) | |
4801 | ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face) | |
4802 | ;;(or (boundp 'font-lock-type-face) | |
4803 | ;; (defconst font-lock-type-face | |
4804 | ;; 'font-lock-type-face | |
4805 | ;; "Face to use for data types.")) | |
4806 | ;;(or (boundp 'cperl-nonoverridable-face) | |
4807 | ;; (defconst cperl-nonoverridable-face | |
4808 | ;; 'cperl-nonoverridable-face | |
4809 | ;; "Face to use for data types from another group.")) | |
4810 | ;;(if (not cperl-xemacs-p) nil | |
4811 | ;; (or (boundp 'font-lock-comment-face) | |
4812 | ;; (defconst font-lock-comment-face | |
4813 | ;; 'font-lock-comment-face | |
4814 | ;; "Face to use for comments.")) | |
4815 | ;; (or (boundp 'font-lock-keyword-face) | |
4816 | ;; (defconst font-lock-keyword-face | |
4817 | ;; 'font-lock-keyword-face | |
4818 | ;; "Face to use for keywords.")) | |
4819 | ;; (or (boundp 'font-lock-function-name-face) | |
4820 | ;; (defconst font-lock-function-name-face | |
4821 | ;; 'font-lock-function-name-face | |
4822 | ;; "Face to use for function names."))) | |
4823 | (if (and | |
5c8b7eaf SS |
4824 | (not (cperl-is-face 'cperl-array-face)) |
4825 | (cperl-is-face 'font-lock-emphasized-face)) | |
5bd52f0e RS |
4826 | (copy-face 'font-lock-emphasized-face 'cperl-array-face)) |
4827 | (if (and | |
5c8b7eaf SS |
4828 | (not (cperl-is-face 'cperl-hash-face)) |
4829 | (cperl-is-face 'font-lock-other-emphasized-face)) | |
4830 | (copy-face 'font-lock-other-emphasized-face | |
5bd52f0e RS |
4831 | 'cperl-hash-face)) |
4832 | (if (and | |
5c8b7eaf SS |
4833 | (not (cperl-is-face 'cperl-nonoverridable-face)) |
4834 | (cperl-is-face 'font-lock-other-type-face)) | |
4835 | (copy-face 'font-lock-other-type-face | |
5bd52f0e RS |
4836 | 'cperl-nonoverridable-face)) |
4837 | ;;(or (boundp 'cperl-hash-face) | |
4838 | ;; (defconst cperl-hash-face | |
4839 | ;; 'cperl-hash-face | |
4840 | ;; "Face to use for hashes.")) | |
4841 | ;;(or (boundp 'cperl-array-face) | |
4842 | ;; (defconst cperl-array-face | |
4843 | ;; 'cperl-array-face | |
4844 | ;; "Face to use for arrays.")) | |
f83d2997 KH |
4845 | ;; Here we try to guess background |
4846 | (let ((background | |
4847 | (if (boundp 'font-lock-background-mode) | |
4848 | font-lock-background-mode | |
5c8b7eaf | 4849 | 'light)) |
f83d2997 | 4850 | (face-list (and (fboundp 'face-list) (face-list))) |
5bd52f0e RS |
4851 | ;; cperl-is-face |
4852 | ) | |
4853 | ;;;; (fset 'cperl-is-face | |
4854 | ;;;; (cond ((fboundp 'find-face) | |
4855 | ;;;; (symbol-function 'find-face)) | |
4856 | ;;;; (face-list | |
4857 | ;;;; (function (lambda (face) (member face face-list)))) | |
4858 | ;;;; (t | |
4859 | ;;;; (function (lambda (face) (boundp face)))))) | |
f83d2997 KH |
4860 | (defvar cperl-guessed-background |
4861 | (if (and (boundp 'font-lock-display-type) | |
4862 | (eq font-lock-display-type 'grayscale)) | |
4863 | 'gray | |
4864 | background) | |
4865 | "Background as guessed by CPerl mode") | |
5c8b7eaf SS |
4866 | (if (and |
4867 | (not (cperl-is-face 'font-lock-constant-face)) | |
4868 | (cperl-is-face 'font-lock-reference-face)) | |
db133cb6 RS |
4869 | (copy-face 'font-lock-reference-face 'font-lock-constant-face)) |
4870 | (if (cperl-is-face 'font-lock-type-face) nil | |
f83d2997 KH |
4871 | (copy-face 'default 'font-lock-type-face) |
4872 | (cond | |
4873 | ((eq background 'light) | |
4874 | (set-face-foreground 'font-lock-type-face | |
4875 | (if (x-color-defined-p "seagreen") | |
4876 | "seagreen" | |
4877 | "sea green"))) | |
4878 | ((eq background 'dark) | |
4879 | (set-face-foreground 'font-lock-type-face | |
4880 | (if (x-color-defined-p "os2pink") | |
4881 | "os2pink" | |
4882 | "pink"))) | |
4883 | (t | |
4884 | (set-face-background 'font-lock-type-face "gray90")))) | |
5bd52f0e | 4885 | (if (cperl-is-face 'cperl-nonoverridable-face) |
f83d2997 | 4886 | nil |
5bd52f0e | 4887 | (copy-face 'font-lock-type-face 'cperl-nonoverridable-face) |
f83d2997 KH |
4888 | (cond |
4889 | ((eq background 'light) | |
5bd52f0e | 4890 | (set-face-foreground 'cperl-nonoverridable-face |
f83d2997 KH |
4891 | (if (x-color-defined-p "chartreuse3") |
4892 | "chartreuse3" | |
4893 | "chartreuse"))) | |
4894 | ((eq background 'dark) | |
5bd52f0e | 4895 | (set-face-foreground 'cperl-nonoverridable-face |
f83d2997 KH |
4896 | (if (x-color-defined-p "orchid1") |
4897 | "orchid1" | |
4898 | "orange"))))) | |
5bd52f0e RS |
4899 | ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil |
4900 | ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face) | |
4901 | ;;; (cond | |
4902 | ;;; ((eq background 'light) | |
4903 | ;;; (set-face-background 'font-lock-other-emphasized-face | |
4904 | ;;; (if (x-color-defined-p "lightyellow2") | |
4905 | ;;; "lightyellow2" | |
4906 | ;;; (if (x-color-defined-p "lightyellow") | |
4907 | ;;; "lightyellow" | |
4908 | ;;; "light yellow")))) | |
4909 | ;;; ((eq background 'dark) | |
4910 | ;;; (set-face-background 'font-lock-other-emphasized-face | |
4911 | ;;; (if (x-color-defined-p "navy") | |
4912 | ;;; "navy" | |
4913 | ;;; (if (x-color-defined-p "darkgreen") | |
4914 | ;;; "darkgreen" | |
4915 | ;;; "dark green")))) | |
4916 | ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90")))) | |
4917 | ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil | |
4918 | ;;; (copy-face 'bold 'font-lock-emphasized-face) | |
4919 | ;;; (cond | |
4920 | ;;; ((eq background 'light) | |
4921 | ;;; (set-face-background 'font-lock-emphasized-face | |
4922 | ;;; (if (x-color-defined-p "lightyellow2") | |
4923 | ;;; "lightyellow2" | |
4924 | ;;; "lightyellow"))) | |
4925 | ;;; ((eq background 'dark) | |
4926 | ;;; (set-face-background 'font-lock-emphasized-face | |
4927 | ;;; (if (x-color-defined-p "navy") | |
4928 | ;;; "navy" | |
4929 | ;;; (if (x-color-defined-p "darkgreen") | |
4930 | ;;; "darkgreen" | |
4931 | ;;; "dark green")))) | |
4932 | ;;; (t (set-face-background 'font-lock-emphasized-face "gray90")))) | |
db133cb6 | 4933 | (if (cperl-is-face 'font-lock-variable-name-face) nil |
f83d2997 | 4934 | (copy-face 'italic 'font-lock-variable-name-face)) |
db133cb6 | 4935 | (if (cperl-is-face 'font-lock-constant-face) nil |
883212ce | 4936 | (copy-face 'italic 'font-lock-constant-face)))) |
f83d2997 | 4937 | (setq cperl-faces-init t)) |
5bd52f0e | 4938 | (error (message "cperl-init-faces (ignored): %s" errs)))) |
f83d2997 KH |
4939 | |
4940 | ||
4941 | (defun cperl-ps-print-init () | |
4942 | "Initialization of `ps-print' components for faces used in CPerl." | |
5bd52f0e RS |
4943 | (eval-after-load "ps-print" |
4944 | '(setq ps-bold-faces | |
5c8b7eaf | 4945 | ;; font-lock-variable-name-face |
5bd52f0e RS |
4946 | ;; font-lock-constant-face |
4947 | (append '(cperl-array-face | |
5c8b7eaf | 4948 | cperl-hash-face) |
5bd52f0e RS |
4949 | ps-bold-faces) |
4950 | ps-italic-faces | |
4951 | ;; font-lock-constant-face | |
4952 | (append '(cperl-nonoverridable-face | |
4953 | cperl-hash-face) | |
4954 | ps-italic-faces) | |
4955 | ps-underlined-faces | |
4956 | ;; font-lock-type-face | |
4957 | (append '(cperl-array-face | |
4958 | cperl-hash-face | |
4959 | underline | |
4960 | cperl-nonoverridable-face) | |
4961 | ps-underlined-faces)))) | |
4962 | ||
4963 | (defvar ps-print-face-extension-alist) | |
4964 | ||
4965 | (defun cperl-ps-print (&optional file) | |
4966 | "Pretty-print in CPerl style. | |
4967 | If optional argument FILE is an empty string, prints to printer, otherwise | |
4968 | to the file FILE. If FILE is nil, prompts for a file name. | |
4969 | ||
4970 | Style of printout regulated by the variable `cperl-ps-print-face-properties'." | |
4971 | (interactive) | |
5c8b7eaf SS |
4972 | (or file |
4973 | (setq file (read-from-minibuffer | |
5bd52f0e RS |
4974 | "Print to file (if empty - to printer): " |
4975 | (concat (buffer-file-name) ".ps") | |
4976 | nil nil 'file-name-history))) | |
4977 | (or (> (length file) 0) | |
4978 | (setq file nil)) | |
4979 | (require 'ps-print) ; To get ps-print-face-extension-alist | |
4980 | (let ((ps-print-color-p t) | |
4981 | (ps-print-face-extension-alist ps-print-face-extension-alist)) | |
4982 | (cperl-ps-extend-face-list cperl-ps-print-face-properties) | |
4983 | (ps-print-buffer-with-faces file))) | |
4984 | ||
4985 | ;;; (defun cperl-ps-print-init () | |
4986 | ;;; "Initialization of `ps-print' components for faces used in CPerl." | |
4987 | ;;; ;; Guard against old versions | |
4988 | ;;; (defvar ps-underlined-faces nil) | |
4989 | ;;; (defvar ps-bold-faces nil) | |
4990 | ;;; (defvar ps-italic-faces nil) | |
4991 | ;;; (setq ps-bold-faces | |
4992 | ;;; (append '(font-lock-emphasized-face | |
4993 | ;;; cperl-array-face | |
5c8b7eaf SS |
4994 | ;;; font-lock-keyword-face |
4995 | ;;; font-lock-variable-name-face | |
4996 | ;;; font-lock-constant-face | |
4997 | ;;; font-lock-reference-face | |
5bd52f0e | 4998 | ;;; font-lock-other-emphasized-face |
5c8b7eaf | 4999 | ;;; cperl-hash-face) |
5bd52f0e RS |
5000 | ;;; ps-bold-faces)) |
5001 | ;;; (setq ps-italic-faces | |
5002 | ;;; (append '(cperl-nonoverridable-face | |
5c8b7eaf SS |
5003 | ;;; font-lock-constant-face |
5004 | ;;; font-lock-reference-face | |
5bd52f0e RS |
5005 | ;;; font-lock-other-emphasized-face |
5006 | ;;; cperl-hash-face) | |
5007 | ;;; ps-italic-faces)) | |
5008 | ;;; (setq ps-underlined-faces | |
5009 | ;;; (append '(font-lock-emphasized-face | |
5010 | ;;; cperl-array-face | |
5011 | ;;; font-lock-other-emphasized-face | |
5012 | ;;; cperl-hash-face | |
5013 | ;;; cperl-nonoverridable-face font-lock-type-face) | |
5014 | ;;; ps-underlined-faces)) | |
5015 | ;;; (cons 'font-lock-type-face ps-underlined-faces)) | |
f83d2997 KH |
5016 | |
5017 | ||
5018 | (if (cperl-enable-font-lock) (cperl-windowed-init)) | |
5019 | ||
db133cb6 | 5020 | (defconst cperl-styles-entries |
5c8b7eaf SS |
5021 | '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset |
5022 | cperl-label-offset cperl-extra-newline-before-brace | |
bab27c0c | 5023 | cperl-merge-trailing-else |
db133cb6 RS |
5024 | cperl-continued-statement-offset)) |
5025 | ||
5026 | (defconst cperl-style-alist | |
5027 | '(("CPerl" ; =GNU without extra-newline-before-brace | |
5028 | (cperl-indent-level . 2) | |
5029 | (cperl-brace-offset . 0) | |
5030 | (cperl-continued-brace-offset . 0) | |
5031 | (cperl-label-offset . -2) | |
5032 | (cperl-extra-newline-before-brace . nil) | |
bab27c0c | 5033 | (cperl-merge-trailing-else . t) |
db133cb6 RS |
5034 | (cperl-continued-statement-offset . 2)) |
5035 | ("PerlStyle" ; CPerl with 4 as indent | |
5036 | (cperl-indent-level . 4) | |
5037 | (cperl-brace-offset . 0) | |
5038 | (cperl-continued-brace-offset . 0) | |
5039 | (cperl-label-offset . -4) | |
5040 | (cperl-extra-newline-before-brace . nil) | |
bab27c0c | 5041 | (cperl-merge-trailing-else . t) |
db133cb6 RS |
5042 | (cperl-continued-statement-offset . 4)) |
5043 | ("GNU" | |
5044 | (cperl-indent-level . 2) | |
5045 | (cperl-brace-offset . 0) | |
5046 | (cperl-continued-brace-offset . 0) | |
5047 | (cperl-label-offset . -2) | |
5048 | (cperl-extra-newline-before-brace . t) | |
bab27c0c | 5049 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5050 | (cperl-continued-statement-offset . 2)) |
5051 | ("K&R" | |
5052 | (cperl-indent-level . 5) | |
5053 | (cperl-brace-offset . 0) | |
5054 | (cperl-continued-brace-offset . -5) | |
5055 | (cperl-label-offset . -5) | |
5056 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
bab27c0c | 5057 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5058 | (cperl-continued-statement-offset . 5)) |
5059 | ("BSD" | |
5060 | (cperl-indent-level . 4) | |
5061 | (cperl-brace-offset . 0) | |
5062 | (cperl-continued-brace-offset . -4) | |
5063 | (cperl-label-offset . -4) | |
5064 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
5065 | (cperl-continued-statement-offset . 4)) | |
5066 | ("C++" | |
5067 | (cperl-indent-level . 4) | |
5068 | (cperl-brace-offset . 0) | |
5069 | (cperl-continued-brace-offset . -4) | |
5070 | (cperl-label-offset . -4) | |
5071 | (cperl-continued-statement-offset . 4) | |
bab27c0c | 5072 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5073 | (cperl-extra-newline-before-brace . t)) |
5074 | ("Current") | |
5075 | ("Whitesmith" | |
5076 | (cperl-indent-level . 4) | |
5077 | (cperl-brace-offset . 0) | |
5078 | (cperl-continued-brace-offset . 0) | |
5079 | (cperl-label-offset . -4) | |
5080 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
5081 | (cperl-continued-statement-offset . 4))) | |
5082 | "(Experimental) list of variables to set to get a particular indentation style. | |
5bd52f0e | 5083 | Should be used via `cperl-set-style' or via Perl menu.") |
db133cb6 | 5084 | |
f83d2997 | 5085 | (defun cperl-set-style (style) |
f94a632a | 5086 | "Set CPerl mode variables to use one of several different indentation styles. |
f83d2997 | 5087 | The arguments are a string representing the desired style. |
5c8b7eaf | 5088 | The list of styles is in `cperl-style-alist', available styles |
db133cb6 RS |
5089 | are GNU, K&R, BSD, C++ and Whitesmith. |
5090 | ||
5091 | The current value of style is memorized (unless there is a memorized | |
5092 | data already), may be restored by `cperl-set-style-back'. | |
5093 | ||
5094 | Chosing \"Current\" style will not change style, so this may be used for | |
5095 | side-effect of memorizing only." | |
5c8b7eaf SS |
5096 | (interactive |
5097 | (let ((list (mapcar (function (lambda (elt) (list (car elt)))) | |
db133cb6 | 5098 | cperl-style-alist))) |
f83d2997 | 5099 | (list (completing-read "Enter style: " list nil 'insist)))) |
db133cb6 RS |
5100 | (or cperl-old-style |
5101 | (setq cperl-old-style | |
5102 | (mapcar (function | |
5103 | (lambda (name) | |
5104 | (cons name (eval name)))) | |
5105 | cperl-styles-entries))) | |
5106 | (let ((style (cdr (assoc style cperl-style-alist))) setting str sym) | |
f83d2997 KH |
5107 | (while style |
5108 | (setq setting (car style) style (cdr style)) | |
db133cb6 RS |
5109 | (set (car setting) (cdr setting))))) |
5110 | ||
5111 | (defun cperl-set-style-back () | |
5112 | "Restore a style memorised by `cperl-set-style'." | |
5113 | (interactive) | |
5114 | (or cperl-old-style (error "The style was not changed")) | |
5115 | (let (setting) | |
5116 | (while cperl-old-style | |
5c8b7eaf | 5117 | (setq setting (car cperl-old-style) |
db133cb6 RS |
5118 | cperl-old-style (cdr cperl-old-style)) |
5119 | (set (car setting) (cdr setting))))) | |
f83d2997 KH |
5120 | |
5121 | (defun cperl-check-syntax () | |
5122 | (interactive) | |
5123 | (require 'mode-compile) | |
db133cb6 RS |
5124 | (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc"))) |
5125 | (eval '(mode-compile)))) ; Avoid a warning | |
f83d2997 KH |
5126 | |
5127 | (defun cperl-info-buffer (type) | |
5128 | ;; Returns buffer with documentation. Creates if missing. | |
5129 | ;; If TYPE, this vars buffer. | |
5130 | ;; Special care is taken to not stomp over an existing info buffer | |
5131 | (let* ((bname (if type "*info-perl-var*" "*info-perl*")) | |
5132 | (info (get-buffer bname)) | |
5133 | (oldbuf (get-buffer "*info*"))) | |
5134 | (if info info | |
5135 | (save-window-excursion | |
5136 | ;; Get Info running | |
5137 | (require 'info) | |
5138 | (cond (oldbuf | |
5139 | (set-buffer oldbuf) | |
5140 | (rename-buffer "*info-perl-tmp*"))) | |
5141 | (save-window-excursion | |
5142 | (info)) | |
5143 | (Info-find-node cperl-info-page (if type "perlvar" "perlfunc")) | |
5144 | (set-buffer "*info*") | |
5145 | (rename-buffer bname) | |
5146 | (cond (oldbuf | |
5147 | (set-buffer "*info-perl-tmp*") | |
5148 | (rename-buffer "*info*") | |
5149 | (set-buffer bname))) | |
029cb4d5 | 5150 | (make-local-variable 'window-min-height) |
f83d2997 KH |
5151 | (setq window-min-height 2) |
5152 | (current-buffer))))) | |
5153 | ||
5154 | (defun cperl-word-at-point (&optional p) | |
f94a632a | 5155 | "Return the word at point or at P." |
f83d2997 KH |
5156 | (save-excursion |
5157 | (if p (goto-char p)) | |
5158 | (or (cperl-word-at-point-hard) | |
5159 | (progn | |
5160 | (require 'etags) | |
5161 | (funcall (or (and (boundp 'find-tag-default-function) | |
5162 | find-tag-default-function) | |
5163 | (get major-mode 'find-tag-default-function) | |
5164 | ;; XEmacs 19.12 has `find-tag-default-hook'; it is | |
5165 | ;; automatically used within `find-tag-default': | |
5166 | 'find-tag-default)))))) | |
5167 | ||
5168 | (defun cperl-info-on-command (command) | |
f94a632a | 5169 | "Show documentation for Perl command COMMAND in other window. |
f83d2997 KH |
5170 | If perl-info buffer is shown in some frame, uses this frame. |
5171 | Customized by setting variables `cperl-shrink-wrap-info-frame', | |
5172 | `cperl-max-help-size'." | |
5c8b7eaf | 5173 | (interactive |
f83d2997 | 5174 | (let* ((default (cperl-word-at-point)) |
5c8b7eaf SS |
5175 | (read (read-string |
5176 | (format "Find doc for Perl function (default %s): " | |
f83d2997 | 5177 | default)))) |
5c8b7eaf SS |
5178 | (list (if (equal read "") |
5179 | default | |
f83d2997 KH |
5180 | read)))) |
5181 | ||
5182 | (let ((buffer (current-buffer)) | |
5183 | (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///" | |
5184 | pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner | |
5185 | max-height char-height buf-list) | |
5186 | (if (string-match "^-[a-zA-Z]$" command) | |
5187 | (setq cmd-desc "^-X[ \t\n]")) | |
5188 | (setq isvar (string-match "^[$@%]" command) | |
5189 | buf (cperl-info-buffer isvar) | |
5190 | iniwin (selected-window) | |
5191 | fr1 (window-frame iniwin)) | |
5192 | (set-buffer buf) | |
5193 | (beginning-of-buffer) | |
5c8b7eaf | 5194 | (or isvar |
f83d2997 KH |
5195 | (progn (re-search-forward "^-X[ \t\n]") |
5196 | (forward-line -1))) | |
5197 | (if (re-search-forward cmd-desc nil t) | |
5198 | (progn | |
5199 | ;; Go back to beginning of the group (ex, for qq) | |
5200 | (if (re-search-backward "^[ \t\n\f]") | |
5201 | (forward-line 1)) | |
5202 | (beginning-of-line) | |
5c8b7eaf | 5203 | ;; Get some of |
f83d2997 KH |
5204 | (setq pos (point) |
5205 | buf-list (list buf "*info-perl-var*" "*info-perl*")) | |
5206 | (while (and (not win) buf-list) | |
5207 | (setq win (get-buffer-window (car buf-list) t)) | |
5208 | (setq buf-list (cdr buf-list))) | |
5209 | (or (not win) | |
5210 | (eq (window-buffer win) buf) | |
5211 | (set-window-buffer win buf)) | |
5212 | (and win (setq fr2 (window-frame win))) | |
5213 | (if (or (not fr2) (eq fr1 fr2)) | |
5214 | (pop-to-buffer buf) | |
5215 | (special-display-popup-frame buf) ; Make it visible | |
5216 | (select-window win)) | |
5217 | (goto-char pos) ; Needed (?!). | |
5218 | ;; Resize | |
5219 | (setq iniheight (window-height) | |
5220 | frheight (frame-height) | |
5221 | not-loner (< iniheight (1- frheight))) ; Are not alone | |
5c8b7eaf | 5222 | (cond ((if not-loner cperl-max-help-size |
f83d2997 | 5223 | cperl-shrink-wrap-info-frame) |
5c8b7eaf SS |
5224 | (setq height |
5225 | (+ 2 | |
5226 | (count-lines | |
5227 | pos | |
f83d2997 KH |
5228 | (save-excursion |
5229 | (if (re-search-forward | |
5230 | "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t) | |
5231 | (match-beginning 0) (point-max))))) | |
5c8b7eaf | 5232 | max-height |
f83d2997 KH |
5233 | (if not-loner |
5234 | (/ (* (- frheight 3) cperl-max-help-size) 100) | |
5235 | (setq char-height (frame-char-height)) | |
5236 | ;; Non-functioning under OS/2: | |
5237 | (if (eq char-height 1) (setq char-height 18)) | |
5238 | ;; Title, menubar, + 2 for slack | |
5239 | (- (/ (x-display-pixel-height) char-height) 4) | |
5240 | )) | |
5241 | (if (> height max-height) (setq height max-height)) | |
5242 | ;;(message "was %s doing %s" iniheight height) | |
5243 | (if not-loner | |
5244 | (enlarge-window (- height iniheight)) | |
5245 | (set-frame-height (window-frame win) (1+ height))))) | |
5246 | (set-window-start (selected-window) pos)) | |
5247 | (message "No entry for %s found." command)) | |
5248 | ;;(pop-to-buffer buffer) | |
5249 | (select-window iniwin))) | |
5250 | ||
5251 | (defun cperl-info-on-current-command () | |
029cb4d5 | 5252 | "Show documentation for Perl command at point in other window." |
f83d2997 KH |
5253 | (interactive) |
5254 | (cperl-info-on-command (cperl-word-at-point))) | |
5255 | ||
5256 | (defun cperl-imenu-info-imenu-search () | |
5257 | (if (looking-at "^-X[ \t\n]") nil | |
5258 | (re-search-backward | |
5259 | "^\n\\([-a-zA-Z_]+\\)[ \t\n]") | |
5260 | (forward-line 1))) | |
5261 | ||
5c8b7eaf | 5262 | (defun cperl-imenu-info-imenu-name () |
f83d2997 KH |
5263 | (buffer-substring |
5264 | (match-beginning 1) (match-end 1))) | |
5265 | ||
5266 | (defun cperl-imenu-on-info () | |
5267 | (interactive) | |
5268 | (let* ((buffer (current-buffer)) | |
5269 | imenu-create-index-function | |
5c8b7eaf SS |
5270 | imenu-prev-index-position-function |
5271 | imenu-extract-index-name-function | |
f83d2997 KH |
5272 | (index-item (save-restriction |
5273 | (save-window-excursion | |
5274 | (set-buffer (cperl-info-buffer nil)) | |
5c8b7eaf | 5275 | (setq imenu-create-index-function |
f83d2997 KH |
5276 | 'imenu-default-create-index-function |
5277 | imenu-prev-index-position-function | |
5278 | 'cperl-imenu-info-imenu-search | |
5279 | imenu-extract-index-name-function | |
5280 | 'cperl-imenu-info-imenu-name) | |
5281 | (imenu-choose-buffer-index))))) | |
5282 | (and index-item | |
5283 | (progn | |
5284 | (push-mark) | |
5285 | (pop-to-buffer "*info-perl*") | |
5286 | (cond | |
5287 | ((markerp (cdr index-item)) | |
5288 | (goto-char (marker-position (cdr index-item)))) | |
5289 | (t | |
5290 | (goto-char (cdr index-item)))) | |
5291 | (set-window-start (selected-window) (point)) | |
5292 | (pop-to-buffer buffer))))) | |
5293 | ||
5294 | (defun cperl-lineup (beg end &optional step minshift) | |
5295 | "Lineup construction in a region. | |
5296 | Beginning of region should be at the start of a construction. | |
5297 | All first occurrences of this construction in the lines that are | |
5298 | partially contained in the region are lined up at the same column. | |
5299 | ||
5300 | MINSHIFT is the minimal amount of space to insert before the construction. | |
5301 | STEP is the tabwidth to position constructions. | |
029cb4d5 | 5302 | If STEP is nil, `cperl-lineup-step' will be used |
f83d2997 KH |
5303 | \(or `cperl-indent-level', if `cperl-lineup-step' is `nil'). |
5304 | Will not move the position at the start to the left." | |
5305 | (interactive "r") | |
5306 | (let (search col tcol seen b e) | |
5307 | (save-excursion | |
5308 | (goto-char end) | |
5309 | (end-of-line) | |
5310 | (setq end (point-marker)) | |
5311 | (goto-char beg) | |
5312 | (skip-chars-forward " \t\f") | |
5313 | (setq beg (point-marker)) | |
5314 | (indent-region beg end nil) | |
5315 | (goto-char beg) | |
5316 | (setq col (current-column)) | |
5317 | (if (looking-at "[a-zA-Z0-9_]") | |
5318 | (if (looking-at "\\<[a-zA-Z0-9_]+\\>") | |
5319 | (setq search | |
5c8b7eaf SS |
5320 | (concat "\\<" |
5321 | (regexp-quote | |
f83d2997 KH |
5322 | (buffer-substring (match-beginning 0) |
5323 | (match-end 0))) "\\>")) | |
5324 | (error "Cannot line up in a middle of the word")) | |
5325 | (if (looking-at "$") | |
5326 | (error "Cannot line up end of line")) | |
5327 | (setq search (regexp-quote (char-to-string (following-char))))) | |
5328 | (setq step (or step cperl-lineup-step cperl-indent-level)) | |
5329 | (or minshift (setq minshift 1)) | |
5330 | (while (progn | |
5331 | (beginning-of-line 2) | |
5c8b7eaf | 5332 | (and (< (point) end) |
f83d2997 KH |
5333 | (re-search-forward search end t) |
5334 | (goto-char (match-beginning 0)))) | |
5335 | (setq tcol (current-column) seen t) | |
5336 | (if (> tcol col) (setq col tcol))) | |
5337 | (or seen | |
5338 | (error "The construction to line up occurred only once")) | |
5339 | (goto-char beg) | |
5340 | (setq col (+ col minshift)) | |
5341 | (if (/= (% col step) 0) (setq step (* step (1+ (/ col step))))) | |
5c8b7eaf | 5342 | (while |
f83d2997 KH |
5343 | (progn |
5344 | (setq e (point)) | |
5345 | (skip-chars-backward " \t") | |
5346 | (delete-region (point) e) | |
5347 | (indent-to-column col); (make-string (- col (current-column)) ?\ )) | |
5c8b7eaf SS |
5348 | (beginning-of-line 2) |
5349 | (and (< (point) end) | |
f83d2997 KH |
5350 | (re-search-forward search end t) |
5351 | (goto-char (match-beginning 0)))))))) ; No body | |
5352 | ||
5353 | (defun cperl-etags (&optional add all files) | |
5354 | "Run etags with appropriate options for Perl files. | |
5355 | If optional argument ALL is `recursive', will process Perl files | |
5356 | in subdirectories too." | |
5357 | (interactive) | |
5358 | (let ((cmd "etags") | |
5359 | (args '("-l" "none" "-r" "/\\<\\(package\\|sub\\)[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([{#]\\|$\\)\\)/\\4/")) | |
5360 | res) | |
5361 | (if add (setq args (cons "-a" args))) | |
5362 | (or files (setq files (list buffer-file-name))) | |
5363 | (cond | |
5364 | ((eq all 'recursive) | |
5365 | ;;(error "Not implemented: recursive") | |
5c8b7eaf | 5366 | (setq args (append (list "-e" |
f83d2997 KH |
5367 | "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/} |
5368 | use File::Find; | |
5369 | find(\\&wanted, '.'); | |
5c8b7eaf | 5370 | exec @ARGV;" |
f83d2997 KH |
5371 | cmd) args) |
5372 | cmd "perl")) | |
5c8b7eaf | 5373 | (all |
f83d2997 | 5374 | ;;(error "Not implemented: all") |
5c8b7eaf | 5375 | (setq args (append (list "-e" |
f83d2997 | 5376 | "push @ARGV, <*.PL *.pl *.pm>; |
5c8b7eaf | 5377 | exec @ARGV;" |
f83d2997 KH |
5378 | cmd) args) |
5379 | cmd "perl")) | |
5380 | (t | |
5381 | (setq args (append args files)))) | |
5382 | (setq res (apply 'call-process cmd nil nil nil args)) | |
5383 | (or (eq res 0) | |
5384 | (message "etags returned \"%s\"" res)))) | |
5385 | ||
5386 | (defun cperl-toggle-auto-newline () | |
5387 | "Toggle the state of `cperl-auto-newline'." | |
5388 | (interactive) | |
5389 | (setq cperl-auto-newline (not cperl-auto-newline)) | |
5c8b7eaf | 5390 | (message "Newlines will %sbe auto-inserted now." |
f83d2997 KH |
5391 | (if cperl-auto-newline "" "not "))) |
5392 | ||
5393 | (defun cperl-toggle-abbrev () | |
5394 | "Toggle the state of automatic keyword expansion in CPerl mode." | |
5395 | (interactive) | |
5396 | (abbrev-mode (if abbrev-mode 0 1)) | |
5c8b7eaf | 5397 | (message "Perl control structure will %sbe auto-inserted now." |
f83d2997 KH |
5398 | (if abbrev-mode "" "not "))) |
5399 | ||
5400 | ||
5401 | (defun cperl-toggle-electric () | |
5402 | "Toggle the state of parentheses doubling in CPerl mode." | |
5403 | (interactive) | |
5404 | (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t)) | |
5c8b7eaf | 5405 | (message "Parentheses will %sbe auto-doubled now." |
f83d2997 KH |
5406 | (if (cperl-val 'cperl-electric-parens) "" "not "))) |
5407 | ||
db133cb6 RS |
5408 | (defun cperl-toggle-autohelp () |
5409 | "Toggle the state of automatic help message in CPerl mode. | |
5410 | See `cperl-lazy-help-time' too." | |
5411 | (interactive) | |
5412 | (if (fboundp 'run-with-idle-timer) | |
5413 | (progn | |
5414 | (if cperl-lazy-installed | |
5415 | (eval '(cperl-lazy-unstall)) | |
5416 | (cperl-lazy-install)) | |
5c8b7eaf | 5417 | (message "Perl help messages will %sbe automatically shown now." |
db133cb6 RS |
5418 | (if cperl-lazy-installed "" "not "))) |
5419 | (message "Cannot automatically show Perl help messages - run-with-idle-timer missing."))) | |
5420 | ||
5421 | (defun cperl-toggle-construct-fix () | |
5422 | "Toggle whether `indent-region'/`indent-sexp' fix whitespace too." | |
5423 | (interactive) | |
5c8b7eaf | 5424 | (setq cperl-indent-region-fix-constructs |
5bd52f0e RS |
5425 | (if cperl-indent-region-fix-constructs |
5426 | nil | |
5427 | 1)) | |
5c8b7eaf | 5428 | (message "indent-region/indent-sexp will %sbe automatically fix whitespace." |
db133cb6 RS |
5429 | (if cperl-indent-region-fix-constructs "" "not "))) |
5430 | ||
f83d2997 KH |
5431 | ;;;; Tags file creation. |
5432 | ||
5433 | (defvar cperl-tmp-buffer " *cperl-tmp*") | |
5434 | ||
5435 | (defun cperl-setup-tmp-buf () | |
5436 | (set-buffer (get-buffer-create cperl-tmp-buffer)) | |
5437 | (set-syntax-table cperl-mode-syntax-table) | |
5438 | (buffer-disable-undo) | |
5439 | (auto-fill-mode 0) | |
5440 | (if cperl-use-syntax-table-text-property-for-tags | |
5441 | (progn | |
029cb4d5 | 5442 | (make-local-variable 'parse-sexp-lookup-properties) |
f83d2997 KH |
5443 | ;; Do not introduce variable if not needed, we check it! |
5444 | (set 'parse-sexp-lookup-properties t)))) | |
5445 | ||
5446 | (defun cperl-xsub-scan () | |
f83d2997 | 5447 | (require 'imenu) |
5c8b7eaf | 5448 | (let ((index-alist '()) |
f83d2997 KH |
5449 | (prev-pos 0) index index1 name package prefix) |
5450 | (goto-char (point-min)) | |
5451 | (if noninteractive | |
5452 | (message "Scanning XSUB for index") | |
5453 | (imenu-progress-message prev-pos 0)) | |
5454 | ;; Search for the function | |
5455 | (progn ;;save-match-data | |
5456 | (while (re-search-forward | |
5457 | "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)" | |
5458 | nil t) | |
5459 | (or noninteractive | |
5460 | (imenu-progress-message prev-pos)) | |
5461 | (cond | |
5462 | ((match-beginning 2) ; SECTION | |
5463 | (setq package (buffer-substring (match-beginning 2) (match-end 2))) | |
5464 | (goto-char (match-beginning 0)) | |
5465 | (skip-chars-forward " \t") | |
5466 | (forward-char 1) | |
5467 | (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>") | |
5468 | (setq prefix (buffer-substring (match-beginning 1) (match-end 1))) | |
5469 | (setq prefix nil))) | |
5470 | ((not package) nil) ; C language section | |
5471 | ((match-beginning 3) ; XSUB | |
5472 | (goto-char (1+ (match-beginning 3))) | |
5473 | (setq index (imenu-example--name-and-position)) | |
5474 | (setq name (buffer-substring (match-beginning 3) (match-end 3))) | |
5475 | (if (and prefix (string-match (concat "^" prefix) name)) | |
5476 | (setq name (substring name (length prefix)))) | |
5477 | (cond ((string-match "::" name) nil) | |
5478 | (t | |
5479 | (setq index1 (cons (concat package "::" name) (cdr index))) | |
5480 | (push index1 index-alist))) | |
5481 | (setcar index name) | |
5482 | (push index index-alist)) | |
5483 | (t ; BOOT: section | |
5484 | ;; (beginning-of-line) | |
5485 | (setq index (imenu-example--name-and-position)) | |
5486 | (setcar index (concat package "::BOOT:")) | |
5487 | (push index index-alist))))) | |
5488 | (or noninteractive | |
5489 | (imenu-progress-message prev-pos 100)) | |
f83d2997 KH |
5490 | index-alist)) |
5491 | ||
6c389151 SM |
5492 | (defvar cperl-unreadable-ok nil) |
5493 | ||
5494 | (defun cperl-find-tags (ifile xs topdir) | |
f83d2997 | 5495 | (let (ind (b (get-buffer cperl-tmp-buffer)) lst elt pos ret rel |
6c389151 | 5496 | (cperl-pod-here-fontify nil) f file) |
f83d2997 KH |
5497 | (save-excursion |
5498 | (if b (set-buffer b) | |
5499 | (cperl-setup-tmp-buf)) | |
5500 | (erase-buffer) | |
6c389151 SM |
5501 | (condition-case err |
5502 | (setq file (car (insert-file-contents ifile))) | |
5503 | (error (if cperl-unreadable-ok nil | |
5504 | (if (y-or-n-p | |
5505 | (format "File %s unreadable. Continue? " ifile)) | |
5506 | (setq cperl-unreadable-ok t) | |
5507 | (error "Aborting: unreadable file %s" ifile))))) | |
5508 | (if (not file) | |
5509 | (message "Unreadable file %s" ifile) | |
f83d2997 KH |
5510 | (message "Scanning file %s ..." file) |
5511 | (if (and cperl-use-syntax-table-text-property-for-tags | |
5512 | (not xs)) | |
5513 | (condition-case err ; after __END__ may have garbage | |
6c389151 | 5514 | (cperl-find-pods-heres nil nil noninteractive) |
f83d2997 KH |
5515 | (error (message "While scanning for syntax: %s" err)))) |
5516 | (if xs | |
5517 | (setq lst (cperl-xsub-scan)) | |
80585273 | 5518 | (setq ind (cperl-imenu--create-perl-index)) |
f83d2997 | 5519 | (setq lst (cdr (assoc "+Unsorted List+..." ind)))) |
5c8b7eaf SS |
5520 | (setq lst |
5521 | (mapcar | |
5522 | (function | |
f83d2997 KH |
5523 | (lambda (elt) |
5524 | (cond ((string-match "^[_a-zA-Z]" (car elt)) | |
5525 | (goto-char (cdr elt)) | |
5bd52f0e | 5526 | (beginning-of-line) ; pos should be of the start of the line |
5c8b7eaf SS |
5527 | (list (car elt) |
5528 | (point) | |
5bd52f0e | 5529 | (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l |
f83d2997 | 5530 | (buffer-substring (progn |
6c389151 SM |
5531 | (goto-char (cdr elt)) |
5532 | ;; After name now... | |
f83d2997 KH |
5533 | (or (eolp) (forward-char 1)) |
5534 | (point)) | |
5535 | (progn | |
5536 | (beginning-of-line) | |
5537 | (point)))))))) | |
5538 | lst)) | |
5539 | (erase-buffer) | |
5540 | (while lst | |
5541 | (setq elt (car lst) lst (cdr lst)) | |
5542 | (if elt | |
5543 | (progn | |
5c8b7eaf | 5544 | (insert (elt elt 3) |
f83d2997 KH |
5545 | 127 |
5546 | (if (string-match "^package " (car elt)) | |
5547 | (substring (car elt) 8) | |
5548 | (car elt) ) | |
5549 | 1 | |
5bd52f0e | 5550 | (number-to-string (elt elt 2)) ; Line |
f83d2997 | 5551 | "," |
5bd52f0e | 5552 | (number-to-string (1- (elt elt 1))) ; Char pos 0-based |
f83d2997 KH |
5553 | "\n") |
5554 | (if (and (string-match "^[_a-zA-Z]+::" (car elt)) | |
5555 | (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]" | |
5556 | (elt elt 3))) | |
5557 | ;; Need to insert the name without package as well | |
5c8b7eaf | 5558 | (setq lst (cons (cons (substring (elt elt 3) |
f83d2997 KH |
5559 | (match-beginning 1) |
5560 | (match-end 1)) | |
5561 | (cdr elt)) | |
5562 | lst)))))) | |
5563 | (setq pos (point)) | |
5564 | (goto-char 1) | |
5565 | (setq rel file) | |
5566 | ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties | |
5567 | (set-text-properties 0 (length rel) nil rel) | |
5568 | (and (equal topdir (substring rel 0 (length topdir))) | |
5569 | (setq rel (substring file (length topdir)))) | |
5570 | (insert "\f\n" rel "," (number-to-string (1- pos)) "\n") | |
5571 | (setq ret (buffer-substring 1 (point-max))) | |
5572 | (erase-buffer) | |
5573 | (or noninteractive | |
5574 | (message "Scanning file %s finished" file)) | |
6c389151 | 5575 | ret)))) |
f83d2997 KH |
5576 | |
5577 | (defun cperl-add-tags-recurse-noxs () | |
5578 | "Add to TAGS data for Perl and XSUB files in the current directory and kids. | |
5579 | Use as | |
5580 | emacs -batch -q -no-site-file -l emacs/cperl-mode.el \ | |
5c8b7eaf | 5581 | -f cperl-add-tags-recurse |
f83d2997 KH |
5582 | " |
5583 | (cperl-write-tags nil nil t t nil t)) | |
5584 | ||
5585 | (defun cperl-add-tags-recurse () | |
5586 | "Add to TAGS file data for Perl files in the current directory and kids. | |
5587 | Use as | |
5588 | emacs -batch -q -no-site-file -l emacs/cperl-mode.el \ | |
5c8b7eaf | 5589 | -f cperl-add-tags-recurse |
f83d2997 KH |
5590 | " |
5591 | (cperl-write-tags nil nil t t)) | |
5592 | ||
5593 | (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir) | |
5594 | ;; If INBUFFER, do not select buffer, and do not save | |
5595 | ;; If ERASE is `ignore', do not erase, and do not try to delete old info. | |
5596 | (require 'etags) | |
5597 | (if file nil | |
5598 | (setq file (if dir default-directory (buffer-file-name))) | |
5599 | (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!"))) | |
5600 | (or topdir | |
5601 | (setq topdir default-directory)) | |
5602 | (let ((tags-file-name "TAGS") | |
5603 | (case-fold-search (eq system-type 'emx)) | |
6c389151 | 5604 | xs rel tm) |
f83d2997 KH |
5605 | (save-excursion |
5606 | (cond (inbuffer nil) ; Already there | |
5607 | ((file-exists-p tags-file-name) | |
5bd52f0e RS |
5608 | (if cperl-xemacs-p |
5609 | (visit-tags-table-buffer) | |
5610 | (visit-tags-table-buffer tags-file-name))) | |
f83d2997 KH |
5611 | (t (set-buffer (find-file-noselect tags-file-name)))) |
5612 | (cond | |
5613 | (dir | |
5614 | (cond ((eq erase 'ignore)) | |
5615 | (erase | |
5616 | (erase-buffer) | |
5617 | (setq erase 'ignore))) | |
6c389151 SM |
5618 | (let ((files |
5619 | (condition-case err | |
5620 | (directory-files file t | |
5621 | (if recurse nil cperl-scan-files-regexp) | |
5622 | t) | |
5623 | (error | |
5624 | (if cperl-unreadable-ok nil | |
5625 | (if (y-or-n-p | |
5626 | (format "Directory %s unreadable. Continue? " file)) | |
5627 | (setq cperl-unreadable-ok t | |
5628 | tm nil) ; Return empty list | |
5629 | (error "Aborting: unreadable directory %s" file))))))) | |
f83d2997 KH |
5630 | (mapcar (function (lambda (file) |
5631 | (cond | |
5632 | ((string-match cperl-noscan-files-regexp file) | |
5633 | nil) | |
5634 | ((not (file-directory-p file)) | |
5635 | (if (string-match cperl-scan-files-regexp file) | |
5636 | (cperl-write-tags file erase recurse nil t noxs topdir))) | |
5637 | ((not recurse) nil) | |
5638 | (t (cperl-write-tags file erase recurse t t noxs topdir))))) | |
5639 | files)) | |
5640 | ) | |
5641 | (t | |
5642 | (setq xs (string-match "\\.xs$" file)) | |
5643 | (if (not (and xs noxs)) | |
5644 | (progn | |
5645 | (cond ((eq erase 'ignore) (goto-char (point-max))) | |
5646 | (erase (erase-buffer)) | |
5647 | (t | |
5648 | (goto-char 1) | |
5bd52f0e RS |
5649 | (setq rel file) |
5650 | ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties | |
5651 | (set-text-properties 0 (length rel) nil rel) | |
5652 | (and (equal topdir (substring rel 0 (length topdir))) | |
5653 | (setq rel (substring file (length topdir)))) | |
5654 | (if (search-forward (concat "\f\n" rel ",") nil t) | |
f83d2997 KH |
5655 | (progn |
5656 | (search-backward "\f\n") | |
5657 | (delete-region (point) | |
5658 | (save-excursion | |
5659 | (forward-char 1) | |
5c8b7eaf | 5660 | (if (search-forward "\f\n" |
f83d2997 KH |
5661 | nil 'toend) |
5662 | (- (point) 2) | |
5663 | (point-max))))) | |
5664 | (goto-char (point-max))))) | |
5665 | (insert (cperl-find-tags file xs topdir)))))) | |
5666 | (if inbuffer nil ; Delegate to the caller | |
5667 | (save-buffer 0) ; No backup | |
5668 | (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs? | |
5669 | (initialize-new-tags-table)))))) | |
5670 | ||
5671 | (defvar cperl-tags-hier-regexp-list | |
5c8b7eaf | 5672 | (concat |
f83d2997 KH |
5673 | "^\\(" |
5674 | "\\(package\\)\\>" | |
5675 | "\\|" | |
5676 | "sub\\>[^\n]+::" | |
5677 | "\\|" | |
5678 | "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB? | |
5679 | "\\|" | |
5680 | "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section | |
5681 | "\\)")) | |
5682 | ||
5683 | (defvar cperl-hierarchy '(() ()) | |
f94a632a | 5684 | "Global hierarchy of classes.") |
f83d2997 KH |
5685 | |
5686 | (defun cperl-tags-hier-fill () | |
5687 | ;; Suppose we are in a tag table cooked by cperl. | |
5688 | (goto-char 1) | |
5689 | (let (type pack name pos line chunk ord cons1 file str info fileind) | |
5690 | (while (re-search-forward cperl-tags-hier-regexp-list nil t) | |
5c8b7eaf | 5691 | (setq pos (match-beginning 0) |
f83d2997 KH |
5692 | pack (match-beginning 2)) |
5693 | (beginning-of-line) | |
5694 | (if (looking-at (concat | |
5695 | "\\([^\n]+\\)" | |
5696 | "\C-?" | |
5697 | "\\([^\n]+\\)" | |
5698 | "\C-a" | |
5699 | "\\([0-9]+\\)" | |
5700 | "," | |
5701 | "\\([0-9]+\\)")) | |
5702 | (progn | |
5703 | (setq ;;str (buffer-substring (match-beginning 1) (match-end 1)) | |
5704 | name (buffer-substring (match-beginning 2) (match-end 2)) | |
5705 | ;;pos (buffer-substring (match-beginning 3) (match-end 3)) | |
5bd52f0e | 5706 | line (buffer-substring (match-beginning 3) (match-end 3)) |
f83d2997 | 5707 | ord (if pack 1 0) |
f83d2997 | 5708 | file (file-of-tag) |
5bd52f0e RS |
5709 | fileind (format "%s:%s" file line) |
5710 | ;; Moves to beginning of the next line: | |
5711 | info (cperl-etags-snarf-tag file line)) | |
f83d2997 KH |
5712 | ;; Move back |
5713 | (forward-char -1) | |
5714 | ;; Make new member of hierarchy name ==> file ==> pos if needed | |
5715 | (if (setq cons1 (assoc name (nth ord cperl-hierarchy))) | |
5716 | ;; Name known | |
5717 | (setcdr cons1 (cons (cons fileind (vector file info)) | |
5718 | (cdr cons1))) | |
5719 | ;; First occurrence of the name, start alist | |
5720 | (setq cons1 (cons name (list (cons fileind (vector file info))))) | |
5c8b7eaf | 5721 | (if pack |
f83d2997 KH |
5722 | (setcar (cdr cperl-hierarchy) |
5723 | (cons cons1 (nth 1 cperl-hierarchy))) | |
5724 | (setcar cperl-hierarchy | |
5725 | (cons cons1 (car cperl-hierarchy))))))) | |
5726 | (end-of-line)))) | |
5727 | ||
5728 | (defun cperl-tags-hier-init (&optional update) | |
5729 | "Show hierarchical menu of classes and methods. | |
5730 | Finds info about classes by a scan of loaded TAGS files. | |
5731 | Supposes that the TAGS files contain fully qualified function names. | |
5732 | One may build such TAGS files from CPerl mode menu." | |
5733 | (interactive) | |
5734 | (require 'etags) | |
5735 | (require 'imenu) | |
5736 | (if (or update (null (nth 2 cperl-hierarchy))) | |
5bd52f0e | 5737 | (let (pack name cons1 to l1 l2 l3 l4 b |
f83d2997 KH |
5738 | (remover (function (lambda (elt) ; (name (file1...) (file2..)) |
5739 | (or (nthcdr 2 elt) | |
5740 | ;; Only in one file | |
5741 | (setcdr elt (cdr (nth 1 elt)))))))) | |
5742 | ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later! | |
5743 | (setq cperl-hierarchy (list l1 l2 l3)) | |
5bd52f0e RS |
5744 | (if cperl-xemacs-p ; Not checked |
5745 | (progn | |
5746 | (or tags-file-name | |
5747 | ;; Does this work in XEmacs? | |
f83d2997 KH |
5748 | (call-interactively 'visit-tags-table)) |
5749 | (message "Updating list of classes...") | |
5bd52f0e RS |
5750 | (set-buffer (get-file-buffer tags-file-name)) |
5751 | (cperl-tags-hier-fill)) | |
5752 | (or tags-table-list | |
5753 | (call-interactively 'visit-tags-table)) | |
5c8b7eaf | 5754 | (mapcar |
f83d2997 KH |
5755 | (function |
5756 | (lambda (tagsfile) | |
5bd52f0e | 5757 | (message "Updating list of classes... %s" tagsfile) |
f83d2997 KH |
5758 | (set-buffer (get-file-buffer tagsfile)) |
5759 | (cperl-tags-hier-fill))) | |
5760 | tags-table-list) | |
5bd52f0e | 5761 | (message "Updating list of classes... postprocessing...")) |
f83d2997 KH |
5762 | (mapcar remover (car cperl-hierarchy)) |
5763 | (mapcar remover (nth 1 cperl-hierarchy)) | |
5764 | (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy)) | |
5765 | (cons "Methods: " (car cperl-hierarchy)))) | |
5766 | (cperl-tags-treeify to 1) | |
5767 | (setcar (nthcdr 2 cperl-hierarchy) | |
5768 | (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to)))) | |
5769 | (message "Updating list of classes: done, requesting display...") | |
5770 | ;;(cperl-imenu-addback (nth 2 cperl-hierarchy)) | |
5771 | )) | |
5772 | (or (nth 2 cperl-hierarchy) | |
5773 | (error "No items found")) | |
5774 | (setq update | |
5775 | ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy)) | |
5776 | (if window-system | |
5777 | (x-popup-menu t (nth 2 cperl-hierarchy)) | |
5778 | (require 'tmm) | |
5779 | (tmm-prompt (nth 2 cperl-hierarchy)))) | |
5780 | (if (and update (listp update)) | |
5781 | (progn (while (cdr update) (setq update (cdr update))) | |
5782 | (setq update (car update)))) ; Get the last from the list | |
5c8b7eaf | 5783 | (if (vectorp update) |
f83d2997 KH |
5784 | (progn |
5785 | (find-file (elt update 0)) | |
5bd52f0e | 5786 | (cperl-etags-goto-tag-location (elt update 1)))) |
f83d2997 KH |
5787 | (if (eq update -999) (cperl-tags-hier-init t))) |
5788 | ||
5789 | (defun cperl-tags-treeify (to level) | |
5790 | ;; cadr of `to' is read-write. On start it is a cons | |
5c8b7eaf | 5791 | (let* ((regexp (concat "^\\(" (mapconcat |
f83d2997 KH |
5792 | 'identity |
5793 | (make-list level "[_a-zA-Z0-9]+") | |
5794 | "::") | |
5795 | "\\)\\(::\\)?")) | |
5796 | (packages (cdr (nth 1 to))) | |
5797 | (methods (cdr (nth 2 to))) | |
5798 | l1 head tail cons1 cons2 ord writeto packs recurse | |
5799 | root-packages root-functions ms many_ms same_name ps | |
5800 | (move-deeper | |
5c8b7eaf | 5801 | (function |
f83d2997 KH |
5802 | (lambda (elt) |
5803 | (cond ((and (string-match regexp (car elt)) | |
5804 | (or (eq ord 1) (match-end 2))) | |
5805 | (setq head (substring (car elt) 0 (match-end 1)) | |
5c8b7eaf | 5806 | tail (if (match-end 2) (substring (car elt) |
f83d2997 KH |
5807 | (match-end 2))) |
5808 | recurse t) | |
5809 | (if (setq cons1 (assoc head writeto)) nil | |
5810 | ;; Need to init new head | |
5811 | (setcdr writeto (cons (list head (list "Packages: ") | |
5812 | (list "Methods: ")) | |
5813 | (cdr writeto))) | |
5814 | (setq cons1 (nth 1 writeto))) | |
5815 | (setq cons2 (nth ord cons1)) ; Either packs or meths | |
5816 | (setcdr cons2 (cons elt (cdr cons2)))) | |
5817 | ((eq ord 2) | |
5818 | (setq root-functions (cons elt root-functions))) | |
5819 | (t | |
5820 | (setq root-packages (cons elt root-packages)))))))) | |
5821 | (setcdr to l1) ; Init to dynamic space | |
5822 | (setq writeto to) | |
5823 | (setq ord 1) | |
5824 | (mapcar move-deeper packages) | |
5825 | (setq ord 2) | |
5826 | (mapcar move-deeper methods) | |
5827 | (if recurse | |
5828 | (mapcar (function (lambda (elt) | |
5829 | (cperl-tags-treeify elt (1+ level)))) | |
5830 | (cdr to))) | |
5831 | ;;Now clean up leaders with one child only | |
5832 | (mapcar (function (lambda (elt) | |
5c8b7eaf | 5833 | (if (not (and (listp (cdr elt)) |
f83d2997 KH |
5834 | (eq (length elt) 2))) nil |
5835 | (setcar elt (car (nth 1 elt))) | |
5836 | (setcdr elt (cdr (nth 1 elt)))))) | |
5837 | (cdr to)) | |
5838 | ;; Sort the roots of subtrees | |
5839 | (if (default-value 'imenu-sort-function) | |
5840 | (setcdr to | |
5841 | (sort (cdr to) (default-value 'imenu-sort-function)))) | |
5842 | ;; Now add back functions removed from display | |
5843 | (mapcar (function (lambda (elt) | |
5844 | (setcdr to (cons elt (cdr to))))) | |
5845 | (if (default-value 'imenu-sort-function) | |
5846 | (nreverse | |
5847 | (sort root-functions (default-value 'imenu-sort-function))) | |
5848 | root-functions)) | |
5849 | ;; Now add back packages removed from display | |
5850 | (mapcar (function (lambda (elt) | |
5c8b7eaf SS |
5851 | (setcdr to (cons (cons (concat "package " (car elt)) |
5852 | (cdr elt)) | |
f83d2997 KH |
5853 | (cdr to))))) |
5854 | (if (default-value 'imenu-sort-function) | |
5c8b7eaf | 5855 | (nreverse |
f83d2997 KH |
5856 | (sort root-packages (default-value 'imenu-sort-function))) |
5857 | root-packages)) | |
5858 | )) | |
5859 | ||
5860 | ;;;(x-popup-menu t | |
5c8b7eaf | 5861 | ;;; '(keymap "Name1" |
f83d2997 | 5862 | ;;; ("Ret1" "aa") |
5c8b7eaf SS |
5863 | ;;; ("Head1" "ab" |
5864 | ;;; keymap "Name2" | |
f83d2997 KH |
5865 | ;;; ("Tail1" "x") ("Tail2" "y")))) |
5866 | ||
5867 | (defun cperl-list-fold (list name limit) | |
5868 | (let (list1 list2 elt1 (num 0)) | |
5869 | (if (<= (length list) limit) list | |
5870 | (setq list1 nil list2 nil) | |
5871 | (while list | |
5c8b7eaf | 5872 | (setq num (1+ num) |
f83d2997 KH |
5873 | elt1 (car list) |
5874 | list (cdr list)) | |
5875 | (if (<= num imenu-max-items) | |
5876 | (setq list2 (cons elt1 list2)) | |
5877 | (setq list1 (cons (cons name | |
5878 | (nreverse list2)) | |
5879 | list1) | |
5880 | list2 (list elt1) | |
5881 | num 1))) | |
5882 | (nreverse (cons (cons name | |
5883 | (nreverse list2)) | |
5884 | list1))))) | |
5885 | ||
5886 | (defun cperl-menu-to-keymap (menu &optional name) | |
5887 | (let (list) | |
5c8b7eaf SS |
5888 | (cons 'keymap |
5889 | (mapcar | |
5890 | (function | |
f83d2997 KH |
5891 | (lambda (elt) |
5892 | (cond ((listp (cdr elt)) | |
5893 | (setq list (cperl-list-fold | |
5894 | (cdr elt) (car elt) imenu-max-items)) | |
5895 | (cons nil | |
5896 | (cons (car elt) | |
5897 | (cperl-menu-to-keymap list)))) | |
5898 | (t | |
5899 | (list (cdr elt) (car elt) t))))) ; t is needed in 19.34 | |
5900 | (cperl-list-fold menu "Root" imenu-max-items))))) | |
5901 | ||
5902 | \f | |
5903 | (defvar cperl-bad-style-regexp | |
5904 | (mapconcat 'identity | |
5905 | '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign | |
5906 | "[-<>=+^&|]+[^- \t\n=+<>~]" ; sign+ char | |
5907 | ) | |
5908 | "\\|") | |
5909 | "Finds places such that insertion of a whitespace may help a lot.") | |
5910 | ||
5c8b7eaf | 5911 | (defvar cperl-not-bad-style-regexp |
f83d2997 KH |
5912 | (mapconcat 'identity |
5913 | '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++ | |
5914 | "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used. | |
5915 | "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field) | |
5916 | "<\\$?\\sw+\\(\\.\\sw+\\)?>" ; <IN> <stdin.h> | |
5bd52f0e | 5917 | "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN |
f83d2997 KH |
5918 | "-[0-9]" ; -5 |
5919 | "\\+\\+" ; ++var | |
5920 | "--" ; --var | |
5921 | ".->" ; a->b | |
5922 | "->" ; a SPACE ->b | |
5923 | "\\[-" ; a[-1] | |
5bd52f0e | 5924 | "\\\\[&$@*\\\\]" ; \&func |
f83d2997 | 5925 | "^=" ; =head |
5bd52f0e RS |
5926 | "\\$." ; $| |
5927 | "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO' | |
f83d2997 KH |
5928 | "||" |
5929 | "&&" | |
5930 | "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text> | |
5931 | "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value | |
5932 | ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below | |
5933 | ;;"[*/+-|&<.]+=" | |
5934 | ) | |
5935 | "\\|") | |
5936 | "If matches at the start of match found by `my-bad-c-style-regexp', | |
5937 | insertion of a whitespace will not help.") | |
5938 | ||
5939 | (defvar found-bad) | |
5940 | ||
5941 | (defun cperl-find-bad-style () | |
5942 | "Find places in the buffer where insertion of a whitespace may help. | |
5943 | Prompts user for insertion of spaces. | |
5944 | Currently it is tuned to C and Perl syntax." | |
5945 | (interactive) | |
5946 | (let (found-bad (p (point))) | |
5947 | (setq last-nonmenu-event 13) ; To disable popup | |
5948 | (beginning-of-buffer) | |
5949 | (map-y-or-n-p "Insert space here? " | |
5950 | (function (lambda (arg) (insert " "))) | |
5951 | 'cperl-next-bad-style | |
5c8b7eaf | 5952 | '("location" "locations" "insert a space into") |
f83d2997 KH |
5953 | '((?\C-r (lambda (arg) |
5954 | (let ((buffer-quit-function | |
5955 | 'exit-recursive-edit)) | |
5956 | (message "Exit with Esc Esc") | |
5957 | (recursive-edit) | |
5958 | t)) ; Consider acted upon | |
5c8b7eaf | 5959 | "edit, exit with Esc Esc") |
f83d2997 KH |
5960 | (?e (lambda (arg) |
5961 | (let ((buffer-quit-function | |
5962 | 'exit-recursive-edit)) | |
5963 | (message "Exit with Esc Esc") | |
5964 | (recursive-edit) | |
5965 | t)) ; Consider acted upon | |
5966 | "edit, exit with Esc Esc")) | |
5967 | t) | |
5968 | (if found-bad (goto-char found-bad) | |
5969 | (goto-char p) | |
5970 | (message "No appropriate place found")))) | |
5971 | ||
5972 | (defun cperl-next-bad-style () | |
5973 | (let (p (not-found t) (point (point)) found) | |
5974 | (while (and not-found | |
5975 | (re-search-forward cperl-bad-style-regexp nil 'to-end)) | |
5976 | (setq p (point)) | |
5977 | (goto-char (match-beginning 0)) | |
5978 | (if (or | |
5979 | (looking-at cperl-not-bad-style-regexp) | |
5980 | ;; Check for a < -b and friends | |
5981 | (and (eq (following-char) ?\-) | |
5982 | (save-excursion | |
5983 | (skip-chars-backward " \t\n") | |
5984 | (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\(, ?\[, ?\{)))) | |
5985 | ;; Now check for syntax type | |
5986 | (save-match-data | |
5987 | (setq found (point)) | |
5988 | (beginning-of-defun) | |
5989 | (let ((pps (parse-partial-sexp (point) found))) | |
5990 | (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))) | |
5991 | (goto-char (match-end 0)) | |
5992 | (goto-char (1- p)) | |
5993 | (setq not-found nil | |
5994 | found-bad found))) | |
5995 | (not not-found))) | |
5996 | ||
f1d851ae | 5997 | \f |
f83d2997 | 5998 | ;;; Getting help |
5c8b7eaf | 5999 | (defvar cperl-have-help-regexp |
f83d2997 KH |
6000 | ;;(concat "\\(" |
6001 | (mapconcat | |
6002 | 'identity | |
6003 | '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable | |
6004 | "[$@]\\^[a-zA-Z]" ; Special variable | |
6005 | "[$@][^ \n\t]" ; Special variable | |
6006 | "-[a-zA-Z]" ; File test | |
6007 | "\\\\[a-zA-Z0]" ; Special chars | |
6008 | "^=[a-z][a-zA-Z0-9_]*" ; Pod sections | |
6009 | "[-!&*+,-./<=>?\\\\^|~]+" ; Operator | |
6010 | "[a-zA-Z_0-9:]+" ; symbol or number | |
6011 | "x=" | |
6012 | "#!" | |
6013 | ) | |
6014 | ;;"\\)\\|\\(" | |
6015 | "\\|" | |
6016 | ) | |
6017 | ;;"\\)" | |
6018 | ;;) | |
6019 | "Matches places in the buffer we can find help for.") | |
6020 | ||
6021 | (defvar cperl-message-on-help-error t) | |
6022 | (defvar cperl-help-from-timer nil) | |
6023 | ||
6024 | (defun cperl-word-at-point-hard () | |
6025 | ;; Does not save-excursion | |
6026 | ;; Get to the something meaningful | |
6027 | (or (eobp) (eolp) (forward-char 1)) | |
5c8b7eaf | 6028 | (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]" |
f83d2997 KH |
6029 | (save-excursion (beginning-of-line) (point)) |
6030 | 'to-beg) | |
6031 | ;; (cond | |
6032 | ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol | |
6033 | ;; (skip-chars-backward " \n\t\r({[]});,") | |
6034 | ;; (or (bobp) (backward-char 1)))) | |
6035 | ;; Try to backtrace | |
6036 | (cond | |
6037 | ((looking-at "[a-zA-Z0-9_:]") ; symbol | |
6038 | (skip-chars-backward "a-zA-Z0-9_:") | |
5c8b7eaf | 6039 | (cond |
f83d2997 KH |
6040 | ((and (eq (preceding-char) ?^) ; $^I |
6041 | (eq (char-after (- (point) 2)) ?\$)) | |
6042 | (forward-char -2)) | |
6043 | ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob | |
6044 | (forward-char -1)) | |
6045 | ((and (eq (preceding-char) ?\=) | |
6046 | (eq (current-column) 1)) | |
6047 | (forward-char -1))) ; =head1 | |
6048 | (if (and (eq (preceding-char) ?\<) | |
6049 | (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH> | |
6050 | (forward-char -1))) | |
6051 | ((and (looking-at "=") (eq (preceding-char) ?x)) ; x= | |
6052 | (forward-char -1)) | |
6053 | ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I | |
6054 | (forward-char -1)) | |
6055 | ((looking-at "[-!&*+,-./<=>?\\\\^|~]") | |
6056 | (skip-chars-backward "-!&*+,-./<=>?\\\\^|~") | |
6057 | (cond | |
6058 | ((and (eq (preceding-char) ?\$) | |
6059 | (not (eq (char-after (- (point) 2)) ?\$))) ; $- | |
6060 | (forward-char -1)) | |
6061 | ((and (eq (following-char) ?\>) | |
6062 | (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char))) | |
6063 | (save-excursion | |
6064 | (forward-sexp -1) | |
6065 | (and (eq (preceding-char) ?\<) | |
6066 | (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH> | |
6067 | (search-backward "<")))) | |
6068 | ((and (eq (following-char) ?\$) | |
6069 | (eq (preceding-char) ?\<) | |
6070 | (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh> | |
6071 | (forward-char -1))) | |
6072 | (if (looking-at cperl-have-help-regexp) | |
6073 | (buffer-substring (match-beginning 0) (match-end 0)))) | |
6074 | ||
6075 | (defun cperl-get-help () | |
6076 | "Get one-line docs on the symbol at the point. | |
6077 | The data for these docs is a little bit obsolete and may be in fact longer | |
6078 | than a line. Your contribution to update/shorten it is appreciated." | |
6079 | (interactive) | |
6080 | (save-match-data ; May be called "inside" query-replace | |
6081 | (save-excursion | |
6082 | (let ((word (cperl-word-at-point-hard))) | |
6083 | (if word | |
6084 | (if (and cperl-help-from-timer ; Bail out if not in mainland | |
6085 | (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings. | |
6086 | (or (memq (get-text-property (point) 'face) | |
6087 | '(font-lock-comment-face font-lock-string-face)) | |
6088 | (memq (get-text-property (point) 'syntax-type) | |
6089 | '(pod here-doc format)))) | |
6090 | nil | |
6091 | (cperl-describe-perl-symbol word)) | |
6092 | (if cperl-message-on-help-error | |
5c8b7eaf | 6093 | (message "Nothing found for %s..." |
f83d2997 KH |
6094 | (buffer-substring (point) (min (+ 5 (point)) (point-max)))))))))) |
6095 | ||
6096 | ;;; Stolen from perl-descr.el by Johan Vromans: | |
6097 | ||
6098 | (defvar cperl-doc-buffer " *perl-doc*" | |
6099 | "Where the documentation can be found.") | |
6100 | ||
6101 | (defun cperl-describe-perl-symbol (val) | |
6102 | "Display the documentation of symbol at point, a Perl operator." | |
6103 | (let ((enable-recursive-minibuffers t) | |
6104 | args-file regexp) | |
6105 | (cond | |
6106 | ((string-match "^[&*][a-zA-Z_]" val) | |
6107 | (setq val (concat (substring val 0 1) "NAME"))) | |
6108 | ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val) | |
6109 | (setq val (concat "@" (substring val 1 (match-end 1))))) | |
6110 | ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val) | |
6111 | (setq val (concat "%" (substring val 1 (match-end 1))))) | |
6112 | ((and (string= val "x") (string-match "^x=" val)) | |
6113 | (setq val "x=")) | |
6114 | ((string-match "^\\$[\C-a-\C-z]" val) | |
6115 | (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1)))))) | |
6116 | ((string-match "^CORE::" val) | |
6117 | (setq val "CORE::")) | |
6118 | ((string-match "^SUPER::" val) | |
6119 | (setq val "SUPER::")) | |
6120 | ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val)) | |
6121 | (setq val "<NAME>"))) | |
5c8b7eaf | 6122 | (setq regexp (concat "^" |
f83d2997 | 6123 | "\\([^a-zA-Z0-9_:]+[ \t]+\\)?" |
5c8b7eaf | 6124 | (regexp-quote val) |
f83d2997 KH |
6125 | "\\([ \t([/]\\|$\\)")) |
6126 | ||
6127 | ;; get the buffer with the documentation text | |
6128 | (cperl-switch-to-doc-buffer) | |
6129 | ||
6130 | ;; lookup in the doc | |
6131 | (goto-char (point-min)) | |
6132 | (let ((case-fold-search nil)) | |
5c8b7eaf | 6133 | (list |
f83d2997 KH |
6134 | (if (re-search-forward regexp (point-max) t) |
6135 | (save-excursion | |
6136 | (beginning-of-line 1) | |
6137 | (let ((lnstart (point))) | |
6138 | (end-of-line) | |
6139 | (message "%s" (buffer-substring lnstart (point))))) | |
6140 | (if cperl-message-on-help-error | |
6141 | (message "No definition for %s" val))))))) | |
6142 | ||
6143 | (defvar cperl-short-docs "Ignore my value" | |
6144 | ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl) | |
6145 | "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5] | |
5c8b7eaf | 6146 | ! ... Logical negation. |
f83d2997 KH |
6147 | ... != ... Numeric inequality. |
6148 | ... !~ ... Search pattern, substitution, or translation (negated). | |
6149 | $! In numeric context: errno. In a string context: error string. | |
6150 | $\" The separator which joins elements of arrays interpolated in strings. | |
6151 | $# The output format for printed numbers. Initial value is %.15g or close. | |
6152 | $$ Process number of this script. Changes in the fork()ed child process. | |
6153 | $% The current page number of the currently selected output channel. | |
6154 | ||
6155 | The following variables are always local to the current block: | |
6156 | ||
6157 | $1 Match of the 1st set of parentheses in the last match (auto-local). | |
6158 | $2 Match of the 2nd set of parentheses in the last match (auto-local). | |
6159 | $3 Match of the 3rd set of parentheses in the last match (auto-local). | |
6160 | $4 Match of the 4th set of parentheses in the last match (auto-local). | |
6161 | $5 Match of the 5th set of parentheses in the last match (auto-local). | |
6162 | $6 Match of the 6th set of parentheses in the last match (auto-local). | |
6163 | $7 Match of the 7th set of parentheses in the last match (auto-local). | |
6164 | $8 Match of the 8th set of parentheses in the last match (auto-local). | |
6165 | $9 Match of the 9th set of parentheses in the last match (auto-local). | |
6166 | $& The string matched by the last pattern match (auto-local). | |
6167 | $' The string after what was matched by the last match (auto-local). | |
6168 | $` The string before what was matched by the last match (auto-local). | |
6169 | ||
6170 | $( The real gid of this process. | |
6171 | $) The effective gid of this process. | |
6172 | $* Deprecated: Set to 1 to do multiline matching within a string. | |
6173 | $+ The last bracket matched by the last search pattern. | |
6174 | $, The output field separator for the print operator. | |
6175 | $- The number of lines left on the page. | |
6176 | $. The current input line number of the last filehandle that was read. | |
6177 | $/ The input record separator, newline by default. | |
6178 | $0 Name of the file containing the perl script being executed. May be set. | |
6179 | $: String may be broken after these characters to fill ^-lines in a format. | |
6180 | $; Subscript separator for multi-dim array emulation. Default \"\\034\". | |
6181 | $< The real uid of this process. | |
6182 | $= The page length of the current output channel. Default is 60 lines. | |
6183 | $> The effective uid of this process. | |
6184 | $? The status returned by the last ``, pipe close or `system'. | |
6185 | $@ The perl error message from the last eval or do @var{EXPR} command. | |
6186 | $ARGV The name of the current file used with <> . | |
6187 | $[ Deprecated: The index of the first element/char in an array/string. | |
6188 | $\\ The output record separator for the print operator. | |
6189 | $] The perl version string as displayed with perl -v. | |
6190 | $^ The name of the current top-of-page format. | |
6191 | $^A The current value of the write() accumulator for format() lines. | |
6192 | $^D The value of the perl debug (-D) flags. | |
6193 | $^E Information about the last system error other than that provided by $!. | |
6194 | $^F The highest system file descriptor, ordinarily 2. | |
6195 | $^H The current set of syntax checks enabled by `use strict'. | |
6196 | $^I The value of the in-place edit extension (perl -i option). | |
6197 | $^L What formats output to perform a formfeed. Default is \f. | |
5bd52f0e | 6198 | $^M A buffer for emergency memory allocation when running out of memory. |
f83d2997 KH |
6199 | $^O The operating system name under which this copy of Perl was built. |
6200 | $^P Internal debugging flag. | |
6201 | $^T The time the script was started. Used by -A/-M/-C file tests. | |
6202 | $^W True if warnings are requested (perl -w flag). | |
6203 | $^X The name under which perl was invoked (argv[0] in C-speech). | |
6204 | $_ The default input and pattern-searching space. | |
5c8b7eaf | 6205 | $| Auto-flush after write/print on current output channel? Default 0. |
f83d2997 KH |
6206 | $~ The name of the current report format. |
6207 | ... % ... Modulo division. | |
6208 | ... %= ... Modulo division assignment. | |
6209 | %ENV Contains the current environment. | |
6210 | %INC List of files that have been require-d or do-ne. | |
6211 | %SIG Used to set signal handlers for various signals. | |
6212 | ... & ... Bitwise and. | |
6213 | ... && ... Logical and. | |
6214 | ... &&= ... Logical and assignment. | |
6215 | ... &= ... Bitwise and assignment. | |
6216 | ... * ... Multiplication. | |
6217 | ... ** ... Exponentiation. | |
6218 | *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2. | |
6219 | &NAME(arg0, ...) Subroutine call. Arguments go to @_. | |
6220 | ... + ... Addition. +EXPR Makes EXPR into scalar context. | |
6221 | ++ Auto-increment (magical on strings). ++EXPR EXPR++ | |
6222 | ... += ... Addition assignment. | |
6223 | , Comma operator. | |
6224 | ... - ... Subtraction. | |
6225 | -- Auto-decrement (NOT magical on strings). --EXPR EXPR-- | |
6226 | ... -= ... Subtraction assignment. | |
6227 | -A Access time in days since script started. | |
6228 | -B File is a non-text (binary) file. | |
6229 | -C Inode change time in days since script started. | |
6230 | -M Age in days since script started. | |
6231 | -O File is owned by real uid. | |
6232 | -R File is readable by real uid. | |
6233 | -S File is a socket . | |
6234 | -T File is a text file. | |
6235 | -W File is writable by real uid. | |
6236 | -X File is executable by real uid. | |
6237 | -b File is a block special file. | |
6238 | -c File is a character special file. | |
6239 | -d File is a directory. | |
6240 | -e File exists . | |
6241 | -f File is a plain file. | |
6242 | -g File has setgid bit set. | |
6243 | -k File has sticky bit set. | |
6244 | -l File is a symbolic link. | |
6245 | -o File is owned by effective uid. | |
6246 | -p File is a named pipe (FIFO). | |
6247 | -r File is readable by effective uid. | |
6248 | -s File has non-zero size. | |
6249 | -t Tests if filehandle (STDIN by default) is opened to a tty. | |
6250 | -u File has setuid bit set. | |
6251 | -w File is writable by effective uid. | |
6252 | -x File is executable by effective uid. | |
6253 | -z File has zero size. | |
6254 | . Concatenate strings. | |
6255 | .. Alternation, also range operator. | |
6256 | .= Concatenate assignment strings | |
6257 | ... / ... Division. /PATTERN/ioxsmg Pattern match | |
6258 | ... /= ... Division assignment. | |
6259 | /PATTERN/ioxsmg Pattern match. | |
6260 | ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well. | |
6261 | <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword). | |
6262 | <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>). | |
6263 | <> Reads line from union of files in @ARGV (= command line) and STDIN. | |
6264 | ... << ... Bitwise shift left. << start of HERE-DOCUMENT. | |
6265 | ... <= ... Numeric less than or equal to. | |
6266 | ... <=> ... Numeric compare. | |
6267 | ... = ... Assignment. | |
6268 | ... == ... Numeric equality. | |
6269 | ... =~ ... Search pattern, substitution, or translation | |
6270 | ... > ... Numeric greater than. | |
6271 | ... >= ... Numeric greater than or equal to. | |
6272 | ... >> ... Bitwise shift right. | |
6273 | ... >>= ... Bitwise shift right assignment. | |
6274 | ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match. | |
6275 | ?PATTERN? One-time pattern match. | |
6276 | @ARGV Command line arguments (not including the command name - see $0). | |
6277 | @INC List of places to look for perl scripts during do/include/use. | |
6278 | @_ Parameter array for subroutines. Also used by split unless in array context. | |
6279 | \\ Creates reference to what follows, like \$var, or quotes non-\w in strings. | |
6280 | \\0 Octal char, e.g. \\033. | |
6281 | \\E Case modification terminator. See \\Q, \\L, and \\U. | |
6282 | \\L Lowercase until \\E . See also \l, lc. | |
6283 | \\U Upcase until \\E . See also \u, uc. | |
6284 | \\Q Quote metacharacters until \\E . See also quotemeta. | |
6285 | \\a Alarm character (octal 007). | |
6286 | \\b Backspace character (octal 010). | |
6287 | \\c Control character, e.g. \\c[ . | |
6288 | \\e Escape character (octal 033). | |
6289 | \\f Formfeed character (octal 014). | |
6290 | \\l Lowercase the next character. See also \\L and \\u, lcfirst. | |
6291 | \\n Newline character (octal 012 on most systems). | |
6292 | \\r Return character (octal 015 on most systems). | |
6293 | \\t Tab character (octal 011). | |
6294 | \\u Upcase the next character. See also \\U and \\l, ucfirst. | |
6295 | \\x Hex character, e.g. \\x1b. | |
6296 | ... ^ ... Bitwise exclusive or. | |
6297 | __END__ Ends program source. | |
6298 | __DATA__ Ends program source. | |
6299 | __FILE__ Current (source) filename. | |
6300 | __LINE__ Current line in current source. | |
6301 | __PACKAGE__ Current package. | |
6302 | ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>. | |
6303 | ARGVOUT Output filehandle with -i flag. | |
6304 | BEGIN { ... } Immediately executed (during compilation) piece of code. | |
6305 | END { ... } Pseudo-subroutine executed after the script finishes. | |
6c389151 SM |
6306 | CHECK { ... } Pseudo-subroutine executed after the script is compiled. |
6307 | INIT { ... } Pseudo-subroutine executed before the script starts running. | |
f83d2997 KH |
6308 | DATA Input filehandle for what follows after __END__ or __DATA__. |
6309 | accept(NEWSOCKET,GENERICSOCKET) | |
6310 | alarm(SECONDS) | |
6311 | atan2(X,Y) | |
6312 | bind(SOCKET,NAME) | |
6313 | binmode(FILEHANDLE) | |
6314 | caller[(LEVEL)] | |
6315 | chdir(EXPR) | |
6316 | chmod(LIST) | |
6317 | chop[(LIST|VAR)] | |
6318 | chown(LIST) | |
6319 | chroot(FILENAME) | |
6320 | close(FILEHANDLE) | |
6321 | closedir(DIRHANDLE) | |
6322 | ... cmp ... String compare. | |
6323 | connect(SOCKET,NAME) | |
6324 | continue of { block } continue { block }. Is executed after `next' or at end. | |
6325 | cos(EXPR) | |
6326 | crypt(PLAINTEXT,SALT) | |
6327 | dbmclose(%HASH) | |
6328 | dbmopen(%HASH,DBNAME,MODE) | |
6329 | defined(EXPR) | |
6330 | delete($HASH{KEY}) | |
6331 | die(LIST) | |
6332 | do { ... }|SUBR while|until EXPR executes at least once | |
6333 | do(EXPR|SUBR([LIST])) (with while|until executes at least once) | |
6334 | dump LABEL | |
6335 | each(%HASH) | |
6336 | endgrent | |
6337 | endhostent | |
6338 | endnetent | |
6339 | endprotoent | |
6340 | endpwent | |
6341 | endservent | |
6342 | eof[([FILEHANDLE])] | |
6343 | ... eq ... String equality. | |
6344 | eval(EXPR) or eval { BLOCK } | |
6345 | exec(LIST) | |
6346 | exit(EXPR) | |
6347 | exp(EXPR) | |
6348 | fcntl(FILEHANDLE,FUNCTION,SCALAR) | |
6349 | fileno(FILEHANDLE) | |
6350 | flock(FILEHANDLE,OPERATION) | |
6351 | for (EXPR;EXPR;EXPR) { ... } | |
6352 | foreach [VAR] (@ARRAY) { ... } | |
6353 | fork | |
6354 | ... ge ... String greater than or equal. | |
6355 | getc[(FILEHANDLE)] | |
6356 | getgrent | |
6357 | getgrgid(GID) | |
6358 | getgrnam(NAME) | |
6359 | gethostbyaddr(ADDR,ADDRTYPE) | |
6360 | gethostbyname(NAME) | |
6361 | gethostent | |
6362 | getlogin | |
6363 | getnetbyaddr(ADDR,ADDRTYPE) | |
6364 | getnetbyname(NAME) | |
6365 | getnetent | |
6366 | getpeername(SOCKET) | |
6367 | getpgrp(PID) | |
6368 | getppid | |
6369 | getpriority(WHICH,WHO) | |
6370 | getprotobyname(NAME) | |
6371 | getprotobynumber(NUMBER) | |
6372 | getprotoent | |
6373 | getpwent | |
6374 | getpwnam(NAME) | |
6375 | getpwuid(UID) | |
6376 | getservbyname(NAME,PROTO) | |
6377 | getservbyport(PORT,PROTO) | |
6378 | getservent | |
6379 | getsockname(SOCKET) | |
6380 | getsockopt(SOCKET,LEVEL,OPTNAME) | |
6381 | gmtime(EXPR) | |
6382 | goto LABEL | |
f83d2997 KH |
6383 | ... gt ... String greater than. |
6384 | hex(EXPR) | |
6385 | if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR | |
6386 | index(STR,SUBSTR[,OFFSET]) | |
6387 | int(EXPR) | |
6388 | ioctl(FILEHANDLE,FUNCTION,SCALAR) | |
6389 | join(EXPR,LIST) | |
6390 | keys(%HASH) | |
6391 | kill(LIST) | |
6392 | last [LABEL] | |
6393 | ... le ... String less than or equal. | |
6394 | length(EXPR) | |
6395 | link(OLDFILE,NEWFILE) | |
6396 | listen(SOCKET,QUEUESIZE) | |
6397 | local(LIST) | |
6398 | localtime(EXPR) | |
6399 | log(EXPR) | |
6400 | lstat(EXPR|FILEHANDLE|VAR) | |
6401 | ... lt ... String less than. | |
6402 | m/PATTERN/iogsmx | |
6403 | mkdir(FILENAME,MODE) | |
6404 | msgctl(ID,CMD,ARG) | |
6405 | msgget(KEY,FLAGS) | |
6406 | msgrcv(ID,VAR,SIZE,TYPE.FLAGS) | |
6407 | msgsnd(ID,MSG,FLAGS) | |
6408 | my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH). | |
6c389151 | 6409 | our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H). |
f83d2997 KH |
6410 | ... ne ... String inequality. |
6411 | next [LABEL] | |
6412 | oct(EXPR) | |
6413 | open(FILEHANDLE[,EXPR]) | |
6414 | opendir(DIRHANDLE,EXPR) | |
6415 | ord(EXPR) ASCII value of the first char of the string. | |
6416 | pack(TEMPLATE,LIST) | |
6417 | package NAME Introduces package context. | |
6418 | pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe. | |
6419 | pop(ARRAY) | |
6420 | print [FILEHANDLE] [(LIST)] | |
6421 | printf [FILEHANDLE] (FORMAT,LIST) | |
6422 | push(ARRAY,LIST) | |
6423 | q/STRING/ Synonym for 'STRING' | |
6424 | qq/STRING/ Synonym for \"STRING\" | |
6425 | qx/STRING/ Synonym for `STRING` | |
6426 | rand[(EXPR)] | |
6427 | read(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6428 | readdir(DIRHANDLE) | |
6429 | readlink(EXPR) | |
6430 | recv(SOCKET,SCALAR,LEN,FLAGS) | |
6431 | redo [LABEL] | |
6432 | rename(OLDNAME,NEWNAME) | |
6433 | require [FILENAME | PERL_VERSION] | |
6434 | reset[(EXPR)] | |
6435 | return(LIST) | |
6436 | reverse(LIST) | |
6437 | rewinddir(DIRHANDLE) | |
6438 | rindex(STR,SUBSTR[,OFFSET]) | |
6439 | rmdir(FILENAME) | |
6440 | s/PATTERN/REPLACEMENT/gieoxsm | |
6441 | scalar(EXPR) | |
6442 | seek(FILEHANDLE,POSITION,WHENCE) | |
6443 | seekdir(DIRHANDLE,POS) | |
6444 | select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT) | |
6445 | semctl(ID,SEMNUM,CMD,ARG) | |
6446 | semget(KEY,NSEMS,SIZE,FLAGS) | |
6447 | semop(KEY,...) | |
6448 | send(SOCKET,MSG,FLAGS[,TO]) | |
6449 | setgrent | |
6450 | sethostent(STAYOPEN) | |
6451 | setnetent(STAYOPEN) | |
6452 | setpgrp(PID,PGRP) | |
6453 | setpriority(WHICH,WHO,PRIORITY) | |
6454 | setprotoent(STAYOPEN) | |
6455 | setpwent | |
6456 | setservent(STAYOPEN) | |
6457 | setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL) | |
6458 | shift[(ARRAY)] | |
6459 | shmctl(ID,CMD,ARG) | |
6460 | shmget(KEY,SIZE,FLAGS) | |
6461 | shmread(ID,VAR,POS,SIZE) | |
6462 | shmwrite(ID,STRING,POS,SIZE) | |
6463 | shutdown(SOCKET,HOW) | |
6464 | sin(EXPR) | |
6465 | sleep[(EXPR)] | |
6466 | socket(SOCKET,DOMAIN,TYPE,PROTOCOL) | |
6467 | socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL) | |
6468 | sort [SUBROUTINE] (LIST) | |
6469 | splice(ARRAY,OFFSET[,LENGTH[,LIST]]) | |
6470 | split[(/PATTERN/[,EXPR[,LIMIT]])] | |
6471 | sprintf(FORMAT,LIST) | |
6472 | sqrt(EXPR) | |
6473 | srand(EXPR) | |
6474 | stat(EXPR|FILEHANDLE|VAR) | |
6475 | study[(SCALAR)] | |
6476 | sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...} | |
6477 | substr(EXPR,OFFSET[,LEN]) | |
6478 | symlink(OLDFILE,NEWFILE) | |
6479 | syscall(LIST) | |
6480 | sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6481 | system(LIST) | |
6482 | syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6483 | tell[(FILEHANDLE)] | |
6484 | telldir(DIRHANDLE) | |
6485 | time | |
6486 | times | |
6487 | tr/SEARCHLIST/REPLACEMENTLIST/cds | |
6488 | truncate(FILE|EXPR,LENGTH) | |
6489 | umask[(EXPR)] | |
6490 | undef[(EXPR)] | |
6491 | unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR | |
6492 | unlink(LIST) | |
6493 | unpack(TEMPLATE,EXPR) | |
6494 | unshift(ARRAY,LIST) | |
6495 | until (EXPR) { ... } EXPR until EXPR | |
6496 | utime(LIST) | |
6497 | values(%HASH) | |
6498 | vec(EXPR,OFFSET,BITS) | |
6499 | wait | |
6500 | waitpid(PID,FLAGS) | |
6501 | wantarray Returns true if the sub/eval is called in list context. | |
6502 | warn(LIST) | |
6503 | while (EXPR) { ... } EXPR while EXPR | |
6504 | write[(EXPR|FILEHANDLE)] | |
6505 | ... x ... Repeat string or array. | |
6506 | x= ... Repetition assignment. | |
6507 | y/SEARCHLIST/REPLACEMENTLIST/ | |
6508 | ... | ... Bitwise or. | |
6509 | ... || ... Logical or. | |
6510 | ~ ... Unary bitwise complement. | |
db133cb6 | 6511 | #! OS interpreter indicator. If contains `perl', used for options, and -x. |
f83d2997 KH |
6512 | AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'. |
6513 | CORE:: Prefix to access builtin function if imported sub obscures it. | |
6514 | SUPER:: Prefix to lookup for a method in @ISA classes. | |
6515 | DESTROY Shorthand for `sub DESTROY {...}'. | |
6516 | ... EQ ... Obsolete synonym of `eq'. | |
6517 | ... GE ... Obsolete synonym of `ge'. | |
6518 | ... GT ... Obsolete synonym of `gt'. | |
6519 | ... LE ... Obsolete synonym of `le'. | |
6520 | ... LT ... Obsolete synonym of `lt'. | |
6521 | ... NE ... Obsolete synonym of `ne'. | |
6522 | abs [ EXPR ] absolute value | |
6523 | ... and ... Low-precedence synonym for &&. | |
6524 | bless REFERENCE [, PACKAGE] Makes reference into an object of a package. | |
6525 | chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''! | |
6526 | chr Converts a number to char with the same ordinal. | |
6527 | else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}. | |
6528 | elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}. | |
6529 | exists $HASH{KEY} True if the key exists. | |
6530 | format [NAME] = Start of output format. Ended by a single dot (.) on a line. | |
6531 | formline PICTURE, LIST Backdoor into \"format\" processing. | |
6532 | glob EXPR Synonym of <EXPR>. | |
6533 | lc [ EXPR ] Returns lowercased EXPR. | |
6534 | lcfirst [ EXPR ] Returns EXPR with lower-cased first letter. | |
db133cb6 | 6535 | grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK. |
f83d2997 KH |
6536 | map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST. |
6537 | no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method. | |
6538 | not ... Low-precedence synonym for ! - negation. | |
6539 | ... or ... Low-precedence synonym for ||. | |
6540 | pos STRING Set/Get end-position of the last match over this string, see \\G. | |
6541 | quotemeta [ EXPR ] Quote regexp metacharacters. | |
6542 | qw/WORD1 .../ Synonym of split('', 'WORD1 ...') | |
6543 | readline FH Synonym of <FH>. | |
6544 | readpipe CMD Synonym of `CMD`. | |
6545 | ref [ EXPR ] Type of EXPR when dereferenced. | |
6546 | sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.) | |
6547 | tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable. | |
6548 | tied Returns internal object for a tied data. | |
6549 | uc [ EXPR ] Returns upcased EXPR. | |
6550 | ucfirst [ EXPR ] Returns EXPR with upcased first letter. | |
6551 | untie VAR Unlink an object from a simple Perl variable. | |
6552 | use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'. | |
6553 | ... xor ... Low-precedence synonym for exclusive or. | |
6554 | prototype \&SUB Returns prototype of the function given a reference. | |
6555 | =head1 Top-level heading. | |
6556 | =head2 Second-level heading. | |
6557 | =head3 Third-level heading (is there such?). | |
6558 | =over [ NUMBER ] Start list. | |
6559 | =item [ TITLE ] Start new item in the list. | |
6560 | =back End list. | |
6561 | =cut Switch from POD to Perl. | |
6562 | =pod Switch from Perl to POD. | |
6563 | ") | |
6564 | ||
6565 | (defun cperl-switch-to-doc-buffer () | |
6566 | "Go to the perl documentation buffer and insert the documentation." | |
6567 | (interactive) | |
6568 | (let ((buf (get-buffer-create cperl-doc-buffer))) | |
6569 | (if (interactive-p) | |
6570 | (switch-to-buffer-other-window buf) | |
6571 | (set-buffer buf)) | |
6572 | (if (= (buffer-size) 0) | |
6573 | (progn | |
6574 | (insert (documentation-property 'cperl-short-docs | |
6575 | 'variable-documentation)) | |
6576 | (setq buffer-read-only t))))) | |
6577 | ||
6c389151 | 6578 | (defun cperl-beautify-regexp-piece (b e embed level) |
f83d2997 KH |
6579 | ;; b is before the starting delimiter, e before the ending |
6580 | ;; e should be a marker, may be changed, but remains "correct". | |
6c389151 SM |
6581 | ;; EMBED is nil iff we process the whole REx. |
6582 | ;; The REx is guarantied to have //x | |
6583 | ;; LEVEL shows how many levels deep to go | |
6584 | ;; position at enter and at leave is not defined | |
6585 | (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos) | |
f83d2997 KH |
6586 | (if (not embed) |
6587 | (goto-char (1+ b)) | |
6588 | (goto-char b) | |
6c389151 | 6589 | (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing |
f83d2997 KH |
6590 | (forward-char 2) |
6591 | (delete-char 1) | |
6592 | (forward-char 1)) | |
6593 | ((looking-at "(\\?[^a-zA-Z]") | |
6594 | (forward-char 3)) | |
6595 | ((looking-at "(\\?") ; (?i) | |
6596 | (forward-char 2)) | |
6597 | (t | |
6598 | (forward-char 1)))) | |
6599 | (setq c (if embed (current-indentation) (1- (current-column))) | |
6600 | c1 (+ c (or cperl-regexp-indent-step cperl-indent-level))) | |
6601 | (or (looking-at "[ \t]*[\n#]") | |
6602 | (progn | |
6603 | (insert "\n"))) | |
6604 | (goto-char e) | |
6605 | (beginning-of-line) | |
6606 | (if (re-search-forward "[^ \t]" e t) | |
6c389151 | 6607 | (progn ; Something before the ending delimiter |
f83d2997 | 6608 | (goto-char e) |
6c389151 | 6609 | (delete-horizontal-space) |
f83d2997 KH |
6610 | (insert "\n") |
6611 | (indent-to-column c) | |
6612 | (set-marker e (point)))) | |
6613 | (goto-char b) | |
6614 | (end-of-line 2) | |
6615 | (while (< (point) (marker-position e)) | |
6616 | (beginning-of-line) | |
6617 | (setq s (point) | |
6618 | inline t) | |
6619 | (skip-chars-forward " \t") | |
6620 | (delete-region s (point)) | |
6621 | (indent-to-column c1) | |
6622 | (while (and | |
6623 | inline | |
5c8b7eaf | 6624 | (looking-at |
f83d2997 KH |
6625 | (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word |
6626 | "\\|" ; Embedded variable | |
6627 | "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3 | |
6628 | "\\|" ; $ ^ | |
6629 | "[$^]" | |
6630 | "\\|" ; simple-code simple-code*? | |
6631 | "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5 | |
6632 | "\\|" ; Class | |
6633 | "\\(\\[\\)" ; 6 | |
6634 | "\\|" ; Grouping | |
6635 | "\\((\\(\\?\\)?\\)" ; 7 8 | |
6636 | "\\|" ; | | |
6637 | "\\(|\\)" ; 9 | |
6638 | ))) | |
6639 | (goto-char (match-end 0)) | |
6640 | (setq spaces t) | |
6641 | (cond ((match-beginning 1) ; Alphanum word + junk | |
6642 | (forward-char -1)) | |
6643 | ((or (match-beginning 3) ; $ab[12] | |
6644 | (and (match-beginning 5) ; X* X+ X{2,3} | |
6645 | (eq (preceding-char) ?\{))) | |
6646 | (forward-char -1) | |
6647 | (forward-sexp 1)) | |
6648 | ((match-beginning 6) ; [] | |
6649 | (setq tmp (point)) | |
6650 | (if (looking-at "\\^?\\]") | |
6651 | (goto-char (match-end 0))) | |
6c389151 SM |
6652 | ;; XXXX POSIX classes?! |
6653 | (while (and (not pos) | |
6654 | (re-search-forward "\\[:\\|\\]" e t)) | |
6655 | (if (eq (preceding-char) ?:) | |
6656 | (or (re-search-forward ":\\]" e t) | |
6657 | (error "[:POSIX:]-group in []-group not terminated")) | |
6658 | (setq pos t))) | |
6659 | (or (eq (preceding-char) ?\]) | |
6660 | (error "[]-group not terminated")) | |
6661 | (if (eq (following-char) ?\{) | |
f83d2997 | 6662 | (progn |
6c389151 SM |
6663 | (forward-sexp 1) |
6664 | (and (eq (following-char) ??) | |
6665 | (forward-char 1))) | |
6666 | (re-search-forward "\\=\\([*+?]\\??\\)" e t))) | |
f83d2997 KH |
6667 | ((match-beginning 7) ; () |
6668 | (goto-char (match-beginning 0)) | |
6c389151 SM |
6669 | (setq pos (current-column)) |
6670 | (or (eq pos c1) | |
f83d2997 | 6671 | (progn |
6c389151 | 6672 | (delete-horizontal-space) |
f83d2997 KH |
6673 | (insert "\n") |
6674 | (indent-to-column c1))) | |
6675 | (setq tmp (point)) | |
6676 | (forward-sexp 1) | |
6677 | ;; (or (forward-sexp 1) | |
6678 | ;; (progn | |
6679 | ;; (goto-char tmp) | |
6680 | ;; (error "()-group not terminated"))) | |
6681 | (set-marker m (1- (point))) | |
6682 | (set-marker m1 (point)) | |
6c389151 SM |
6683 | (if (= level 1) |
6684 | (if (progn ; indent rigidly if multiline | |
6685 | ;; In fact does not make a lot of sense, since | |
6686 | ;; the starting position can be already lost due | |
6687 | ;; to insertion of "\n" and " " | |
6688 | (goto-char tmp) | |
6689 | (search-forward "\n" m1 t)) | |
6690 | (indent-rigidly (point) m1 (- c1 pos))) | |
6691 | (setq level (1- level)) | |
6692 | (cond | |
6693 | ((not (match-beginning 8)) | |
6694 | (cperl-beautify-regexp-piece tmp m t level)) | |
6695 | ((eq (char-after (+ 2 tmp)) ?\{) ; Code | |
6696 | t) | |
6697 | ((eq (char-after (+ 2 tmp)) ?\() ; Conditional | |
6698 | (goto-char (+ 2 tmp)) | |
6699 | (forward-sexp 1) | |
6700 | (cperl-beautify-regexp-piece (point) m t level)) | |
6701 | ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind | |
6702 | (goto-char (+ 3 tmp)) | |
6703 | (cperl-beautify-regexp-piece (point) m t level)) | |
6704 | (t | |
6705 | (cperl-beautify-regexp-piece tmp m t level)))) | |
f83d2997 KH |
6706 | (goto-char m1) |
6707 | (cond ((looking-at "[*+?]\\??") | |
6708 | (goto-char (match-end 0))) | |
6709 | ((eq (following-char) ?\{) | |
6710 | (forward-sexp 1) | |
6711 | (if (eq (following-char) ?\?) | |
6712 | (forward-char)))) | |
6713 | (skip-chars-forward " \t") | |
6714 | (setq spaces nil) | |
6715 | (if (looking-at "[#\n]") | |
6716 | (progn | |
6717 | (or (eolp) (indent-for-comment)) | |
6718 | (beginning-of-line 2)) | |
6c389151 | 6719 | (delete-horizontal-space) |
f83d2997 KH |
6720 | (insert "\n")) |
6721 | (end-of-line) | |
6722 | (setq inline nil)) | |
6723 | ((match-beginning 9) ; | | |
6724 | (forward-char -1) | |
6725 | (setq tmp (point)) | |
6726 | (beginning-of-line) | |
6727 | (if (re-search-forward "[^ \t]" tmp t) | |
6728 | (progn | |
6729 | (goto-char tmp) | |
6c389151 | 6730 | (delete-horizontal-space) |
f83d2997 KH |
6731 | (insert "\n")) |
6732 | ;; first at line | |
6733 | (delete-region (point) tmp)) | |
6734 | (indent-to-column c) | |
6735 | (forward-char 1) | |
6736 | (skip-chars-forward " \t") | |
6737 | (setq spaces nil) | |
6738 | (if (looking-at "[#\n]") | |
6739 | (beginning-of-line 2) | |
6c389151 | 6740 | (delete-horizontal-space) |
f83d2997 KH |
6741 | (insert "\n")) |
6742 | (end-of-line) | |
6743 | (setq inline nil))) | |
6744 | (or (looking-at "[ \t\n]") | |
6745 | (not spaces) | |
6746 | (insert " ")) | |
6747 | (skip-chars-forward " \t")) | |
6748 | (or (looking-at "[#\n]") | |
f94a632a | 6749 | (error "Unknown code `%s' in a regexp" |
6c389151 | 6750 | (buffer-substring (point) (1+ (point))))) |
f83d2997 KH |
6751 | (and inline (end-of-line 2))) |
6752 | ;; Special-case the last line of group | |
6753 | (if (and (>= (point) (marker-position e)) | |
6754 | (/= (current-indentation) c)) | |
6755 | (progn | |
6756 | (beginning-of-line) | |
6757 | (setq s (point)) | |
6758 | (skip-chars-forward " \t") | |
6759 | (delete-region s (point)) | |
6760 | (indent-to-column c))) | |
6761 | )) | |
6762 | ||
6763 | (defun cperl-make-regexp-x () | |
db133cb6 | 6764 | ;; Returns position of the start |
6c389151 | 6765 | ;; XXX this is called too often! Need to cache the result! |
f83d2997 KH |
6766 | (save-excursion |
6767 | (or cperl-use-syntax-table-text-property | |
5bd52f0e | 6768 | (error "I need to have a regexp marked!")) |
f83d2997 | 6769 | ;; Find the start |
db133cb6 RS |
6770 | (if (looking-at "\\s|") |
6771 | nil ; good already | |
5bd52f0e | 6772 | (if (looking-at "\\([smy]\\|qr\\)\\s|") |
db133cb6 RS |
6773 | (forward-char 1) |
6774 | (re-search-backward "\\s|"))) ; Assume it is scanned already. | |
f83d2997 KH |
6775 | ;;(forward-char 1) |
6776 | (let ((b (point)) (e (make-marker)) have-x delim (c (current-column)) | |
6777 | (sub-p (eq (preceding-char) ?s)) s) | |
6778 | (forward-sexp 1) | |
6779 | (set-marker e (1- (point))) | |
6780 | (setq delim (preceding-char)) | |
6781 | (if (and sub-p (eq delim (char-after (- (point) 2)))) | |
6782 | (error "Possible s/blah// - do not know how to deal with")) | |
6783 | (if sub-p (forward-sexp 1)) | |
5c8b7eaf | 6784 | (if (looking-at "\\sw*x") |
f83d2997 KH |
6785 | (setq have-x t) |
6786 | (insert "x")) | |
6787 | ;; Protect fragile " ", "#" | |
6788 | (if have-x nil | |
6789 | (goto-char (1+ b)) | |
6790 | (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too? | |
6791 | (forward-char -1) | |
6792 | (insert "\\") | |
6793 | (forward-char 1))) | |
6794 | b))) | |
6795 | ||
6c389151 | 6796 | (defun cperl-beautify-regexp (&optional deep) |
f94a632a | 6797 | "Do it. (Experimental, may change semantics, recheck the result.) |
f83d2997 | 6798 | We suppose that the regexp is scanned already." |
6c389151 SM |
6799 | (interactive "P") |
6800 | (if deep | |
6801 | (prefix-numeric-value deep) | |
6802 | (setq deep -1)) | |
6803 | (save-excursion | |
6804 | (goto-char (cperl-make-regexp-x)) | |
6805 | (let ((b (point)) (e (make-marker))) | |
6806 | (forward-sexp 1) | |
6807 | (set-marker e (1- (point))) | |
6808 | (cperl-beautify-regexp-piece b e nil deep)))) | |
f83d2997 | 6809 | |
db133cb6 RS |
6810 | (defun cperl-regext-to-level-start () |
6811 | "Goto start of an enclosing group in regexp. | |
f83d2997 KH |
6812 | We suppose that the regexp is scanned already." |
6813 | (interactive) | |
db133cb6 | 6814 | (let ((limit (cperl-make-regexp-x)) done) |
f83d2997 KH |
6815 | (while (not done) |
6816 | (or (eq (following-char) ?\() | |
db133cb6 | 6817 | (search-backward "(" (1+ limit) t) |
f83d2997 KH |
6818 | (error "Cannot find `(' which starts a group")) |
6819 | (setq done | |
6820 | (save-excursion | |
6821 | (skip-chars-backward "\\") | |
6822 | (looking-at "\\(\\\\\\\\\\)*("))) | |
db133cb6 RS |
6823 | (or done (forward-char -1))))) |
6824 | ||
6825 | (defun cperl-contract-level () | |
5bd52f0e | 6826 | "Find an enclosing group in regexp and contract it. |
db133cb6 RS |
6827 | \(Experimental, may change semantics, recheck the result.) |
6828 | We suppose that the regexp is scanned already." | |
6829 | (interactive) | |
6c389151 SM |
6830 | ;; (save-excursion ; Can't, breaks `cperl-contract-levels' |
6831 | (cperl-regext-to-level-start) | |
6832 | (let ((b (point)) (e (make-marker)) s c) | |
6833 | (forward-sexp 1) | |
6834 | (set-marker e (1- (point))) | |
6835 | (goto-char b) | |
6836 | (while (re-search-forward "\\(#\\)\\|\n" e 'to-end) | |
6837 | (cond | |
6838 | ((match-beginning 1) ; #-comment | |
6839 | (or c (setq c (current-indentation))) | |
5efe6a56 SM |
6840 | (beginning-of-line 2) ; Skip |
6841 | (setq s (point)) | |
6842 | (skip-chars-forward " \t") | |
6843 | (delete-region s (point)) | |
6844 | (indent-to-column c)) | |
6845 | (t | |
6846 | (delete-char -1) | |
6847 | (just-one-space)))))) | |
db133cb6 RS |
6848 | |
6849 | (defun cperl-contract-levels () | |
5bd52f0e | 6850 | "Find an enclosing group in regexp and contract all the kids. |
db133cb6 RS |
6851 | \(Experimental, may change semantics, recheck the result.) |
6852 | We suppose that the regexp is scanned already." | |
6853 | (interactive) | |
6c389151 SM |
6854 | (save-excursion |
6855 | (condition-case nil | |
6856 | (cperl-regext-to-level-start) | |
6857 | (error ; We are outside outermost group | |
5efe6a56 SM |
6858 | (goto-char (cperl-make-regexp-x)))) |
6859 | (let ((b (point)) (e (make-marker)) s c) | |
6860 | (forward-sexp 1) | |
6861 | (set-marker e (1- (point))) | |
6862 | (goto-char (1+ b)) | |
6863 | (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t) | |
6864 | (cond | |
6c389151 SM |
6865 | ((match-beginning 1) ; Skip |
6866 | nil) | |
6867 | (t ; Group | |
6868 | (cperl-contract-level))))))) | |
f83d2997 | 6869 | |
6c389151 | 6870 | (defun cperl-beautify-level (&optional deep) |
f83d2997 KH |
6871 | "Find an enclosing group in regexp and beautify it. |
6872 | \(Experimental, may change semantics, recheck the result.) | |
6873 | We suppose that the regexp is scanned already." | |
6c389151 SM |
6874 | (interactive "P") |
6875 | (if deep | |
6876 | (prefix-numeric-value deep) | |
6877 | (setq deep -1)) | |
6878 | (save-excursion | |
6879 | (cperl-regext-to-level-start) | |
6880 | (let ((b (point)) (e (make-marker))) | |
6881 | (forward-sexp 1) | |
6882 | (set-marker e (1- (point))) | |
6883 | (cperl-beautify-regexp-piece b e nil deep)))) | |
db133cb6 RS |
6884 | |
6885 | (defun cperl-invert-if-unless () | |
6c389151 | 6886 | "Change `if (A) {B}' into `B if A;' etc if possible." |
db133cb6 RS |
6887 | (interactive) |
6888 | (or (looking-at "\\<") | |
6889 | (forward-sexp -1)) | |
6c389151 | 6890 | (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>") |
db133cb6 RS |
6891 | (let ((pos1 (point)) |
6892 | pos2 pos3 pos4 pos5 s1 s2 state p pos45 | |
6893 | (s0 (buffer-substring (match-beginning 0) (match-end 0)))) | |
6894 | (forward-sexp 2) | |
6895 | (setq pos3 (point)) | |
6896 | (forward-sexp -1) | |
6897 | (setq pos2 (point)) | |
6898 | (if (eq (following-char) ?\( ) | |
6899 | (progn | |
6900 | (goto-char pos3) | |
6901 | (forward-sexp 1) | |
6902 | (setq pos5 (point)) | |
6903 | (forward-sexp -1) | |
6904 | (setq pos4 (point)) | |
6905 | ;; XXXX In fact may be `A if (B); {C}' ... | |
6906 | (if (and (eq (following-char) ?\{ ) | |
6907 | (progn | |
6908 | (cperl-backward-to-noncomment pos3) | |
6909 | (eq (preceding-char) ?\) ))) | |
6910 | (if (condition-case nil | |
6911 | (progn | |
6912 | (goto-char pos5) | |
6913 | (forward-sexp 1) | |
6914 | (forward-sexp -1) | |
6915 | (looking-at "\\<els\\(e\\|if\\)\\>")) | |
6916 | (error nil)) | |
6917 | (error | |
6918 | "`%s' (EXPR) {BLOCK} with `else'/`elsif'" s0) | |
6919 | (goto-char (1- pos5)) | |
6920 | (cperl-backward-to-noncomment pos4) | |
6921 | (if (eq (preceding-char) ?\;) | |
6922 | (forward-char -1)) | |
6923 | (setq pos45 (point)) | |
6924 | (goto-char pos4) | |
6925 | (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" pos45 t) | |
6926 | (setq p (match-beginning 0) | |
6927 | s1 (buffer-substring p (match-end 0)) | |
6928 | state (parse-partial-sexp pos4 p)) | |
5c8b7eaf | 6929 | (or (nth 3 state) |
db133cb6 RS |
6930 | (nth 4 state) |
6931 | (nth 5 state) | |
6932 | (error "`%s' inside `%s' BLOCK" s1 s0)) | |
6933 | (goto-char (match-end 0))) | |
6934 | ;; Finally got it | |
6935 | (goto-char (1+ pos4)) | |
6936 | (skip-chars-forward " \t\n") | |
6937 | (setq s2 (buffer-substring (point) pos45)) | |
6938 | (goto-char pos45) | |
6939 | (or (looking-at ";?[ \t\n]*}") | |
6940 | (progn | |
6941 | (skip-chars-forward "; \t\n") | |
6942 | (setq s2 (concat s2 "\n" (buffer-substring (point) (1- pos5)))))) | |
6943 | (and (equal s2 "") | |
6944 | (setq s2 "1")) | |
6945 | (goto-char (1- pos3)) | |
6946 | (cperl-backward-to-noncomment pos2) | |
6947 | (or (looking-at "[ \t\n]*)") | |
6948 | (goto-char (1- pos3))) | |
6949 | (setq p (point)) | |
6950 | (goto-char (1+ pos2)) | |
6951 | (skip-chars-forward " \t\n") | |
6952 | (setq s1 (buffer-substring (point) p)) | |
6953 | (delete-region pos4 pos5) | |
6954 | (delete-region pos2 pos3) | |
6955 | (goto-char pos1) | |
6956 | (insert s2 " ") | |
6957 | (just-one-space) | |
6958 | (forward-word 1) | |
6959 | (setq pos1 (point)) | |
6960 | (insert " " s1 ";") | |
6c389151 | 6961 | (delete-horizontal-space) |
db133cb6 RS |
6962 | (forward-char -1) |
6963 | (delete-horizontal-space) | |
6964 | (goto-char pos1) | |
6965 | (just-one-space) | |
6966 | (cperl-indent-line)) | |
6967 | (error "`%s' (EXPR) not with an {BLOCK}" s0))) | |
6968 | (error "`%s' not with an (EXPR)" s0))) | |
f94a632a | 6969 | (error "Not at `if', `unless', `while', `until', `for' or `foreach'"))) |
db133cb6 | 6970 | |
5bd52f0e | 6971 | ;;; By Anthony Foiani <afoiani@uswest.com> |
b7ec9e59 RS |
6972 | ;;; Getting help on modules in C-h f ? |
6973 | ;;; This is a modified version of `man'. | |
6974 | ;;; Need to teach it how to lookup functions | |
6975 | (defun cperl-perldoc (word) | |
6976 | "Run `perldoc' on WORD." | |
6977 | (interactive | |
6978 | (list (let* ((default-entry (cperl-word-at-point)) | |
6979 | (input (read-string | |
6980 | (format "perldoc entry%s: " | |
6981 | (if (string= default-entry "") | |
6982 | "" | |
6983 | (format " (default %s)" default-entry)))))) | |
6984 | (if (string= input "") | |
6985 | (if (string= default-entry "") | |
6986 | (error "No perldoc args given") | |
6987 | default-entry) | |
6988 | input)))) | |
a8e1e57f | 6989 | (require 'man) |
5c8b7eaf | 6990 | (let* ((is-func (and |
b7ec9e59 RS |
6991 | (string-match "^[a-z]+$" word) |
6992 | (string-match (concat "^" word "\\>") | |
6993 | (documentation-property | |
6994 | 'cperl-short-docs | |
6995 | 'variable-documentation)))) | |
6996 | (manual-program (if is-func "perldoc -f" "perldoc"))) | |
b7ec9e59 RS |
6997 | (Man-getpage-in-background word))) |
6998 | ||
6999 | (defun cperl-perldoc-at-point () | |
7000 | "Run a `perldoc' on the word around point." | |
7001 | (interactive) | |
7002 | (cperl-perldoc (cperl-word-at-point))) | |
7003 | ||
7004 | (defcustom pod2man-program "pod2man" | |
7005 | "*File name for `pod2man'." | |
7006 | :type 'file | |
7007 | :group 'cperl) | |
7008 | ||
5bd52f0e | 7009 | ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes) |
b7ec9e59 RS |
7010 | (defun cperl-pod-to-manpage () |
7011 | "Create a virtual manpage in Emacs from the Perl Online Documentation." | |
7012 | (interactive) | |
7013 | (require 'man) | |
7014 | (let* ((pod2man-args (concat buffer-file-name " | nroff -man ")) | |
7015 | (bufname (concat "Man " buffer-file-name)) | |
7016 | (buffer (generate-new-buffer bufname))) | |
7017 | (save-excursion | |
7018 | (set-buffer buffer) | |
7019 | (let ((process-environment (copy-sequence process-environment))) | |
7020 | ;; Prevent any attempt to use display terminal fanciness. | |
7021 | (setenv "TERM" "dumb") | |
7022 | (set-process-sentinel | |
7023 | (start-process pod2man-program buffer "sh" "-c" | |
7024 | (format (cperl-pod2man-build-command) pod2man-args)) | |
7025 | 'Man-bgproc-sentinel))))) | |
7026 | ||
7027 | (defun cperl-pod2man-build-command () | |
7028 | "Builds the entire background manpage and cleaning command." | |
7029 | (let ((command (concat pod2man-program " %s 2>/dev/null")) | |
7030 | (flist Man-filter-list)) | |
7031 | (while (and flist (car flist)) | |
7032 | (let ((pcom (car (car flist))) | |
7033 | (pargs (cdr (car flist)))) | |
7034 | (setq command | |
7035 | (concat command " | " pcom " " | |
7036 | (mapconcat '(lambda (phrase) | |
7037 | (if (not (stringp phrase)) | |
7038 | (error "Malformed Man-filter-list")) | |
7039 | phrase) | |
7040 | pargs " "))) | |
7041 | (setq flist (cdr flist)))) | |
7042 | command)) | |
db133cb6 RS |
7043 | |
7044 | (defun cperl-lazy-install ()) ; Avoid a warning | |
f83d2997 KH |
7045 | |
7046 | (if (fboundp 'run-with-idle-timer) | |
7047 | (progn | |
7048 | (defvar cperl-help-shown nil | |
7049 | "Non-nil means that the help was already shown now.") | |
7050 | ||
7051 | (defvar cperl-lazy-installed nil | |
7052 | "Non-nil means that the lazy-help handlers are installed now.") | |
7053 | ||
7054 | (defun cperl-lazy-install () | |
7055 | (interactive) | |
7056 | (make-variable-buffer-local 'cperl-help-shown) | |
7057 | (if (and (cperl-val 'cperl-lazy-help-time) | |
7058 | (not cperl-lazy-installed)) | |
7059 | (progn | |
7060 | (add-hook 'post-command-hook 'cperl-lazy-hook) | |
5c8b7eaf SS |
7061 | (run-with-idle-timer |
7062 | (cperl-val 'cperl-lazy-help-time 1000000 5) | |
7063 | t | |
f83d2997 KH |
7064 | 'cperl-get-help-defer) |
7065 | (setq cperl-lazy-installed t)))) | |
7066 | ||
7067 | (defun cperl-lazy-unstall () | |
7068 | (interactive) | |
7069 | (remove-hook 'post-command-hook 'cperl-lazy-hook) | |
7070 | (cancel-function-timers 'cperl-get-help-defer) | |
7071 | (setq cperl-lazy-installed nil)) | |
7072 | ||
7073 | (defun cperl-lazy-hook () | |
7074 | (setq cperl-help-shown nil)) | |
7075 | ||
7076 | (defun cperl-get-help-defer () | |
996e2616 | 7077 | (when (memq major-mode '(perl-mode cperl-mode)) |
f83d2997 KH |
7078 | (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t)) |
7079 | (cperl-get-help) | |
7080 | (setq cperl-help-shown t)))) | |
7081 | (cperl-lazy-install))) | |
7082 | ||
db133cb6 RS |
7083 | |
7084 | ;;; Plug for wrong font-lock: | |
7085 | ||
7086 | (defun cperl-font-lock-unfontify-region-function (beg end) | |
7087 | (let* ((modified (buffer-modified-p)) (buffer-undo-list t) | |
7088 | (inhibit-read-only t) (inhibit-point-motion-hooks t) | |
7089 | before-change-functions after-change-functions | |
7090 | deactivate-mark buffer-file-name buffer-file-truename) | |
7091 | (remove-text-properties beg end '(face nil)) | |
7092 | (when (and (not modified) (buffer-modified-p)) | |
7093 | (set-buffer-modified-p nil)))) | |
7094 | ||
7095 | (defvar cperl-d-l nil) | |
7096 | (defun cperl-fontify-syntaxically (end) | |
5bd52f0e | 7097 | ;; Some vars for debugging only |
6c389151 SM |
7098 | ;; (message "Syntaxifying...") |
7099 | (let (start (dbg (point)) (iend end) | |
5bd52f0e RS |
7100 | (istate (car cperl-syntax-state))) |
7101 | (and cperl-syntaxify-unwind | |
7102 | (setq end (cperl-unwind-to-safe t end))) | |
7103 | (setq start (point)) | |
db133cb6 RS |
7104 | (or cperl-syntax-done-to |
7105 | (setq cperl-syntax-done-to (point-min))) | |
7106 | (if (or (not (boundp 'font-lock-hot-pass)) | |
5bd52f0e RS |
7107 | (eval 'font-lock-hot-pass) |
7108 | t) ; Not debugged otherwise | |
db133cb6 RS |
7109 | ;; Need to forget what is after `start' |
7110 | (setq start (min cperl-syntax-done-to start)) | |
7111 | ;; Fontification without a change | |
7112 | (setq start (max cperl-syntax-done-to start))) | |
7113 | (and (> end start) | |
7114 | (setq cperl-syntax-done-to start) ; In case what follows fails | |
7115 | (cperl-find-pods-heres start end t nil t)) | |
5bd52f0e | 7116 | (if (eq cperl-syntaxify-by-font-lock 'message) |
5c8b7eaf SS |
7117 | (message "Syntaxified %s..%s from %s to %s(%s), state %s-->%s" |
7118 | dbg iend | |
7119 | start end cperl-syntax-done-to | |
7120 | istate (car cperl-syntax-state))) ; For debugging | |
db133cb6 RS |
7121 | nil)) ; Do not iterate |
7122 | ||
5bd52f0e RS |
7123 | (defun cperl-fontify-update (end) |
7124 | (let ((pos (point)) prop posend) | |
7125 | (while (< pos end) | |
7126 | (setq prop (get-text-property pos 'cperl-postpone)) | |
7127 | (setq posend (next-single-property-change pos 'cperl-postpone nil end)) | |
7128 | (and prop (put-text-property pos posend (car prop) (cdr prop))) | |
7129 | (setq pos posend))) | |
7130 | nil) ; Do not iterate | |
7131 | ||
7132 | (defun cperl-update-syntaxification (from to) | |
7133 | (if (and cperl-use-syntax-table-text-property | |
7134 | cperl-syntaxify-by-font-lock | |
7135 | (or (null cperl-syntax-done-to) | |
7136 | (< cperl-syntax-done-to to))) | |
7137 | (progn | |
7138 | (save-excursion | |
7139 | (goto-char from) | |
7140 | (cperl-fontify-syntaxically to))))) | |
7141 | ||
5c8b7eaf | 7142 | (defvar cperl-version |
6c389151 | 7143 | (let ((v "Revision: 4.32")) |
5bd52f0e RS |
7144 | (string-match ":\\s *\\([0-9.]+\\)" v) |
7145 | (substring v (match-beginning 1) (match-end 1))) | |
7146 | "Version of IZ-supported CPerl package this file is based on.") | |
7147 | ||
f83d2997 KH |
7148 | (provide 'cperl-mode) |
7149 | ||
7150 | ;;; cperl-mode.el ends here |