Commit | Line | Data |
---|---|---|
f83d2997 KH |
1 | ;;; cperl-mode.el --- Perl code editing commands for Emacs |
2 | ||
83261a2f | 3 | ;; Copyright (C) 1985, 86, 87, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2003 |
f83d2997 KH |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Author: Ilya Zakharevich and Bob Olson | |
83261a2f | 7 | ;; Maintainer: Ilya Zakharevich <cperl@ilyaz.org> |
f83d2997 KH |
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 | ||
83261a2f | 27 | ;;; Corrections made by Ilya Zakharevich cperl@ilyaz.org |
f83d2997 KH |
28 | |
29 | ;;; Commentary: | |
30 | ||
83261a2f SM |
31 | ;; You can either fine-tune the bells and whistles of this mode or |
32 | ;; bulk enable them by putting | |
f83d2997 KH |
33 | |
34 | ;; (setq cperl-hairy t) | |
35 | ||
83261a2f SM |
36 | ;; in your .emacs file. (Emacs rulers do not consider it politically |
37 | ;; correct to make whistles enabled by default.) | |
f83d2997 | 38 | |
83261a2f SM |
39 | ;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<< |
40 | ;; or as help on variables `cperl-tips', `cperl-problems', <<<<<< | |
41 | ;; `cperl-praise', `cperl-speed'. <<<<<< | |
f83d2997 | 42 | |
83261a2f SM |
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. | |
f83d2997 | 46 | |
83261a2f SM |
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. | |
f83d2997 | 52 | |
83261a2f SM |
53 | ;; This mode supports font-lock, imenu and mode-compile. In the |
54 | ;; hairy version font-lock is on, but you should activate imenu | |
55 | ;; yourself (note that mode-compile is not standard yet). Well, you | |
56 | ;; can use imenu from keyboard anyway (M-x imenu), but it is better | |
57 | ;; to bind it like that: | |
f83d2997 KH |
58 | |
59 | ;; (define-key global-map [M-S-down-mouse-3] 'imenu) | |
60 | ||
83261a2f SM |
61 | ;;; Font lock bugs as of v4.32: |
62 | ||
63 | ;; The following kinds of Perl code erroneously start strings: | |
64 | ;; \$` \$' \$" | |
65 | ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../ | |
66 | ;; likewise with m, tr, y, q, qX instead of s | |
67 | ||
f83d2997 KH |
68 | ;;; Code: |
69 | ||
5bd52f0e | 70 | ;; Some macros are needed for `defcustom' |
80585273 | 71 | (eval-when-compile |
83261a2f SM |
72 | (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version)) |
73 | (defvar cperl-can-font-lock | |
74 | (or cperl-xemacs-p | |
75 | (and (boundp 'emacs-major-version) | |
76 | (or window-system | |
77 | (> emacs-major-version 20))))) | |
78 | (if cperl-can-font-lock | |
79 | (require 'font-lock)) | |
80585273 DL |
80 | (defvar msb-menu-cond) |
81 | (defvar gud-perldb-history) | |
82 | (defvar font-lock-background-mode) ; not in Emacs | |
83 | (defvar font-lock-display-type) ; ditto | |
80585273 DL |
84 | (defmacro cperl-is-face (arg) ; Takes quoted arg |
85 | (cond ((fboundp 'find-face) | |
86 | `(find-face ,arg)) | |
87 | (;;(and (fboundp 'face-list) | |
88 | ;; (face-list)) | |
89 | (fboundp 'face-list) | |
90 | `(member ,arg (and (fboundp 'face-list) | |
91 | (face-list)))) | |
92 | (t | |
93 | `(boundp ,arg)))) | |
94 | (defmacro cperl-make-face (arg descr) ; Takes unquoted arg | |
95 | (cond ((fboundp 'make-face) | |
96 | `(make-face (quote ,arg))) | |
97 | (t | |
ce22dd53 | 98 | `(defvar ,arg (quote ,arg) ,descr)))) |
80585273 DL |
99 | (defmacro cperl-force-face (arg descr) ; Takes unquoted arg |
100 | `(progn | |
101 | (or (cperl-is-face (quote ,arg)) | |
102 | (cperl-make-face ,arg ,descr)) | |
103 | (or (boundp (quote ,arg)) ; We use unquoted variants too | |
ce22dd53 | 104 | (defvar ,arg (quote ,arg) ,descr)))) |
80585273 DL |
105 | (if cperl-xemacs-p |
106 | (defmacro cperl-etags-snarf-tag (file line) | |
b787fc05 | 107 | `(progn |
80585273 DL |
108 | (beginning-of-line 2) |
109 | (list ,file ,line))) | |
110 | (defmacro cperl-etags-snarf-tag (file line) | |
111 | `(etags-snarf-tag))) | |
112 | (if cperl-xemacs-p | |
113 | (defmacro cperl-etags-goto-tag-location (elt) | |
114 | ;;(progn | |
115 | ;; (switch-to-buffer (get-file-buffer (elt (, elt) 0))) | |
116 | ;; (set-buffer (get-file-buffer (elt (, elt) 0))) | |
117 | ;; Probably will not work due to some save-excursion??? | |
118 | ;; Or save-file-position? | |
119 | ;; (message "Did I get to line %s?" (elt (, elt) 1)) | |
120 | `(goto-line (string-to-int (elt ,elt 1)))) | |
121 | ;;) | |
122 | (defmacro cperl-etags-goto-tag-location (elt) | |
123 | `(etags-goto-tag-location ,elt))) | |
124 | (autoload 'tmm-prompt "tmm")) | |
5bd52f0e | 125 | |
83261a2f SM |
126 | (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version)) |
127 | ||
128 | (defvar cperl-can-font-lock | |
129 | (or cperl-xemacs-p | |
130 | (and (boundp 'emacs-major-version) | |
131 | (or window-system | |
132 | (> emacs-major-version 20))))) | |
133 | ||
5bd52f0e RS |
134 | (defun cperl-choose-color (&rest list) |
135 | (let (answer) | |
136 | (while list | |
137 | (or answer | |
138 | (if (or (x-color-defined-p (car list)) | |
139 | (null (cdr list))) | |
140 | (setq answer (car list)))) | |
141 | (setq list (cdr list))) | |
142 | answer)) | |
143 | ||
ccc3ce39 SE |
144 | (defgroup cperl nil |
145 | "Major mode for editing Perl code." | |
146 | :prefix "cperl-" | |
db133cb6 RS |
147 | :group 'languages |
148 | :version "20.3") | |
149 | ||
150 | (defgroup cperl-indentation-details nil | |
151 | "Indentation." | |
152 | :prefix "cperl-" | |
153 | :group 'cperl) | |
154 | ||
155 | (defgroup cperl-affected-by-hairy nil | |
156 | "Variables affected by `cperl-hairy'." | |
157 | :prefix "cperl-" | |
158 | :group 'cperl) | |
159 | ||
160 | (defgroup cperl-autoinsert-details nil | |
161 | "Auto-insert tuneup." | |
162 | :prefix "cperl-" | |
163 | :group 'cperl) | |
164 | ||
165 | (defgroup cperl-faces nil | |
166 | "Fontification colors." | |
167 | :prefix "cperl-" | |
168 | :group 'cperl) | |
169 | ||
170 | (defgroup cperl-speed nil | |
171 | "Speed vs. validity tuneup." | |
172 | :prefix "cperl-" | |
173 | :group 'cperl) | |
174 | ||
175 | (defgroup cperl-help-system nil | |
176 | "Help system tuneup." | |
177 | :prefix "cperl-" | |
178 | :group 'cperl) | |
ccc3ce39 | 179 | |
f83d2997 | 180 | \f |
ccc3ce39 | 181 | (defcustom cperl-extra-newline-before-brace nil |
f83d2997 KH |
182 | "*Non-nil means that if, elsif, while, until, else, for, foreach |
183 | and do constructs look like: | |
184 | ||
185 | if () | |
186 | { | |
187 | } | |
188 | ||
189 | instead of: | |
190 | ||
191 | if () { | |
83261a2f | 192 | }" |
ccc3ce39 | 193 | :type 'boolean |
db133cb6 RS |
194 | :group 'cperl-autoinsert-details) |
195 | ||
5c8b7eaf | 196 | (defcustom cperl-extra-newline-before-brace-multiline |
db133cb6 RS |
197 | cperl-extra-newline-before-brace |
198 | "*Non-nil means the same as `cperl-extra-newline-before-brace', but | |
199 | for constructs with multiline if/unless/while/until/for/foreach condition." | |
200 | :type 'boolean | |
201 | :group 'cperl-autoinsert-details) | |
ccc3ce39 SE |
202 | |
203 | (defcustom cperl-indent-level 2 | |
204 | "*Indentation of CPerl statements with respect to containing block." | |
205 | :type 'integer | |
db133cb6 | 206 | :group 'cperl-indentation-details) |
f83d2997 | 207 | |
ccc3ce39 | 208 | (defcustom cperl-lineup-step nil |
f83d2997 | 209 | "*`cperl-lineup' will always lineup at multiple of this number. |
029cb4d5 | 210 | If nil, the value of `cperl-indent-level' will be used." |
ccc3ce39 | 211 | :type '(choice (const nil) integer) |
db133cb6 RS |
212 | :group 'cperl-indentation-details) |
213 | ||
ccc3ce39 | 214 | (defcustom cperl-brace-imaginary-offset 0 |
f83d2997 KH |
215 | "*Imagined indentation of a Perl open brace that actually follows a statement. |
216 | An open brace following other text is treated as if it were this far | |
ccc3ce39 SE |
217 | to the right of the start of its line." |
218 | :type 'integer | |
db133cb6 | 219 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
220 | |
221 | (defcustom cperl-brace-offset 0 | |
222 | "*Extra indentation for braces, compared with other text in same context." | |
223 | :type 'integer | |
db133cb6 | 224 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
225 | (defcustom cperl-label-offset -2 |
226 | "*Offset of CPerl label lines relative to usual indentation." | |
227 | :type 'integer | |
db133cb6 | 228 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
229 | (defcustom cperl-min-label-indent 1 |
230 | "*Minimal offset of CPerl label lines." | |
231 | :type 'integer | |
db133cb6 | 232 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
233 | (defcustom cperl-continued-statement-offset 2 |
234 | "*Extra indent for lines not starting new statements." | |
235 | :type 'integer | |
db133cb6 | 236 | :group 'cperl-indentation-details) |
ccc3ce39 | 237 | (defcustom cperl-continued-brace-offset 0 |
f83d2997 | 238 | "*Extra indent for substatements that start with open-braces. |
ccc3ce39 SE |
239 | This is in addition to cperl-continued-statement-offset." |
240 | :type 'integer | |
db133cb6 | 241 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
242 | (defcustom cperl-close-paren-offset -1 |
243 | "*Extra indent for substatements that start with close-parenthesis." | |
244 | :type 'integer | |
db133cb6 | 245 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
246 | |
247 | (defcustom cperl-auto-newline nil | |
f83d2997 KH |
248 | "*Non-nil means automatically newline before and after braces, |
249 | and after colons and semicolons, inserted in CPerl code. The following | |
250 | \\[cperl-electric-backspace] will remove the inserted whitespace. | |
5c8b7eaf | 251 | Insertion after colons requires both this variable and |
ccc3ce39 SE |
252 | `cperl-auto-newline-after-colon' set." |
253 | :type 'boolean | |
db133cb6 | 254 | :group 'cperl-autoinsert-details) |
f83d2997 | 255 | |
6c389151 SM |
256 | (defcustom cperl-autoindent-on-semi nil |
257 | "*Non-nil means automatically indent after insertion of (semi)colon. | |
258 | Active if `cperl-auto-newline' is false." | |
259 | :type 'boolean | |
260 | :group 'cperl-autoinsert-details) | |
261 | ||
ccc3ce39 | 262 | (defcustom cperl-auto-newline-after-colon nil |
f83d2997 | 263 | "*Non-nil means automatically newline even after colons. |
ccc3ce39 SE |
264 | Subject to `cperl-auto-newline' setting." |
265 | :type 'boolean | |
db133cb6 | 266 | :group 'cperl-autoinsert-details) |
f83d2997 | 267 | |
ccc3ce39 | 268 | (defcustom cperl-tab-always-indent t |
f83d2997 | 269 | "*Non-nil means TAB in CPerl mode should always reindent the current line, |
ccc3ce39 SE |
270 | regardless of where in the line point is when the TAB command is used." |
271 | :type 'boolean | |
db133cb6 | 272 | :group 'cperl-indentation-details) |
f83d2997 | 273 | |
ccc3ce39 | 274 | (defcustom cperl-font-lock nil |
029cb4d5 | 275 | "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'. |
ccc3ce39 | 276 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
277 | :type '(choice (const null) boolean) |
278 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 279 | |
ccc3ce39 | 280 | (defcustom cperl-electric-lbrace-space nil |
029cb4d5 | 281 | "*Non-nil (and non-null) means { after $ should be preceded by ` '. |
ccc3ce39 | 282 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
283 | :type '(choice (const null) boolean) |
284 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 285 | |
ccc3ce39 | 286 | (defcustom cperl-electric-parens-string "({[]})<" |
f83d2997 | 287 | "*String of parentheses that should be electric in CPerl. |
ccc3ce39 SE |
288 | Closing ones are electric only if the region is highlighted." |
289 | :type 'string | |
db133cb6 | 290 | :group 'cperl-affected-by-hairy) |
f83d2997 | 291 | |
ccc3ce39 | 292 | (defcustom cperl-electric-parens nil |
f83d2997 | 293 | "*Non-nil (and non-null) means parentheses should be electric in CPerl. |
ccc3ce39 | 294 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
295 | :type '(choice (const null) boolean) |
296 | :group 'cperl-affected-by-hairy) | |
297 | ||
298 | (defvar zmacs-regions) ; Avoid warning | |
299 | ||
5c8b7eaf | 300 | (defcustom cperl-electric-parens-mark |
f83d2997 KH |
301 | (and window-system |
302 | (or (and (boundp 'transient-mark-mode) ; For Emacs | |
303 | transient-mark-mode) | |
304 | (and (boundp 'zmacs-regions) ; For XEmacs | |
305 | zmacs-regions))) | |
306 | "*Not-nil means that electric parens look for active mark. | |
ccc3ce39 SE |
307 | Default is yes if there is visual feedback on mark." |
308 | :type 'boolean | |
db133cb6 | 309 | :group 'cperl-autoinsert-details) |
f83d2997 | 310 | |
ccc3ce39 | 311 | (defcustom cperl-electric-linefeed nil |
f83d2997 KH |
312 | "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy. |
313 | In any case these two mean plain and hairy linefeeds together. | |
ccc3ce39 | 314 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
315 | :type '(choice (const null) boolean) |
316 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 317 | |
ccc3ce39 | 318 | (defcustom cperl-electric-keywords nil |
f83d2997 | 319 | "*Not-nil (and non-null) means keywords are electric in CPerl. |
ccc3ce39 | 320 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
321 | :type '(choice (const null) boolean) |
322 | :group 'cperl-affected-by-hairy) | |
ccc3ce39 SE |
323 | |
324 | (defcustom cperl-hairy nil | |
db133cb6 | 325 | "*Not-nil means most of the bells and whistles are enabled in CPerl. |
5c8b7eaf | 326 | Affects: `cperl-font-lock', `cperl-electric-lbrace-space', |
db133cb6 RS |
327 | `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords', |
328 | `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings', | |
329 | `cperl-lazy-help-time'." | |
ccc3ce39 | 330 | :type 'boolean |
db133cb6 | 331 | :group 'cperl-affected-by-hairy) |
ccc3ce39 SE |
332 | |
333 | (defcustom cperl-comment-column 32 | |
334 | "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)." | |
335 | :type 'integer | |
db133cb6 | 336 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
337 | |
338 | (defcustom cperl-vc-header-alist '((SCCS "$sccs = '%W\%' ;") | |
339 | (RCS "$rcs = ' $Id\$ ' ;")) | |
340 | "*What to use as `vc-header-alist' in CPerl." | |
341 | :type '(repeat (list symbol string)) | |
342 | :group 'cperl) | |
343 | ||
5c8b7eaf | 344 | (defcustom cperl-clobber-mode-lists |
5bd52f0e RS |
345 | (not |
346 | (and | |
347 | (boundp 'interpreter-mode-alist) | |
348 | (assoc "miniperl" interpreter-mode-alist) | |
349 | (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist))) | |
350 | "*Whether to install us into `interpreter-' and `extension' mode lists." | |
351 | :type 'boolean | |
352 | :group 'cperl) | |
353 | ||
ccc3ce39 | 354 | (defcustom cperl-info-on-command-no-prompt nil |
f83d2997 KH |
355 | "*Not-nil (and non-null) means not to prompt on C-h f. |
356 | The opposite behaviour is always available if prefixed with C-c. | |
ccc3ce39 | 357 | Can be overwritten by `cperl-hairy' if nil." |
db133cb6 RS |
358 | :type '(choice (const null) boolean) |
359 | :group 'cperl-affected-by-hairy) | |
360 | ||
361 | (defcustom cperl-clobber-lisp-bindings nil | |
362 | "*Not-nil (and non-null) means not overwrite C-h f. | |
363 | The function is available on \\[cperl-info-on-command], \\[cperl-get-help]. | |
364 | Can be overwritten by `cperl-hairy' if nil." | |
365 | :type '(choice (const null) boolean) | |
366 | :group 'cperl-affected-by-hairy) | |
f83d2997 | 367 | |
ccc3ce39 | 368 | (defcustom cperl-lazy-help-time nil |
db133cb6 RS |
369 | "*Not-nil (and non-null) means to show lazy help after given idle time. |
370 | Can be overwritten by `cperl-hairy' to be 5 sec if nil." | |
300f7bb3 | 371 | :type '(choice (const null) (const nil) integer) |
db133cb6 | 372 | :group 'cperl-affected-by-hairy) |
f83d2997 | 373 | |
ccc3ce39 | 374 | (defcustom cperl-pod-face 'font-lock-comment-face |
83261a2f | 375 | "*Face for POD highlighting." |
ccc3ce39 | 376 | :type 'face |
db133cb6 | 377 | :group 'cperl-faces) |
f83d2997 | 378 | |
ccc3ce39 | 379 | (defcustom cperl-pod-head-face 'font-lock-variable-name-face |
83261a2f | 380 | "*Face for POD highlighting. |
ccc3ce39 SE |
381 | Font for POD headers." |
382 | :type 'face | |
db133cb6 | 383 | :group 'cperl-faces) |
f83d2997 | 384 | |
ccc3ce39 | 385 | (defcustom cperl-here-face 'font-lock-string-face |
80585273 | 386 | "*Face for here-docs highlighting." |
ccc3ce39 | 387 | :type 'face |
db133cb6 | 388 | :group 'cperl-faces) |
f83d2997 | 389 | |
ac6857fb | 390 | (defcustom cperl-invalid-face 'underline |
80585273 DL |
391 | "*Face for highlighting trailing whitespace." |
392 | :type 'face | |
ac6857fb | 393 | :version "21.1" |
5bd52f0e RS |
394 | :group 'cperl-faces) |
395 | ||
ccc3ce39 | 396 | (defcustom cperl-pod-here-fontify '(featurep 'font-lock) |
83261a2f | 397 | "*Not-nil after evaluation means to highlight POD and here-docs sections." |
ccc3ce39 | 398 | :type 'boolean |
db133cb6 | 399 | :group 'cperl-faces) |
f83d2997 | 400 | |
5bd52f0e RS |
401 | (defcustom cperl-fontify-m-as-s t |
402 | "*Not-nil means highlight 1arg regular expressions operators same as 2arg." | |
403 | :type 'boolean | |
404 | :group 'cperl-faces) | |
405 | ||
6c389151 SM |
406 | (defcustom cperl-highlight-variables-indiscriminately nil |
407 | "*Non-nil means perform additional highlighting on variables. | |
408 | Currently only changes how scalar variables are highlighted. | |
409 | Note that that variable is only read at initialization time for | |
410 | the variable `cperl-font-lock-keywords-2', so changing it after you've | |
f94a632a | 411 | entered CPerl mode the first time will have no effect." |
6c389151 SM |
412 | :type 'boolean |
413 | :group 'cperl) | |
414 | ||
ccc3ce39 | 415 | (defcustom cperl-pod-here-scan t |
83261a2f | 416 | "*Not-nil means look for POD and here-docs sections during startup. |
ccc3ce39 SE |
417 | You can always make lookup from menu or using \\[cperl-find-pods-heres]." |
418 | :type 'boolean | |
db133cb6 | 419 | :group 'cperl-speed) |
f83d2997 | 420 | |
6c389151 SM |
421 | (defcustom cperl-regexp-scan t |
422 | "*Not-nil means make marking of regular expression more thorough. | |
423 | Effective only with `cperl-pod-here-scan'. Not implemented yet." | |
424 | :type 'boolean | |
425 | :group 'cperl-speed) | |
426 | ||
ccc3ce39 | 427 | (defcustom cperl-imenu-addback nil |
f83d2997 | 428 | "*Not-nil means add backreferences to generated `imenu's. |
db133cb6 | 429 | May require patched `imenu' and `imenu-go'. Obsolete." |
ccc3ce39 | 430 | :type 'boolean |
db133cb6 | 431 | :group 'cperl-help-system) |
f83d2997 | 432 | |
ccc3ce39 SE |
433 | (defcustom cperl-max-help-size 66 |
434 | "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents." | |
435 | :type '(choice integer (const nil)) | |
db133cb6 | 436 | :group 'cperl-help-system) |
f83d2997 | 437 | |
ccc3ce39 SE |
438 | (defcustom cperl-shrink-wrap-info-frame t |
439 | "*Non-nil means shrink-wrapping of info-buffer-frame allowed." | |
440 | :type 'boolean | |
db133cb6 | 441 | :group 'cperl-help-system) |
f83d2997 | 442 | |
ccc3ce39 | 443 | (defcustom cperl-info-page "perl" |
f83d2997 | 444 | "*Name of the info page containing perl docs. |
ccc3ce39 SE |
445 | Older version of this page was called `perl5', newer `perl'." |
446 | :type 'string | |
db133cb6 | 447 | :group 'cperl-help-system) |
f83d2997 | 448 | |
5c8b7eaf | 449 | (defcustom cperl-use-syntax-table-text-property |
f83d2997 | 450 | (boundp 'parse-sexp-lookup-properties) |
ccc3ce39 SE |
451 | "*Non-nil means CPerl sets up and uses `syntax-table' text property." |
452 | :type 'boolean | |
db133cb6 | 453 | :group 'cperl-speed) |
f83d2997 | 454 | |
5c8b7eaf | 455 | (defcustom cperl-use-syntax-table-text-property-for-tags |
f83d2997 | 456 | cperl-use-syntax-table-text-property |
ccc3ce39 SE |
457 | "*Non-nil means: set up and use `syntax-table' text property generating TAGS." |
458 | :type 'boolean | |
db133cb6 | 459 | :group 'cperl-speed) |
ccc3ce39 SE |
460 | |
461 | (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$" | |
462 | "*Regexp to match files to scan when generating TAGS." | |
463 | :type 'regexp | |
464 | :group 'cperl) | |
465 | ||
8937f01b RS |
466 | (defcustom cperl-noscan-files-regexp |
467 | "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$" | |
ccc3ce39 SE |
468 | "*Regexp to match files/dirs to skip when generating TAGS." |
469 | :type 'regexp | |
470 | :group 'cperl) | |
471 | ||
472 | (defcustom cperl-regexp-indent-step nil | |
473 | "*Indentation used when beautifying regexps. | |
029cb4d5 | 474 | If nil, the value of `cperl-indent-level' will be used." |
ccc3ce39 | 475 | :type '(choice integer (const nil)) |
db133cb6 | 476 | :group 'cperl-indentation-details) |
ccc3ce39 SE |
477 | |
478 | (defcustom cperl-indent-left-aligned-comments t | |
479 | "*Non-nil means that the comment starting in leftmost column should indent." | |
480 | :type 'boolean | |
db133cb6 | 481 | :group 'cperl-indentation-details) |
ccc3ce39 | 482 | |
8f222248 | 483 | (defcustom cperl-under-as-char nil |
ccc3ce39 SE |
484 | "*Non-nil means that the _ (underline) should be treated as word char." |
485 | :type 'boolean | |
486 | :group 'cperl) | |
f83d2997 | 487 | |
db133cb6 RS |
488 | (defcustom cperl-extra-perl-args "" |
489 | "*Extra arguments to use when starting Perl. | |
490 | Currently used with `cperl-check-syntax' only." | |
491 | :type 'string | |
492 | :group 'cperl) | |
493 | ||
494 | (defcustom cperl-message-electric-keyword t | |
495 | "*Non-nil means that the `cperl-electric-keyword' prints a help message." | |
496 | :type 'boolean | |
497 | :group 'cperl-help-system) | |
498 | ||
499 | (defcustom cperl-indent-region-fix-constructs 1 | |
500 | "*Amount of space to insert between `}' and `else' or `elsif' | |
501 | in `cperl-indent-region'. Set to nil to leave as is. Values other | |
502 | than 1 and nil will probably not work." | |
503 | :type '(choice (const nil) (const 1)) | |
504 | :group 'cperl-indentation-details) | |
505 | ||
506 | (defcustom cperl-break-one-line-blocks-when-indent t | |
507 | "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs | |
2022c546 | 508 | need to be reformatted into multiline ones when indenting a region." |
db133cb6 RS |
509 | :type 'boolean |
510 | :group 'cperl-indentation-details) | |
511 | ||
512 | (defcustom cperl-fix-hanging-brace-when-indent t | |
513 | "*Non-nil means that BLOCK-end `}' may be put on a separate line | |
5c8b7eaf | 514 | when indenting a region. |
db133cb6 RS |
515 | Braces followed by else/elsif/while/until are excepted." |
516 | :type 'boolean | |
517 | :group 'cperl-indentation-details) | |
518 | ||
519 | (defcustom cperl-merge-trailing-else t | |
5c8b7eaf | 520 | "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue |
db133cb6 RS |
521 | may be merged to be on the same line when indenting a region." |
522 | :type 'boolean | |
523 | :group 'cperl-indentation-details) | |
524 | ||
6c389151 SM |
525 | (defcustom cperl-indent-parens-as-block nil |
526 | "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks, | |
527 | but for trailing \",\" inside the group, which won't increase indentation. | |
528 | One should tune up `cperl-close-paren-offset' as well." | |
529 | :type 'boolean | |
530 | :group 'cperl-indentation-details) | |
531 | ||
a1506d29 | 532 | (defcustom cperl-syntaxify-by-font-lock |
83261a2f | 533 | (and cperl-can-font-lock |
5bd52f0e | 534 | (boundp 'parse-sexp-lookup-properties)) |
6c389151 | 535 | "*Non-nil means that CPerl uses `font-lock's routines for syntaxification." |
5bd52f0e RS |
536 | :type '(choice (const message) boolean) |
537 | :group 'cperl-speed) | |
538 | ||
539 | (defcustom cperl-syntaxify-unwind | |
540 | t | |
f94a632a | 541 | "*Non-nil means that CPerl unwinds to a start of a long construction |
5bd52f0e | 542 | when syntaxifying a chunk of buffer." |
db133cb6 RS |
543 | :type 'boolean |
544 | :group 'cperl-speed) | |
545 | ||
5bd52f0e RS |
546 | (defcustom cperl-ps-print-face-properties |
547 | '((font-lock-keyword-face nil nil bold shadow) | |
548 | (font-lock-variable-name-face nil nil bold) | |
549 | (font-lock-function-name-face nil nil bold italic box) | |
550 | (font-lock-constant-face nil "LightGray" bold) | |
551 | (cperl-array-face nil "LightGray" bold underline) | |
552 | (cperl-hash-face nil "LightGray" bold italic underline) | |
553 | (font-lock-comment-face nil "LightGray" italic) | |
554 | (font-lock-string-face nil nil italic underline) | |
555 | (cperl-nonoverridable-face nil nil italic underline) | |
556 | (font-lock-type-face nil nil underline) | |
557 | (underline nil "LightGray" strikeout)) | |
558 | "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'." | |
5c8b7eaf | 559 | :type '(repeat (cons symbol |
5bd52f0e RS |
560 | (cons (choice (const nil) string) |
561 | (cons (choice (const nil) string) | |
562 | (repeat symbol))))) | |
563 | :group 'cperl-faces) | |
564 | ||
5cc679ab JB |
565 | (defvar cperl-dark-background |
566 | (cperl-choose-color "navy" "os2blue" "darkgreen")) | |
567 | (defvar cperl-dark-foreground | |
568 | (cperl-choose-color "orchid1" "orange")) | |
569 | ||
570 | (defface cperl-nonoverridable-face | |
571 | `((((class grayscale) (background light)) | |
572 | (:background "Gray90" :slant italic :underline t)) | |
573 | (((class grayscale) (background dark)) | |
574 | (:foreground "Gray80" :slant italic :underline t :weight bold)) | |
575 | (((class color) (background light)) | |
576 | (:foreground "chartreuse3")) | |
577 | (((class color) (background dark)) | |
578 | (:foreground ,cperl-dark-foreground)) | |
579 | (t (:weight bold :underline t))) | |
580 | "Font Lock mode face used to highlight array names." | |
581 | :group 'cperl-faces) | |
582 | ||
583 | (defface cperl-array-face | |
584 | `((((class grayscale) (background light)) | |
585 | (:background "Gray90" :weight bold)) | |
586 | (((class grayscale) (background dark)) | |
587 | (:foreground "Gray80" :weight bold)) | |
588 | (((class color) (background light)) | |
589 | (:foreground "Blue" :background "lightyellow2" :weight bold)) | |
590 | (((class color) (background dark)) | |
591 | (:foreground "yellow" :background ,cperl-dark-background :weight bold)) | |
592 | (t (:weight bold))) | |
593 | "Font Lock mode face used to highlight array names." | |
594 | :group 'cperl-faces) | |
595 | ||
596 | (defface cperl-hash-face | |
597 | `((((class grayscale) (background light)) | |
598 | (:background "Gray90" :weight bold :slant italic)) | |
599 | (((class grayscale) (background dark)) | |
600 | (:foreground "Gray80" :weight bold :slant italic)) | |
601 | (((class color) (background light)) | |
602 | (:foreground "Red" :background "lightyellow2" :weight bold :slant italic)) | |
603 | (((class color) (background dark)) | |
604 | (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic)) | |
605 | (t (:weight bold :slant italic))) | |
606 | "Font Lock mode face used to highlight hash names." | |
607 | :group 'cperl-faces) | |
5bd52f0e | 608 | |
f83d2997 KH |
609 | \f |
610 | ||
611 | ;;; Short extra-docs. | |
612 | ||
613 | (defvar cperl-tips 'please-ignore-this-line | |
83261a2f | 614 | "Get maybe newer version of this package from |
f83d2997 KH |
615 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs |
616 | and/or | |
617 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl | |
db133cb6 RS |
618 | Subdirectory `cperl-mode' may contain yet newer development releases and/or |
619 | patches to related files. | |
f83d2997 | 620 | |
5bd52f0e RS |
621 | For best results apply to an older Emacs the patches from |
622 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches | |
83261a2f | 623 | \(this upgrades syntax-parsing abilities of Emacsen v19.34 and |
8e3acc66 | 624 | v20.2 up to the level of Emacs v20.3 - a must for a good Perl |
83261a2f | 625 | mode.) As of beginning of 2003, XEmacs may provide a similar ability. |
5bd52f0e | 626 | |
f83d2997 KH |
627 | Get support packages choose-color.el (or font-lock-extra.el before |
628 | 19.30), imenu-go.el from the same place. \(Look for other files there | |
629 | too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and | |
5c8b7eaf | 630 | later you should use choose-color.el *instead* of font-lock-extra.el |
f83d2997 KH |
631 | \(and you will not get smart highlighting in C :-(). |
632 | ||
633 | Note that to enable Compile choices in the menu you need to install | |
634 | mode-compile.el. | |
635 | ||
5efe6a56 SM |
636 | If your Emacs does not default to `cperl-mode' on Perl files, and you |
637 | want it to: put the following into your .emacs file: | |
638 | ||
639 | (defalias 'perl-mode 'cperl-mode) | |
640 | ||
a1506d29 | 641 | Get perl5-info from |
f83d2997 KH |
642 | $CPAN/doc/manual/info/perl-info.tar.gz |
643 | older version was on | |
644 | http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz | |
645 | ||
646 | If you use imenu-go, run imenu on perl5-info buffer (you can do it | |
5bd52f0e RS |
647 | from Perl menu). If many files are related, generate TAGS files from |
648 | Tools/Tags submenu in Perl menu. | |
f83d2997 KH |
649 | |
650 | If some class structure is too complicated, use Tools/Hierarchy-view | |
029cb4d5 | 651 | from Perl menu, or hierarchic view of imenu. The second one uses the |
f83d2997 | 652 | current buffer only, the first one requires generation of TAGS from |
5bd52f0e RS |
653 | Perl/Tools/Tags menu beforehand. |
654 | ||
655 | Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing. | |
656 | ||
657 | Switch auto-help on/off with Perl/Tools/Auto-help. | |
658 | ||
659 | Though with contemporary Emaxen CPerl mode should maintain the correct | |
660 | parsing of Perl even when editing, sometimes it may be lost. Fix this by | |
661 | ||
029cb4d5 | 662 | \\[normal-mode] |
f83d2997 | 663 | |
5bd52f0e | 664 | In cases of more severe confusion sometimes it is helpful to do |
f83d2997 | 665 | |
029cb4d5 SM |
666 | \\[load-library] cperl-mode RET |
667 | \\[normal-mode] | |
f83d2997 | 668 | |
5bd52f0e RS |
669 | Before reporting (non-)problems look in the problem section of online |
670 | micro-docs on what I know about CPerl problems.") | |
f83d2997 KH |
671 | |
672 | (defvar cperl-problems 'please-ignore-this-line | |
f94a632a RS |
673 | "Description of problems in CPerl mode. |
674 | Some faces will not be shown on some versions of Emacs unless you | |
bab27c0c RS |
675 | install choose-color.el, available from |
676 | ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs/ | |
677 | ||
6c389151 SM |
678 | `fill-paragraph' on a comment may leave the point behind the |
679 | paragraph. Parsing of lines with several <<EOF is not implemented | |
680 | yet. | |
681 | ||
8e3acc66 | 682 | Emacs had a _very_ restricted syntax parsing engine until version |
5bd52f0e | 683 | 20.1. Most problems below are corrected starting from this version of |
8e3acc66 | 684 | Emacs, and all of them should be fixed in version 20.3. (Or apply |
83261a2f SM |
685 | patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in |
686 | this respect (until 2003). | |
5bd52f0e | 687 | |
6c389151 SM |
688 | Note that even with newer Emacsen in some very rare cases the details |
689 | of interaction of `font-lock' and syntaxification may be not cleaned | |
690 | up yet. You may get slightly different colors basing on the order of | |
691 | fontification and syntaxification. Say, the initial faces is correct, | |
692 | but editing the buffer breaks this. | |
f83d2997 | 693 | |
db133cb6 RS |
694 | Even with older Emacsen CPerl mode tries to corrects some Emacs |
695 | misunderstandings, however, for efficiency reasons the degree of | |
696 | correction is different for different operations. The partially | |
697 | corrected problems are: POD sections, here-documents, regexps. The | |
698 | operations are: highlighting, indentation, electric keywords, electric | |
699 | braces. | |
f83d2997 KH |
700 | |
701 | This may be confusing, since the regexp s#//#/#\; may be highlighted | |
702 | as a comment, but it will be recognized as a regexp by the indentation | |
83261a2f | 703 | code. Or the opposite case, when a POD section is highlighted, but |
f83d2997 KH |
704 | may break the indentation of the following code (though indentation |
705 | should work if the balance of delimiters is not broken by POD). | |
706 | ||
707 | The main trick (to make $ a \"backslash\") makes constructions like | |
708 | ${aaa} look like unbalanced braces. The only trick I can think of is | |
5c8b7eaf | 709 | to insert it as $ {aaa} (legal in perl5, not in perl4). |
f83d2997 KH |
710 | |
711 | Similar problems arise in regexps, when /(\\s|$)/ should be rewritten | |
db133cb6 RS |
712 | as /($|\\s)/. Note that such a transposition is not always possible. |
713 | ||
5bd52f0e | 714 | The solution is to upgrade your Emacs or patch an older one. Note |
8e3acc66 | 715 | that Emacs 20.2 has some bugs related to `syntax-table' text |
5bd52f0e RS |
716 | properties. Patches are available on the main CPerl download site, |
717 | and on CPAN. | |
db133cb6 RS |
718 | |
719 | If these bugs cannot be fixed on your machine (say, you have an inferior | |
720 | environment and cannot recompile), you may still disable all the fancy stuff | |
83261a2f | 721 | via `cperl-use-syntax-table-text-property'.") |
f83d2997 | 722 | |
f83d2997 | 723 | (defvar cperl-praise 'please-ignore-this-line |
8e3acc66 | 724 | "Advantages of CPerl mode. |
f83d2997 KH |
725 | |
726 | 0) It uses the newest `syntax-table' property ;-); | |
727 | ||
728 | 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl | |
5c8b7eaf | 729 | mode - but the latter number may have improved too in last years) even |
5bd52f0e RS |
730 | with old Emaxen which do not support `syntax-table' property. |
731 | ||
732 | When using `syntax-table' property for syntax assist hints, it should | |
733 | handle 99.995% of lines correct - or somesuch. It automatically | |
734 | updates syntax assist hints when you edit your script. | |
f83d2997 | 735 | |
bab27c0c | 736 | 2) It is generally believed to be \"the most user-friendly Emacs |
f83d2997 KH |
737 | package\" whatever it may mean (I doubt that the people who say similar |
738 | things tried _all_ the rest of Emacs ;-), but this was not a lonely | |
739 | voice); | |
740 | ||
741 | 3) Everything is customizable, one-by-one or in a big sweep; | |
742 | ||
743 | 4) It has many easily-accessable \"tools\": | |
744 | a) Can run program, check syntax, start debugger; | |
745 | b) Can lineup vertically \"middles\" of rows, like `=' in | |
746 | a = b; | |
747 | cc = d; | |
748 | c) Can insert spaces where this impoves readability (in one | |
749 | interactive sweep over the buffer); | |
750 | d) Has support for imenu, including: | |
751 | 1) Separate unordered list of \"interesting places\"; | |
752 | 2) Separate TOC of POD sections; | |
753 | 3) Separate list of packages; | |
754 | 4) Hierarchical view of methods in (sub)packages; | |
755 | 5) and functions (by the full name - with package); | |
756 | e) Has an interface to INFO docs for Perl; The interface is | |
757 | very flexible, including shrink-wrapping of | |
758 | documentation buffer/frame; | |
759 | f) Has a builtin list of one-line explanations for perl constructs. | |
760 | g) Can show these explanations if you stay long enough at the | |
761 | corresponding place (or on demand); | |
762 | h) Has an enhanced fontification (using 3 or 4 additional faces | |
763 | comparing to font-lock - basically, different | |
764 | namespaces in Perl have different colors); | |
765 | i) Can construct TAGS basing on its knowledge of Perl syntax, | |
766 | the standard menu has 6 different way to generate | |
db133cb6 | 767 | TAGS (if \"by directory\", .xs files - with C-language |
f83d2997 KH |
768 | bindings - are included in the scan); |
769 | j) Can build a hierarchical view of classes (via imenu) basing | |
770 | on generated TAGS file; | |
771 | k) Has electric parentheses, electric newlines, uses Abbrev | |
772 | for electric logical constructs | |
773 | while () {} | |
774 | with different styles of expansion (context sensitive | |
775 | to be not so bothering). Electric parentheses behave | |
776 | \"as they should\" in a presence of a visible region. | |
777 | l) Changes msb.el \"on the fly\" to insert a group \"Perl files\"; | |
db133cb6 RS |
778 | m) Can convert from |
779 | if (A) { B } | |
780 | to | |
781 | B if A; | |
f83d2997 | 782 | |
5bd52f0e | 783 | n) Highlights (by user-choice) either 3-delimiters constructs |
6c389151 SM |
784 | (such as tr/a/b/), or regular expressions and `y/tr'; |
785 | o) Highlights trailing whitespace; | |
786 | p) Is able to manipulate Perl Regular Expressions to ease | |
787 | conversion to a more readable form. | |
5bd52f0e | 788 | |
f83d2997 KH |
789 | 5) The indentation engine was very smart, but most of tricks may be |
790 | not needed anymore with the support for `syntax-table' property. Has | |
791 | progress indicator for indentation (with `imenu' loaded). | |
792 | ||
5c8b7eaf | 793 | 6) Indent-region improves inline-comments as well; also corrects |
db133cb6 | 794 | whitespace *inside* the conditional/loop constructs. |
f83d2997 KH |
795 | |
796 | 7) Fill-paragraph correctly handles multi-line comments; | |
db133cb6 RS |
797 | |
798 | 8) Can switch to different indentation styles by one command, and restore | |
799 | the settings present before the switch. | |
800 | ||
5c8b7eaf | 801 | 9) When doing indentation of control constructs, may correct |
db133cb6 | 802 | line-breaks/spacing between elements of the construct. |
029cb4d5 SM |
803 | |
804 | 10) Uses a linear-time algorith for indentation of regions (on Emaxen with | |
83261a2f | 805 | capable syntax engines).") |
db133cb6 RS |
806 | |
807 | (defvar cperl-speed 'please-ignore-this-line | |
808 | "This is an incomplete compendium of what is available in other parts | |
809 | of CPerl documentation. (Please inform me if I skept anything.) | |
810 | ||
811 | There is a perception that CPerl is slower than alternatives. This part | |
812 | of documentation is designed to overcome this misconception. | |
813 | ||
814 | *By default* CPerl tries to enable the most comfortable settings. | |
815 | From most points of view, correctly working package is infinitely more | |
816 | comfortable than a non-correctly working one, thus by default CPerl | |
817 | prefers correctness over speed. Below is the guide how to change | |
818 | settings if your preferences are different. | |
819 | ||
820 | A) Speed of loading the file. When loading file, CPerl may perform a | |
821 | scan which indicates places which cannot be parsed by primitive Emacs | |
822 | syntax-parsing routines, and marks them up so that either | |
823 | ||
824 | A1) CPerl may work around these deficiencies (for big chunks, mostly | |
825 | PODs and HERE-documents), or | |
826 | A2) On capable Emaxen CPerl will use improved syntax-handlings | |
827 | which reads mark-up hints directly. | |
828 | ||
829 | The scan in case A2 is much more comprehensive, thus may be slower. | |
830 | ||
831 | User can disable syntax-engine-helping scan of A2 by setting | |
832 | `cperl-use-syntax-table-text-property' | |
833 | variable to nil (if it is set to t). | |
834 | ||
835 | One can disable the scan altogether (both A1 and A2) by setting | |
836 | `cperl-pod-here-scan' | |
837 | to nil. | |
838 | ||
5c8b7eaf | 839 | B) Speed of editing operations. |
db133cb6 RS |
840 | |
841 | One can add a (minor) speedup to editing operations by setting | |
842 | `cperl-use-syntax-table-text-property' | |
843 | variable to nil (if it is set to t). This will disable | |
844 | syntax-engine-helping scan, thus will make many more Perl | |
845 | constructs be wrongly recognized by CPerl, thus may lead to | |
846 | wrongly matched parentheses, wrong indentation, etc. | |
5bd52f0e RS |
847 | |
848 | One can unset `cperl-syntaxify-unwind'. This might speed up editing | |
83261a2f | 849 | of, say, long POD sections.") |
f83d2997 | 850 | |
5bd52f0e RS |
851 | (defvar cperl-tips-faces 'please-ignore-this-line |
852 | "CPerl mode uses following faces for highlighting: | |
853 | ||
8661c643 DL |
854 | `cperl-array-face' Array names |
855 | `cperl-hash-face' Hash names | |
856 | `font-lock-comment-face' Comments, PODs and whatever is considered | |
5bd52f0e | 857 | syntaxically to be not code |
8661c643 | 858 | `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of |
5bd52f0e | 859 | 2-arg operators s/y/tr/ or of RExen, |
a1506d29 | 860 | `font-lock-function-name-face' Special-cased m// and s//foo/, _ as |
5bd52f0e RS |
861 | a target of a file tests, file tests, |
862 | subroutine names at the moment of definition | |
863 | (except those conflicting with Perl operators), | |
864 | package names (when recognized), format names | |
8661c643 DL |
865 | `font-lock-keyword-face' Control flow switch constructs, declarators |
866 | `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen | |
867 | `font-lock-string-face' Strings, qw() constructs, RExen, POD sections, | |
5bd52f0e RS |
868 | literal parts and the terminator of formats |
869 | and whatever is syntaxically considered | |
870 | as string literals | |
8661c643 DL |
871 | `font-lock-type-face' Overridable keywords |
872 | `font-lock-variable-name-face' Variable declarations, indirect array and | |
5bd52f0e | 873 | hash names, POD headers/item names |
8661c643 | 874 | `cperl-invalid-face' Trailing whitespace |
5bd52f0e RS |
875 | |
876 | Note that in several situations the highlighting tries to inform about | |
877 | possible confusion, such as different colors for function names in | |
878 | declarations depending on what they (do not) override, or special cases | |
879 | m// and s/// which do not do what one would expect them to do. | |
880 | ||
5c8b7eaf | 881 | Help with best setup of these faces for printout requested (for each of |
5bd52f0e RS |
882 | the faces: please specify bold, italic, underline, shadow and box.) |
883 | ||
884 | \(Not finished.)") | |
885 | ||
f83d2997 KH |
886 | \f |
887 | ||
888 | ;;; Portability stuff: | |
889 | ||
890 | (defmacro cperl-define-key (emacs-key definition &optional xemacs-key) | |
b787fc05 GM |
891 | `(define-key cperl-mode-map |
892 | ,(if xemacs-key | |
893 | `(if cperl-xemacs-p ,xemacs-key ,emacs-key) | |
894 | emacs-key) | |
895 | ,definition)) | |
f83d2997 KH |
896 | |
897 | (defvar cperl-del-back-ch | |
898 | (car (append (where-is-internal 'delete-backward-char) | |
899 | (where-is-internal 'backward-delete-char-untabify))) | |
029cb4d5 | 900 | "Character generated by key bound to `delete-backward-char'.") |
f83d2997 | 901 | |
5c8b7eaf | 902 | (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1) |
f83d2997 KH |
903 | (setq cperl-del-back-ch (aref cperl-del-back-ch 0))) |
904 | ||
db133cb6 | 905 | (defun cperl-mark-active () (mark)) ; Avoid undefined warning |
f83d2997 KH |
906 | (if cperl-xemacs-p |
907 | (progn | |
908 | ;; "Active regions" are on: use region only if active | |
909 | ;; "Active regions" are off: use region unconditionally | |
910 | (defun cperl-use-region-p () | |
db133cb6 | 911 | (if zmacs-regions (mark) t))) |
f83d2997 KH |
912 | (defun cperl-use-region-p () |
913 | (if transient-mark-mode mark-active t)) | |
914 | (defun cperl-mark-active () mark-active)) | |
915 | ||
916 | (defsubst cperl-enable-font-lock () | |
83261a2f | 917 | cperl-can-font-lock) |
f83d2997 | 918 | |
83261a2f | 919 | (defun cperl-putback-char (c) ; Emacs 19 |
db133cb6 RS |
920 | (set 'unread-command-events (list c))) ; Avoid undefined warning |
921 | ||
f83d2997 KH |
922 | (if (boundp 'unread-command-events) |
923 | (if cperl-xemacs-p | |
924 | (defun cperl-putback-char (c) ; XEmacs >= 19.12 | |
db133cb6 | 925 | (setq unread-command-events (list (eval '(character-to-event c)))))) |
f83d2997 | 926 | (defun cperl-putback-char (c) ; XEmacs <= 19.11 |
db133cb6 | 927 | (set 'unread-command-event (eval '(character-to-event c))))) ; Avoid warnings |
f83d2997 KH |
928 | |
929 | (or (fboundp 'uncomment-region) | |
930 | (defun uncomment-region (beg end) | |
931 | (interactive "r") | |
932 | (comment-region beg end -1))) | |
933 | ||
934 | (defvar cperl-do-not-fontify | |
935 | (if (string< emacs-version "19.30") | |
936 | 'fontified | |
937 | 'lazy-lock) | |
938 | "Text property which inhibits refontification.") | |
939 | ||
5bd52f0e RS |
940 | (defsubst cperl-put-do-not-fontify (from to &optional post) |
941 | ;; If POST, do not do it with postponed fontification | |
942 | (if (and post cperl-syntaxify-by-font-lock) | |
943 | nil | |
f83d2997 | 944 | (put-text-property (max (point-min) (1- from)) |
5bd52f0e | 945 | to cperl-do-not-fontify t))) |
f83d2997 | 946 | |
ccc3ce39 | 947 | (defcustom cperl-mode-hook nil |
f94a632a | 948 | "Hook run by CPerl mode." |
ccc3ce39 SE |
949 | :type 'hook |
950 | :group 'cperl) | |
f83d2997 | 951 | |
db133cb6 RS |
952 | (defvar cperl-syntax-state nil) |
953 | (defvar cperl-syntax-done-to nil) | |
5bd52f0e | 954 | (defvar cperl-emacs-can-parse (> (length (save-excursion |
ce22dd53 | 955 | (parse-partial-sexp (point) (point)))) 9)) |
db133cb6 RS |
956 | \f |
957 | ;; Make customization possible "in reverse" | |
958 | (defsubst cperl-val (symbol &optional default hairy) | |
959 | (cond | |
960 | ((eq (symbol-value symbol) 'null) default) | |
961 | (cperl-hairy (or hairy t)) | |
962 | (t (symbol-value symbol)))) | |
f83d2997 KH |
963 | \f |
964 | ;;; Probably it is too late to set these guys already, but it can help later: | |
965 | ||
5bd52f0e | 966 | ;;;(and cperl-clobber-mode-lists |
f83d2997 KH |
967 | ;;;(setq auto-mode-alist |
968 | ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist )) | |
969 | ;;;(and (boundp 'interpreter-mode-alist) | |
970 | ;;; (setq interpreter-mode-alist (append interpreter-mode-alist | |
5bd52f0e | 971 | ;;; '(("miniperl" . perl-mode)))))) |
80585273 | 972 | (eval-when-compile |
83261a2f SM |
973 | (mapcar (lambda (p) |
974 | (condition-case nil | |
975 | (require p) | |
976 | (error nil))) | |
977 | '(imenu easymenu etags timer man info)) | |
80585273 DL |
978 | (if (fboundp 'ps-extend-face-list) |
979 | (defmacro cperl-ps-extend-face-list (arg) | |
980 | `(ps-extend-face-list ,arg)) | |
981 | (defmacro cperl-ps-extend-face-list (arg) | |
e8af40ee | 982 | `(error "This version of Emacs has no `ps-extend-face-list'"))) |
80585273 DL |
983 | ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs, |
984 | ;; macros instead of defsubsts don't work on Emacs, so we do the | |
985 | ;; expansion manually. Any other suggestions? | |
986 | (require 'cl)) | |
f83d2997 KH |
987 | |
988 | (defvar cperl-mode-abbrev-table nil | |
f94a632a | 989 | "Abbrev table in use in CPerl mode buffers.") |
f83d2997 KH |
990 | |
991 | (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-"))) | |
992 | ||
993 | (defvar cperl-mode-map () "Keymap used in CPerl mode.") | |
994 | ||
995 | (if cperl-mode-map nil | |
996 | (setq cperl-mode-map (make-sparse-keymap)) | |
997 | (cperl-define-key "{" 'cperl-electric-lbrace) | |
998 | (cperl-define-key "[" 'cperl-electric-paren) | |
999 | (cperl-define-key "(" 'cperl-electric-paren) | |
1000 | (cperl-define-key "<" 'cperl-electric-paren) | |
1001 | (cperl-define-key "}" 'cperl-electric-brace) | |
1002 | (cperl-define-key "]" 'cperl-electric-rparen) | |
1003 | (cperl-define-key ")" 'cperl-electric-rparen) | |
1004 | (cperl-define-key ";" 'cperl-electric-semi) | |
1005 | (cperl-define-key ":" 'cperl-electric-terminator) | |
1006 | (cperl-define-key "\C-j" 'newline-and-indent) | |
1007 | (cperl-define-key "\C-c\C-j" 'cperl-linefeed) | |
db133cb6 | 1008 | (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless) |
f83d2997 KH |
1009 | (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline) |
1010 | (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev) | |
db133cb6 RS |
1011 | (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix) |
1012 | (cperl-define-key "\C-c\C-f" 'auto-fill-mode) | |
f83d2997 | 1013 | (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric) |
db133cb6 | 1014 | (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp) |
f83d2997 KH |
1015 | (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound |
1016 | (cperl-define-key [?\C-\M-\|] 'cperl-lineup | |
1017 | [(control meta |)]) | |
1018 | ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph) | |
1019 | ;;(cperl-define-key "\e;" 'cperl-indent-for-comment) | |
1020 | (cperl-define-key "\177" 'cperl-electric-backspace) | |
1021 | (cperl-define-key "\t" 'cperl-indent-command) | |
1022 | ;; don't clobber the backspace binding: | |
db133cb6 RS |
1023 | (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command |
1024 | [(control c) (control h) F]) | |
db133cb6 RS |
1025 | (if (cperl-val 'cperl-clobber-lisp-bindings) |
1026 | (progn | |
1027 | (cperl-define-key "\C-hf" | |
1028 | ;;(concat (char-to-string help-char) "f") ; does not work | |
1029 | 'cperl-info-on-command | |
1030 | [(control h) f]) | |
1031 | (cperl-define-key "\C-hv" | |
1032 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1033 | 'cperl-get-help | |
5bd52f0e RS |
1034 | [(control h) v]) |
1035 | (cperl-define-key "\C-c\C-hf" | |
1036 | ;;(concat (char-to-string help-char) "f") ; does not work | |
1037 | (key-binding "\C-hf") | |
1038 | [(control c) (control h) f]) | |
1039 | (cperl-define-key "\C-c\C-hv" | |
1040 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1041 | (key-binding "\C-hv") | |
1042 | [(control c) (control h) v])) | |
1043 | (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command | |
1044 | [(control c) (control h) f]) | |
1045 | (cperl-define-key "\C-c\C-hv" | |
1046 | ;;(concat (char-to-string help-char) "v") ; does not work | |
1047 | 'cperl-get-help | |
1048 | [(control c) (control h) v])) | |
5c8b7eaf | 1049 | (if (and cperl-xemacs-p |
f83d2997 KH |
1050 | (<= emacs-minor-version 11) (<= emacs-major-version 19)) |
1051 | (progn | |
1052 | ;; substitute-key-definition is usefulness-deenhanced... | |
1053 | (cperl-define-key "\M-q" 'cperl-fill-paragraph) | |
1054 | (cperl-define-key "\e;" 'cperl-indent-for-comment) | |
1055 | (cperl-define-key "\e\C-\\" 'cperl-indent-region)) | |
1056 | (substitute-key-definition | |
1057 | 'indent-sexp 'cperl-indent-exp | |
1058 | cperl-mode-map global-map) | |
1059 | (substitute-key-definition | |
1060 | 'fill-paragraph 'cperl-fill-paragraph | |
1061 | cperl-mode-map global-map) | |
1062 | (substitute-key-definition | |
1063 | 'indent-region 'cperl-indent-region | |
1064 | cperl-mode-map global-map) | |
1065 | (substitute-key-definition | |
1066 | 'indent-for-comment 'cperl-indent-for-comment | |
1067 | cperl-mode-map global-map))) | |
1068 | ||
1069 | (defvar cperl-menu) | |
db133cb6 RS |
1070 | (defvar cperl-lazy-installed) |
1071 | (defvar cperl-old-style nil) | |
f83d2997 KH |
1072 | (condition-case nil |
1073 | (progn | |
1074 | (require 'easymenu) | |
83261a2f SM |
1075 | (easy-menu-define |
1076 | cperl-menu cperl-mode-map "Menu for CPerl mode" | |
1077 | '("Perl" | |
1078 | ["Beginning of function" beginning-of-defun t] | |
1079 | ["End of function" end-of-defun t] | |
1080 | ["Mark function" mark-defun t] | |
1081 | ["Indent expression" cperl-indent-exp t] | |
1082 | ["Fill paragraph/comment" cperl-fill-paragraph t] | |
1083 | "----" | |
1084 | ["Line up a construction" cperl-lineup (cperl-use-region-p)] | |
1085 | ["Invert if/unless/while etc" cperl-invert-if-unless t] | |
1086 | ("Regexp" | |
1087 | ["Beautify" cperl-beautify-regexp | |
1088 | cperl-use-syntax-table-text-property] | |
1089 | ["Beautify one level deep" (cperl-beautify-regexp 1) | |
1090 | cperl-use-syntax-table-text-property] | |
1091 | ["Beautify a group" cperl-beautify-level | |
1092 | cperl-use-syntax-table-text-property] | |
1093 | ["Beautify a group one level deep" (cperl-beautify-level 1) | |
1094 | cperl-use-syntax-table-text-property] | |
1095 | ["Contract a group" cperl-contract-level | |
1096 | cperl-use-syntax-table-text-property] | |
1097 | ["Contract groups" cperl-contract-levels | |
1098 | cperl-use-syntax-table-text-property]) | |
1099 | ["Refresh \"hard\" constructions" cperl-find-pods-heres t] | |
1100 | "----" | |
1101 | ["Indent region" cperl-indent-region (cperl-use-region-p)] | |
1102 | ["Comment region" cperl-comment-region (cperl-use-region-p)] | |
1103 | ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)] | |
1104 | "----" | |
1105 | ["Run" mode-compile (fboundp 'mode-compile)] | |
1106 | ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill) | |
1107 | (get-buffer "*compilation*"))] | |
1108 | ["Next error" next-error (get-buffer "*compilation*")] | |
1109 | ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)] | |
1110 | "----" | |
1111 | ["Debugger" cperl-db t] | |
1112 | "----" | |
1113 | ("Tools" | |
1114 | ["Imenu" imenu (fboundp 'imenu)] | |
1115 | ["Insert spaces if needed" cperl-find-bad-style t] | |
1116 | ["Class Hierarchy from TAGS" cperl-tags-hier-init t] | |
1117 | ;;["Update classes" (cperl-tags-hier-init t) tags-table-list] | |
1118 | ["CPerl pretty print (exprmntl)" cperl-ps-print | |
1119 | (fboundp 'ps-extend-face-list)] | |
1120 | ["Imenu on info" cperl-imenu-on-info (featurep 'imenu)] | |
1121 | ("Tags" | |
f83d2997 KH |
1122 | ;;; ["Create tags for current file" cperl-etags t] |
1123 | ;;; ["Add tags for current file" (cperl-etags t) t] | |
1124 | ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t] | |
1125 | ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t] | |
5c8b7eaf | 1126 | ;;; ["Create tags for Perl files in (sub)directories" |
f83d2997 KH |
1127 | ;;; (cperl-etags nil 'recursive) t] |
1128 | ;;; ["Add tags for Perl files in (sub)directories" | |
5c8b7eaf | 1129 | ;;; (cperl-etags t 'recursive) t]) |
f83d2997 | 1130 | ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer) |
83261a2f SM |
1131 | ["Create tags for current file" (cperl-write-tags nil t) t] |
1132 | ["Add tags for current file" (cperl-write-tags) t] | |
1133 | ["Create tags for Perl files in directory" | |
1134 | (cperl-write-tags nil t nil t) t] | |
1135 | ["Add tags for Perl files in directory" | |
1136 | (cperl-write-tags nil nil nil t) t] | |
1137 | ["Create tags for Perl files in (sub)directories" | |
1138 | (cperl-write-tags nil t t t) t] | |
1139 | ["Add tags for Perl files in (sub)directories" | |
1140 | (cperl-write-tags nil nil t t) t])) | |
1141 | ("Perl docs" | |
1142 | ["Define word at point" imenu-go-find-at-position | |
1143 | (fboundp 'imenu-go-find-at-position)] | |
1144 | ["Help on function" cperl-info-on-command t] | |
1145 | ["Help on function at point" cperl-info-on-current-command t] | |
1146 | ["Help on symbol at point" cperl-get-help t] | |
1147 | ["Perldoc" cperl-perldoc t] | |
1148 | ["Perldoc on word at point" cperl-perldoc-at-point t] | |
1149 | ["View manpage of POD in this file" cperl-pod-to-manpage t] | |
1150 | ["Auto-help on" cperl-lazy-install | |
1151 | (and (fboundp 'run-with-idle-timer) | |
1152 | (not cperl-lazy-installed))] | |
1153 | ["Auto-help off" (eval '(cperl-lazy-unstall)) | |
1154 | (and (fboundp 'run-with-idle-timer) | |
1155 | cperl-lazy-installed)]) | |
1156 | ("Toggle..." | |
1157 | ["Auto newline" cperl-toggle-auto-newline t] | |
1158 | ["Electric parens" cperl-toggle-electric t] | |
1159 | ["Electric keywords" cperl-toggle-abbrev t] | |
1160 | ["Fix whitespace on indent" cperl-toggle-construct-fix t] | |
1161 | ["Auto fill" auto-fill-mode t]) | |
1162 | ("Indent styles..." | |
1163 | ["CPerl" (cperl-set-style "CPerl") t] | |
1164 | ["PerlStyle" (cperl-set-style "PerlStyle") t] | |
1165 | ["GNU" (cperl-set-style "GNU") t] | |
1166 | ["C++" (cperl-set-style "C++") t] | |
1167 | ["FSF" (cperl-set-style "FSF") t] | |
1168 | ["BSD" (cperl-set-style "BSD") t] | |
1169 | ["Whitesmith" (cperl-set-style "Whitesmith") t] | |
1170 | ["Current" (cperl-set-style "Current") t] | |
1171 | ["Memorized" (cperl-set-style-back) cperl-old-style]) | |
1172 | ("Micro-docs" | |
1173 | ["Tips" (describe-variable 'cperl-tips) t] | |
1174 | ["Problems" (describe-variable 'cperl-problems) t] | |
1175 | ["Speed" (describe-variable 'cperl-speed) t] | |
1176 | ["Praise" (describe-variable 'cperl-praise) t] | |
1177 | ["Faces" (describe-variable 'cperl-tips-faces) t] | |
1178 | ["CPerl mode" (describe-function 'cperl-mode) t] | |
1179 | ["CPerl version" | |
1180 | (message "The version of master-file for this CPerl is %s-emacs" | |
1181 | cperl-version) t])))) | |
f83d2997 KH |
1182 | (error nil)) |
1183 | ||
1184 | (autoload 'c-macro-expand "cmacexp" | |
1185 | "Display the result of expanding all C macros occurring in the region. | |
1186 | The expansion is entirely correct because it uses the C preprocessor." | |
1187 | t) | |
1188 | ||
95f433f4 RS |
1189 | (defvar cperl-imenu--function-name-regexp-perl |
1190 | (concat | |
1191 | "^\\(" | |
1192 | "[ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\(([^()]*)[ \t]*\\)?" | |
1193 | "\\|" | |
1194 | "=head\\([12]\\)[ \t]+\\([^\n]+\\)$" | |
1195 | "\\)")) | |
1196 | ||
8dd511f6 RS |
1197 | (defvar cperl-outline-regexp |
1198 | (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`")) | |
1199 | ||
f83d2997 | 1200 | (defvar cperl-mode-syntax-table nil |
f94a632a | 1201 | "Syntax table in use in CPerl mode buffers.") |
f83d2997 KH |
1202 | |
1203 | (defvar cperl-string-syntax-table nil | |
f94a632a | 1204 | "Syntax table in use in CPerl mode string-like chunks.") |
f83d2997 KH |
1205 | |
1206 | (if cperl-mode-syntax-table | |
1207 | () | |
1208 | (setq cperl-mode-syntax-table (make-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 ?& "." cperl-mode-syntax-table) | |
1219 | (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table) | |
1220 | (modify-syntax-entry ?\n ">" cperl-mode-syntax-table) | |
1221 | (modify-syntax-entry ?# "<" cperl-mode-syntax-table) | |
1222 | (modify-syntax-entry ?' "\"" cperl-mode-syntax-table) | |
1223 | (modify-syntax-entry ?` "\"" cperl-mode-syntax-table) | |
1224 | (if cperl-under-as-char | |
1225 | (modify-syntax-entry ?_ "w" cperl-mode-syntax-table)) | |
1226 | (modify-syntax-entry ?: "_" cperl-mode-syntax-table) | |
1227 | (modify-syntax-entry ?| "." cperl-mode-syntax-table) | |
1228 | (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table)) | |
1229 | (modify-syntax-entry ?$ "." cperl-string-syntax-table) | |
83261a2f | 1230 | (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment ) |
f83d2997 KH |
1231 | |
1232 | ||
1233 | \f | |
db133cb6 | 1234 | (defvar cperl-faces-init nil) |
f83d2997 KH |
1235 | ;; Fix for msb.el |
1236 | (defvar cperl-msb-fixed nil) | |
83261a2f SM |
1237 | (defvar cperl-use-major-mode 'cperl-mode) |
1238 | ||
f83d2997 KH |
1239 | ;;;###autoload |
1240 | (defun cperl-mode () | |
1241 | "Major mode for editing Perl code. | |
1242 | Expression and list commands understand all C brackets. | |
1243 | Tab indents for Perl code. | |
1244 | Paragraphs are separated by blank lines only. | |
1245 | Delete converts tabs to spaces as it moves back. | |
1246 | ||
1247 | Various characters in Perl almost always come in pairs: {}, (), [], | |
1248 | sometimes <>. When the user types the first, she gets the second as | |
1249 | well, with optional special formatting done on {}. (Disabled by | |
1250 | default.) You can always quote (with \\[quoted-insert]) the left | |
1251 | \"paren\" to avoid the expansion. The processing of < is special, | |
f94a632a | 1252 | since most the time you mean \"less\". CPerl mode tries to guess |
f83d2997 KH |
1253 | whether you want to type pair <>, and inserts is if it |
1254 | appropriate. You can set `cperl-electric-parens-string' to the string that | |
1255 | contains the parenths from the above list you want to be electrical. | |
1256 | Electricity of parenths is controlled by `cperl-electric-parens'. | |
1257 | You may also set `cperl-electric-parens-mark' to have electric parens | |
1258 | look for active mark and \"embrace\" a region if possible.' | |
1259 | ||
1260 | CPerl mode provides expansion of the Perl control constructs: | |
db133cb6 | 1261 | |
5c8b7eaf | 1262 | if, else, elsif, unless, while, until, continue, do, |
db133cb6 RS |
1263 | for, foreach, formy and foreachmy. |
1264 | ||
1265 | and POD directives (Disabled by default, see `cperl-electric-keywords'.) | |
1266 | ||
1267 | The user types the keyword immediately followed by a space, which | |
1268 | causes the construct to be expanded, and the point is positioned where | |
1269 | she is most likely to want to be. eg. when the user types a space | |
1270 | following \"if\" the following appears in the buffer: if () { or if () | |
1271 | } { } and the cursor is between the parentheses. The user can then | |
1272 | type some boolean expression within the parens. Having done that, | |
1273 | typing \\[cperl-linefeed] places you - appropriately indented - on a | |
1274 | new line between the braces (if you typed \\[cperl-linefeed] in a POD | |
5c8b7eaf | 1275 | directive line, then appropriate number of new lines is inserted). |
db133cb6 RS |
1276 | |
1277 | If CPerl decides that you want to insert \"English\" style construct like | |
1278 | ||
f83d2997 | 1279 | bite if angry; |
db133cb6 RS |
1280 | |
1281 | it will not do any expansion. See also help on variable | |
1282 | `cperl-extra-newline-before-brace'. (Note that one can switch the | |
1283 | help message on expansion by setting `cperl-message-electric-keyword' | |
1284 | to nil.) | |
f83d2997 KH |
1285 | |
1286 | \\[cperl-linefeed] is a convenience replacement for typing carriage | |
1287 | return. It places you in the next line with proper indentation, or if | |
1288 | you type it inside the inline block of control construct, like | |
db133cb6 | 1289 | |
f83d2997 | 1290 | foreach (@lines) {print; print} |
db133cb6 | 1291 | |
f83d2997 KH |
1292 | and you are on a boundary of a statement inside braces, it will |
1293 | transform the construct into a multiline and will place you into an | |
5c8b7eaf SS |
1294 | appropriately indented blank line. If you need a usual |
1295 | `newline-and-indent' behaviour, it is on \\[newline-and-indent], | |
f83d2997 KH |
1296 | see documentation on `cperl-electric-linefeed'. |
1297 | ||
db133cb6 RS |
1298 | Use \\[cperl-invert-if-unless] to change a construction of the form |
1299 | ||
1300 | if (A) { B } | |
1301 | ||
1302 | into | |
1303 | ||
1304 | B if A; | |
1305 | ||
f83d2997 KH |
1306 | \\{cperl-mode-map} |
1307 | ||
db133cb6 RS |
1308 | Setting the variable `cperl-font-lock' to t switches on font-lock-mode |
1309 | \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches | |
1310 | on electric space between $ and {, `cperl-electric-parens-string' is | |
1311 | the string that contains parentheses that should be electric in CPerl | |
1312 | \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'), | |
f83d2997 KH |
1313 | setting `cperl-electric-keywords' enables electric expansion of |
1314 | control structures in CPerl. `cperl-electric-linefeed' governs which | |
1315 | one of two linefeed behavior is preferable. You can enable all these | |
1316 | options simultaneously (recommended mode of use) by setting | |
1317 | `cperl-hairy' to t. In this case you can switch separate options off | |
db133cb6 RS |
1318 | by setting them to `null'. Note that one may undo the extra |
1319 | whitespace inserted by semis and braces in `auto-newline'-mode by | |
1320 | consequent \\[cperl-electric-backspace]. | |
f83d2997 KH |
1321 | |
1322 | If your site has perl5 documentation in info format, you can use commands | |
1323 | \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it. | |
1324 | These keys run commands `cperl-info-on-current-command' and | |
1325 | `cperl-info-on-command', which one is which is controlled by variable | |
5c8b7eaf | 1326 | `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings' |
db133cb6 | 1327 | \(in turn affected by `cperl-hairy'). |
f83d2997 KH |
1328 | |
1329 | Even if you have no info-format documentation, short one-liner-style | |
db133cb6 RS |
1330 | help is available on \\[cperl-get-help], and one can run perldoc or |
1331 | man via menu. | |
f83d2997 | 1332 | |
db133cb6 RS |
1333 | It is possible to show this help automatically after some idle time. |
1334 | This is regulated by variable `cperl-lazy-help-time'. Default with | |
1335 | `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5 | |
1336 | secs idle time . It is also possible to switch this on/off from the | |
1337 | menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'. | |
f83d2997 KH |
1338 | |
1339 | Use \\[cperl-lineup] to vertically lineup some construction - put the | |
1340 | beginning of the region at the start of construction, and make region | |
1341 | span the needed amount of lines. | |
1342 | ||
1343 | Variables `cperl-pod-here-scan', `cperl-pod-here-fontify', | |
83261a2f | 1344 | `cperl-pod-face', `cperl-pod-head-face' control processing of POD and |
db133cb6 RS |
1345 | here-docs sections. With capable Emaxen results of scan are used |
1346 | for indentation too, otherwise they are used for highlighting only. | |
f83d2997 KH |
1347 | |
1348 | Variables controlling indentation style: | |
1349 | `cperl-tab-always-indent' | |
1350 | Non-nil means TAB in CPerl mode should always reindent the current line, | |
1351 | regardless of where in the line point is when the TAB command is used. | |
db133cb6 RS |
1352 | `cperl-indent-left-aligned-comments' |
1353 | Non-nil means that the comment starting in leftmost column should indent. | |
f83d2997 KH |
1354 | `cperl-auto-newline' |
1355 | Non-nil means automatically newline before and after braces, | |
1356 | and after colons and semicolons, inserted in Perl code. The following | |
1357 | \\[cperl-electric-backspace] will remove the inserted whitespace. | |
5c8b7eaf SS |
1358 | Insertion after colons requires both this variable and |
1359 | `cperl-auto-newline-after-colon' set. | |
f83d2997 KH |
1360 | `cperl-auto-newline-after-colon' |
1361 | Non-nil means automatically newline even after colons. | |
1362 | Subject to `cperl-auto-newline' setting. | |
1363 | `cperl-indent-level' | |
1364 | Indentation of Perl statements within surrounding block. | |
1365 | The surrounding block's indentation is the indentation | |
1366 | of the line on which the open-brace appears. | |
1367 | `cperl-continued-statement-offset' | |
1368 | Extra indentation given to a substatement, such as the | |
1369 | then-clause of an if, or body of a while, or just a statement continuation. | |
1370 | `cperl-continued-brace-offset' | |
1371 | Extra indentation given to a brace that starts a substatement. | |
1372 | This is in addition to `cperl-continued-statement-offset'. | |
1373 | `cperl-brace-offset' | |
1374 | Extra indentation for line if it starts with an open brace. | |
1375 | `cperl-brace-imaginary-offset' | |
1376 | An open brace following other text is treated as if it the line started | |
1377 | this far to the right of the actual line indentation. | |
1378 | `cperl-label-offset' | |
1379 | Extra indentation for line that is a label. | |
1380 | `cperl-min-label-indent' | |
1381 | Minimal indentation for line that is a label. | |
1382 | ||
1383 | Settings for K&R and BSD indentation styles are | |
1384 | `cperl-indent-level' 5 8 | |
1385 | `cperl-continued-statement-offset' 5 8 | |
1386 | `cperl-brace-offset' -5 -8 | |
1387 | `cperl-label-offset' -5 -8 | |
1388 | ||
db133cb6 RS |
1389 | CPerl knows several indentation styles, and may bulk set the |
1390 | corresponding variables. Use \\[cperl-set-style] to do this. Use | |
1391 | \\[cperl-set-style-back] to restore the memorized preexisting values | |
1392 | \(both available from menu). | |
1393 | ||
1394 | If `cperl-indent-level' is 0, the statement after opening brace in | |
5c8b7eaf | 1395 | column 0 is indented on |
db133cb6 | 1396 | `cperl-brace-offset'+`cperl-continued-statement-offset'. |
f83d2997 KH |
1397 | |
1398 | Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook' | |
db133cb6 RS |
1399 | with no args. |
1400 | ||
1401 | DO NOT FORGET to read micro-docs (available from `Perl' menu) | |
1402 | or as help on variables `cperl-tips', `cperl-problems', | |
f94a632a | 1403 | `cperl-praise', `cperl-speed'." |
f83d2997 KH |
1404 | (interactive) |
1405 | (kill-all-local-variables) | |
f83d2997 KH |
1406 | (use-local-map cperl-mode-map) |
1407 | (if (cperl-val 'cperl-electric-linefeed) | |
1408 | (progn | |
1409 | (local-set-key "\C-J" 'cperl-linefeed) | |
1410 | (local-set-key "\C-C\C-J" 'newline-and-indent))) | |
db133cb6 RS |
1411 | (if (and |
1412 | (cperl-val 'cperl-clobber-lisp-bindings) | |
1413 | (cperl-val 'cperl-info-on-command-no-prompt)) | |
f83d2997 KH |
1414 | (progn |
1415 | ;; don't clobber the backspace binding: | |
1416 | (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f]) | |
1417 | (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command | |
1418 | [(control c) (control h) f]))) | |
83261a2f | 1419 | (setq major-mode cperl-use-major-mode) |
f83d2997 KH |
1420 | (setq mode-name "CPerl") |
1421 | (if (not cperl-mode-abbrev-table) | |
1422 | (let ((prev-a-c abbrevs-changed)) | |
1423 | (define-abbrev-table 'cperl-mode-abbrev-table '( | |
1424 | ("if" "if" cperl-electric-keyword 0) | |
1425 | ("elsif" "elsif" cperl-electric-keyword 0) | |
1426 | ("while" "while" cperl-electric-keyword 0) | |
1427 | ("until" "until" cperl-electric-keyword 0) | |
1428 | ("unless" "unless" cperl-electric-keyword 0) | |
1429 | ("else" "else" cperl-electric-else 0) | |
db133cb6 | 1430 | ("continue" "continue" cperl-electric-else 0) |
f83d2997 KH |
1431 | ("for" "for" cperl-electric-keyword 0) |
1432 | ("foreach" "foreach" cperl-electric-keyword 0) | |
db133cb6 RS |
1433 | ("formy" "formy" cperl-electric-keyword 0) |
1434 | ("foreachmy" "foreachmy" cperl-electric-keyword 0) | |
1435 | ("do" "do" cperl-electric-keyword 0) | |
6c389151 SM |
1436 | ("=pod" "=pod" cperl-electric-pod 0) |
1437 | ("=over" "=over" cperl-electric-pod 0) | |
1438 | ("=head1" "=head1" cperl-electric-pod 0) | |
1439 | ("=head2" "=head2" cperl-electric-pod 0) | |
db133cb6 RS |
1440 | ("pod" "pod" cperl-electric-pod 0) |
1441 | ("over" "over" cperl-electric-pod 0) | |
1442 | ("head1" "head1" cperl-electric-pod 0) | |
1443 | ("head2" "head2" cperl-electric-pod 0))) | |
f83d2997 KH |
1444 | (setq abbrevs-changed prev-a-c))) |
1445 | (setq local-abbrev-table cperl-mode-abbrev-table) | |
1446 | (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0)) | |
1447 | (set-syntax-table cperl-mode-syntax-table) | |
6c389151 SM |
1448 | (make-local-variable 'outline-regexp) |
1449 | ;; (setq outline-regexp imenu-example--function-name-regexp-perl) | |
1450 | (setq outline-regexp cperl-outline-regexp) | |
1451 | (make-local-variable 'outline-level) | |
1452 | (setq outline-level 'cperl-outline-level) | |
f83d2997 KH |
1453 | (make-local-variable 'paragraph-start) |
1454 | (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
1455 | (make-local-variable 'paragraph-separate) | |
1456 | (setq paragraph-separate paragraph-start) | |
1457 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
1458 | (setq paragraph-ignore-fill-prefix t) | |
1459 | (make-local-variable 'indent-line-function) | |
1460 | (setq indent-line-function 'cperl-indent-line) | |
1461 | (make-local-variable 'require-final-newline) | |
1462 | (setq require-final-newline t) | |
1463 | (make-local-variable 'comment-start) | |
1464 | (setq comment-start "# ") | |
1465 | (make-local-variable 'comment-end) | |
1466 | (setq comment-end "") | |
1467 | (make-local-variable 'comment-column) | |
1468 | (setq comment-column cperl-comment-column) | |
1469 | (make-local-variable 'comment-start-skip) | |
1470 | (setq comment-start-skip "#+ *") | |
1471 | (make-local-variable 'defun-prompt-regexp) | |
1472 | (setq defun-prompt-regexp "^[ \t]*sub[ \t]+\\([^ \t\n{(;]+\\)[ \t]*") | |
1473 | (make-local-variable 'comment-indent-function) | |
1474 | (setq comment-indent-function 'cperl-comment-indent) | |
1475 | (make-local-variable 'parse-sexp-ignore-comments) | |
1476 | (setq parse-sexp-ignore-comments t) | |
1477 | (make-local-variable 'indent-region-function) | |
1478 | (setq indent-region-function 'cperl-indent-region) | |
1479 | ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off! | |
1480 | (make-local-variable 'imenu-create-index-function) | |
1481 | (setq imenu-create-index-function | |
80585273 | 1482 | (function cperl-imenu--create-perl-index)) |
f83d2997 KH |
1483 | (make-local-variable 'imenu-sort-function) |
1484 | (setq imenu-sort-function nil) | |
1485 | (make-local-variable 'vc-header-alist) | |
db133cb6 | 1486 | (set 'vc-header-alist cperl-vc-header-alist) ; Avoid warning |
f83d2997 KH |
1487 | (make-local-variable 'font-lock-defaults) |
1488 | (setq font-lock-defaults | |
db133cb6 RS |
1489 | (cond |
1490 | ((string< emacs-version "19.30") | |
5efe6a56 | 1491 | '(cperl-font-lock-keywords-2)) |
db133cb6 | 1492 | ((string< emacs-version "19.33") ; Which one to use? |
5efe6a56 SM |
1493 | '((cperl-font-lock-keywords |
1494 | cperl-font-lock-keywords-1 | |
1495 | cperl-font-lock-keywords-2))) | |
db133cb6 RS |
1496 | (t |
1497 | '((cperl-load-font-lock-keywords | |
1498 | cperl-load-font-lock-keywords-1 | |
1499 | cperl-load-font-lock-keywords-2))))) | |
1500 | (make-local-variable 'cperl-syntax-state) | |
f83d2997 KH |
1501 | (if cperl-use-syntax-table-text-property |
1502 | (progn | |
029cb4d5 | 1503 | (make-local-variable 'parse-sexp-lookup-properties) |
f83d2997 | 1504 | ;; Do not introduce variable if not needed, we check it! |
db133cb6 RS |
1505 | (set 'parse-sexp-lookup-properties t) |
1506 | ;; Fix broken font-lock: | |
1507 | (or (boundp 'font-lock-unfontify-region-function) | |
1508 | (set 'font-lock-unfontify-region-function | |
83261a2f | 1509 | 'font-lock-default-unfontify-region)) |
029cb4d5 | 1510 | (make-local-variable 'font-lock-unfontify-region-function) |
83261a2f | 1511 | (set 'font-lock-unfontify-region-function ; not present with old Emacs |
db133cb6 | 1512 | 'cperl-font-lock-unfontify-region-function) |
029cb4d5 | 1513 | (make-local-variable 'cperl-syntax-done-to) |
db133cb6 RS |
1514 | ;; Another bug: unless font-lock-syntactic-keywords, font-lock |
1515 | ;; ignores syntax-table text-property. (t) is a hack | |
1516 | ;; to make font-lock think that font-lock-syntactic-keywords | |
1517 | ;; are defined | |
029cb4d5 | 1518 | (make-local-variable 'font-lock-syntactic-keywords) |
5c8b7eaf | 1519 | (setq font-lock-syntactic-keywords |
db133cb6 RS |
1520 | (if cperl-syntaxify-by-font-lock |
1521 | '(t (cperl-fontify-syntaxically)) | |
1522 | '(t))))) | |
1523 | (make-local-variable 'cperl-old-style) | |
83261a2f SM |
1524 | (if (boundp 'normal-auto-fill-function) ; 19.33 and later |
1525 | (set (make-local-variable 'normal-auto-fill-function) | |
1526 | 'cperl-do-auto-fill) ; RMS has it as #'cperl-do-auto-fill ??? | |
1527 | (or (fboundp 'cperl-old-auto-fill-mode) | |
1528 | (progn | |
1529 | (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode)) | |
1530 | (defun auto-fill-mode (&optional arg) | |
1531 | (interactive "P") | |
1532 | (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning | |
1533 | (and auto-fill-function (memq major-mode '(perl-mode cperl-mode)) | |
1534 | (setq auto-fill-function 'cperl-do-auto-fill)))))) | |
f83d2997 | 1535 | (if (cperl-enable-font-lock) |
5c8b7eaf | 1536 | (if (cperl-val 'cperl-font-lock) |
f83d2997 KH |
1537 | (progn (or cperl-faces-init (cperl-init-faces)) |
1538 | (font-lock-mode 1)))) | |
1539 | (and (boundp 'msb-menu-cond) | |
1540 | (not cperl-msb-fixed) | |
1541 | (cperl-msb-fix)) | |
1542 | (if (featurep 'easymenu) | |
46c72468 | 1543 | (easy-menu-add cperl-menu)) ; A NOP in Emacs. |
f83d2997 KH |
1544 | (run-hooks 'cperl-mode-hook) |
1545 | ;; After hooks since fontification will break this | |
5c8b7eaf | 1546 | (if cperl-pod-here-scan |
83261a2f | 1547 | (or cperl-syntaxify-by-font-lock |
5bd52f0e RS |
1548 | (progn (or cperl-faces-init (cperl-init-faces-weak)) |
1549 | (cperl-find-pods-heres))))) | |
f83d2997 KH |
1550 | \f |
1551 | ;; Fix for perldb - make default reasonable | |
1552 | (defun cperl-db () | |
1553 | (interactive) | |
1554 | (require 'gud) | |
1555 | (perldb (read-from-minibuffer "Run perldb (like this): " | |
1556 | (if (consp gud-perldb-history) | |
1557 | (car gud-perldb-history) | |
1558 | (concat "perl " ;;(file-name-nondirectory | |
83261a2f SM |
1559 | ;; I have problems |
1560 | ;; in OS/2 | |
1561 | ;; otherwise | |
1562 | (buffer-file-name))) | |
f83d2997 KH |
1563 | nil nil |
1564 | '(gud-perldb-history . 1)))) | |
1565 | \f | |
f83d2997 KH |
1566 | (defun cperl-msb-fix () |
1567 | ;; Adds perl files to msb menu, supposes that msb is already loaded | |
1568 | (setq cperl-msb-fixed t) | |
1569 | (let* ((l (length msb-menu-cond)) | |
1570 | (last (nth (1- l) msb-menu-cond)) | |
1571 | (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last | |
1572 | (handle (1- (nth 1 last)))) | |
1573 | (setcdr precdr (list | |
1574 | (list | |
996e2616 | 1575 | '(memq major-mode '(cperl-mode perl-mode)) |
f83d2997 KH |
1576 | handle |
1577 | "Perl Files (%d)") | |
1578 | last)))) | |
1579 | \f | |
1580 | ;; This is used by indent-for-comment | |
1581 | ;; to decide how much to indent a comment in CPerl code | |
1582 | ;; based on its context. Do fallback if comment is found wrong. | |
1583 | ||
1584 | (defvar cperl-wrong-comment) | |
5bd52f0e RS |
1585 | (defvar cperl-st-cfence '(14)) ; Comment-fence |
1586 | (defvar cperl-st-sfence '(15)) ; String-fence | |
1587 | (defvar cperl-st-punct '(1)) | |
1588 | (defvar cperl-st-word '(2)) | |
1589 | (defvar cperl-st-bra '(4 . ?\>)) | |
1590 | (defvar cperl-st-ket '(5 . ?\<)) | |
1591 | ||
f83d2997 KH |
1592 | |
1593 | (defun cperl-comment-indent () | |
5bd52f0e | 1594 | (let ((p (point)) (c (current-column)) was phony) |
f83d2997 KH |
1595 | (if (looking-at "^#") 0 ; Existing comment at bol stays there. |
1596 | ;; Wrong comment found | |
1597 | (save-excursion | |
5bd52f0e RS |
1598 | (setq was (cperl-to-comment-or-eol) |
1599 | phony (eq (get-text-property (point) 'syntax-table) | |
1600 | cperl-st-cfence)) | |
1601 | (if phony | |
1602 | (progn | |
1603 | (re-search-forward "#\\|$") ; Hmm, what about embedded #? | |
1604 | (if (eq (preceding-char) ?\#) | |
1605 | (forward-char -1)) | |
1606 | (setq was nil))) | |
f83d2997 KH |
1607 | (if (= (point) p) |
1608 | (progn | |
1609 | (skip-chars-backward " \t") | |
1610 | (max (1+ (current-column)) ; Else indent at comment column | |
1611 | comment-column)) | |
1612 | (if was nil | |
1613 | (insert comment-start) | |
1614 | (backward-char (length comment-start))) | |
1615 | (setq cperl-wrong-comment t) | |
1616 | (indent-to comment-column 1) ; Indent minimum 1 | |
1617 | c))))) ; except leave at least one space. | |
1618 | ||
1619 | ;;;(defun cperl-comment-indent-fallback () | |
1620 | ;;; "Is called if the standard comment-search procedure fails. | |
1621 | ;;;Point is at start of real comment." | |
1622 | ;;; (let ((c (current-column)) target cnt prevc) | |
1623 | ;;; (if (= c comment-column) nil | |
1624 | ;;; (setq cnt (skip-chars-backward "[ \t]")) | |
5c8b7eaf | 1625 | ;;; (setq target (max (1+ (setq prevc |
f83d2997 KH |
1626 | ;;; (current-column))) ; Else indent at comment column |
1627 | ;;; comment-column)) | |
1628 | ;;; (if (= c comment-column) nil | |
1629 | ;;; (delete-backward-char cnt) | |
1630 | ;;; (while (< prevc target) | |
1631 | ;;; (insert "\t") | |
1632 | ;;; (setq prevc (current-column))) | |
1633 | ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column)))) | |
1634 | ;;; (while (< prevc target) | |
1635 | ;;; (insert " ") | |
1636 | ;;; (setq prevc (current-column))))))) | |
1637 | ||
1638 | (defun cperl-indent-for-comment () | |
1639 | "Substitute for `indent-for-comment' in CPerl." | |
1640 | (interactive) | |
1641 | (let (cperl-wrong-comment) | |
1642 | (indent-for-comment) | |
1643 | (if cperl-wrong-comment | |
1644 | (progn (cperl-to-comment-or-eol) | |
1645 | (forward-char (length comment-start)))))) | |
1646 | ||
1647 | (defun cperl-comment-region (b e arg) | |
1648 | "Comment or uncomment each line in the region in CPerl mode. | |
1649 | See `comment-region'." | |
1650 | (interactive "r\np") | |
1651 | (let ((comment-start "#")) | |
1652 | (comment-region b e arg))) | |
1653 | ||
1654 | (defun cperl-uncomment-region (b e arg) | |
1655 | "Uncomment or comment each line in the region in CPerl mode. | |
1656 | See `comment-region'." | |
1657 | (interactive "r\np") | |
1658 | (let ((comment-start "#")) | |
1659 | (comment-region b e (- arg)))) | |
1660 | ||
1661 | (defvar cperl-brace-recursing nil) | |
1662 | ||
1663 | (defun cperl-electric-brace (arg &optional only-before) | |
1664 | "Insert character and correct line's indentation. | |
1665 | If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the | |
1666 | place (even in empty line), but not after. If after \")\" and the inserted | |
5c8b7eaf | 1667 | char is \"{\", insert extra newline before only if |
f83d2997 KH |
1668 | `cperl-extra-newline-before-brace'." |
1669 | (interactive "P") | |
1670 | (let (insertpos | |
1671 | (other-end (if (and cperl-electric-parens-mark | |
5c8b7eaf | 1672 | (cperl-mark-active) |
f83d2997 | 1673 | (< (mark) (point))) |
5c8b7eaf | 1674 | (mark) |
f83d2997 KH |
1675 | nil))) |
1676 | (if (and other-end | |
1677 | (not cperl-brace-recursing) | |
1678 | (cperl-val 'cperl-electric-parens) | |
1679 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))) | |
1680 | ;; Need to insert a matching pair | |
1681 | (progn | |
1682 | (save-excursion | |
1683 | (setq insertpos (point-marker)) | |
1684 | (goto-char other-end) | |
1685 | (setq last-command-char ?\{) | |
1686 | (cperl-electric-lbrace arg insertpos)) | |
1687 | (forward-char 1)) | |
83261a2f | 1688 | ;; Check whether we close something "usual" with `}' |
db133cb6 | 1689 | (if (and (eq last-command-char ?\}) |
5c8b7eaf | 1690 | (not |
db133cb6 RS |
1691 | (condition-case nil |
1692 | (save-excursion | |
1693 | (up-list (- (prefix-numeric-value arg))) | |
1694 | ;;(cperl-after-block-p (point-min)) | |
1695 | (cperl-after-expr-p nil "{;)")) | |
1696 | (error nil)))) | |
1697 | ;; Just insert the guy | |
1698 | (self-insert-command (prefix-numeric-value arg)) | |
1699 | (if (and (not arg) ; No args, end (of empty line or auto) | |
1700 | (eolp) | |
1701 | (or (and (null only-before) | |
1702 | (save-excursion | |
1703 | (skip-chars-backward " \t") | |
1704 | (bolp))) | |
1705 | (and (eq last-command-char ?\{) ; Do not insert newline | |
1706 | ;; if after ")" and `cperl-extra-newline-before-brace' | |
1707 | ;; is nil, do not insert extra newline. | |
1708 | (not cperl-extra-newline-before-brace) | |
1709 | (save-excursion | |
1710 | (skip-chars-backward " \t") | |
1711 | (eq (preceding-char) ?\)))) | |
5c8b7eaf | 1712 | (if cperl-auto-newline |
db133cb6 RS |
1713 | (progn (cperl-indent-line) (newline) t) nil))) |
1714 | (progn | |
1715 | (self-insert-command (prefix-numeric-value arg)) | |
1716 | (cperl-indent-line) | |
1717 | (if cperl-auto-newline | |
1718 | (setq insertpos (1- (point)))) | |
1719 | (if (and cperl-auto-newline (null only-before)) | |
1720 | (progn | |
1721 | (newline) | |
1722 | (cperl-indent-line))) | |
1723 | (save-excursion | |
1724 | (if insertpos (progn (goto-char insertpos) | |
5c8b7eaf | 1725 | (search-forward (make-string |
db133cb6 RS |
1726 | 1 last-command-char)) |
1727 | (setq insertpos (1- (point))))) | |
1728 | (delete-char -1)))) | |
1729 | (if insertpos | |
f83d2997 | 1730 | (save-excursion |
db133cb6 RS |
1731 | (goto-char insertpos) |
1732 | (self-insert-command (prefix-numeric-value arg))) | |
1733 | (self-insert-command (prefix-numeric-value arg))))))) | |
f83d2997 KH |
1734 | |
1735 | (defun cperl-electric-lbrace (arg &optional end) | |
1736 | "Insert character, correct line's indentation, correct quoting by space." | |
1737 | (interactive "P") | |
83261a2f SM |
1738 | (let ((cperl-brace-recursing t) |
1739 | (cperl-auto-newline cperl-auto-newline) | |
1740 | (other-end (or end | |
1741 | (if (and cperl-electric-parens-mark | |
1742 | (cperl-mark-active) | |
1743 | (> (mark) (point))) | |
1744 | (save-excursion | |
1745 | (goto-char (mark)) | |
1746 | (point-marker)) | |
1747 | nil))) | |
1748 | pos after) | |
f83d2997 KH |
1749 | (and (cperl-val 'cperl-electric-lbrace-space) |
1750 | (eq (preceding-char) ?$) | |
1751 | (save-excursion | |
1752 | (skip-chars-backward "$") | |
1753 | (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)")) | |
1754 | (insert ?\ )) | |
bab27c0c | 1755 | ;; Check whether we are in comment |
5c8b7eaf | 1756 | (if (and |
bab27c0c RS |
1757 | (save-excursion |
1758 | (beginning-of-line) | |
1759 | (not (looking-at "[ \t]*#"))) | |
1760 | (cperl-after-expr-p nil "{;)")) | |
1761 | nil | |
1762 | (setq cperl-auto-newline nil)) | |
f83d2997 KH |
1763 | (cperl-electric-brace arg) |
1764 | (and (cperl-val 'cperl-electric-parens) | |
1765 | (eq last-command-char ?{) | |
5c8b7eaf | 1766 | (memq last-command-char |
f83d2997 KH |
1767 | (append cperl-electric-parens-string nil)) |
1768 | (or (if other-end (goto-char (marker-position other-end))) | |
1769 | t) | |
1770 | (setq last-command-char ?} pos (point)) | |
1771 | (progn (cperl-electric-brace arg t) | |
1772 | (goto-char pos))))) | |
1773 | ||
1774 | (defun cperl-electric-paren (arg) | |
1775 | "Insert a matching pair of parentheses." | |
1776 | (interactive "P") | |
1777 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
1778 | (other-end (if (and cperl-electric-parens-mark | |
5c8b7eaf | 1779 | (cperl-mark-active) |
f83d2997 | 1780 | (> (mark) (point))) |
83261a2f SM |
1781 | (save-excursion |
1782 | (goto-char (mark)) | |
1783 | (point-marker)) | |
f83d2997 KH |
1784 | nil))) |
1785 | (if (and (cperl-val 'cperl-electric-parens) | |
1786 | (memq last-command-char | |
1787 | (append cperl-electric-parens-string nil)) | |
1788 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)) | |
1789 | ;;(not (save-excursion (search-backward "#" beg t))) | |
1790 | (if (eq last-command-char ?<) | |
1791 | (progn | |
1792 | (and abbrev-mode ; later it is too late, may be after `for' | |
1793 | (expand-abbrev)) | |
1794 | (cperl-after-expr-p nil "{;(,:=")) | |
1795 | 1)) | |
1796 | (progn | |
1797 | (self-insert-command (prefix-numeric-value arg)) | |
1798 | (if other-end (goto-char (marker-position other-end))) | |
5c8b7eaf | 1799 | (insert (make-string |
f83d2997 KH |
1800 | (prefix-numeric-value arg) |
1801 | (cdr (assoc last-command-char '((?{ .?}) | |
1802 | (?[ . ?]) | |
1803 | (?( . ?)) | |
1804 | (?< . ?>)))))) | |
1805 | (forward-char (- (prefix-numeric-value arg)))) | |
1806 | (self-insert-command (prefix-numeric-value arg))))) | |
1807 | ||
1808 | (defun cperl-electric-rparen (arg) | |
1809 | "Insert a matching pair of parentheses if marking is active. | |
1810 | If not, or if we are not at the end of marking range, would self-insert." | |
1811 | (interactive "P") | |
1812 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
1813 | (other-end (if (and cperl-electric-parens-mark | |
1814 | (cperl-val 'cperl-electric-parens) | |
1815 | (memq last-command-char | |
1816 | (append cperl-electric-parens-string nil)) | |
5c8b7eaf | 1817 | (cperl-mark-active) |
f83d2997 | 1818 | (< (mark) (point))) |
5c8b7eaf | 1819 | (mark) |
f83d2997 KH |
1820 | nil)) |
1821 | p) | |
1822 | (if (and other-end | |
1823 | (cperl-val 'cperl-electric-parens) | |
1824 | (memq last-command-char '( ?\) ?\] ?\} ?\> )) | |
1825 | (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)) | |
1826 | ;;(not (save-excursion (search-backward "#" beg t))) | |
1827 | ) | |
1828 | (progn | |
1829 | (self-insert-command (prefix-numeric-value arg)) | |
1830 | (setq p (point)) | |
1831 | (if other-end (goto-char other-end)) | |
1832 | (insert (make-string | |
1833 | (prefix-numeric-value arg) | |
1834 | (cdr (assoc last-command-char '((?\} . ?\{) | |
83261a2f SM |
1835 | (?\] . ?\[) |
1836 | (?\) . ?\() | |
1837 | (?\> . ?\<)))))) | |
f83d2997 KH |
1838 | (goto-char (1+ p))) |
1839 | (self-insert-command (prefix-numeric-value arg))))) | |
1840 | ||
1841 | (defun cperl-electric-keyword () | |
db133cb6 RS |
1842 | "Insert a construction appropriate after a keyword. |
1843 | Help message may be switched off by setting `cperl-message-electric-keyword' | |
1844 | to nil." | |
5c8b7eaf | 1845 | (let ((beg (save-excursion (beginning-of-line) (point))) |
f83d2997 KH |
1846 | (dollar (and (eq last-command-char ?$) |
1847 | (eq this-command 'self-insert-command))) | |
0fe6068f | 1848 | (delete (and (memq last-command-char '(?\ ?\n ?\t ?\f)) |
db133cb6 RS |
1849 | (memq this-command '(self-insert-command newline)))) |
1850 | my do) | |
f83d2997 | 1851 | (and (save-excursion |
db133cb6 RS |
1852 | (condition-case nil |
1853 | (progn | |
1854 | (backward-sexp 1) | |
1855 | (setq do (looking-at "do\\>"))) | |
1856 | (error nil)) | |
f83d2997 | 1857 | (cperl-after-expr-p nil "{;:")) |
5c8b7eaf SS |
1858 | (save-excursion |
1859 | (not | |
f83d2997 | 1860 | (re-search-backward |
5bd52f0e | 1861 | "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>" |
f83d2997 KH |
1862 | beg t))) |
1863 | (save-excursion (or (not (re-search-backward "^=" nil t)) | |
db133cb6 RS |
1864 | (or |
1865 | (looking-at "=cut") | |
1866 | (and cperl-use-syntax-table-text-property | |
1867 | (not (eq (get-text-property (point) | |
1868 | 'syntax-type) | |
1869 | 'pod)))))) | |
f83d2997 | 1870 | (progn |
db133cb6 RS |
1871 | (and (eq (preceding-char) ?y) |
1872 | (progn ; "foreachmy" | |
1873 | (forward-char -2) | |
1874 | (insert " ") | |
1875 | (forward-char 2) | |
5c8b7eaf SS |
1876 | (setq my t dollar t |
1877 | delete | |
db133cb6 | 1878 | (memq this-command '(self-insert-command newline))))) |
f83d2997 KH |
1879 | (and dollar (insert " $")) |
1880 | (cperl-indent-line) | |
1881 | ;;(insert " () {\n}") | |
1882 | (cond | |
1883 | (cperl-extra-newline-before-brace | |
db133cb6 | 1884 | (insert (if do "\n" " ()\n")) |
f83d2997 KH |
1885 | (insert "{") |
1886 | (cperl-indent-line) | |
1887 | (insert "\n") | |
1888 | (cperl-indent-line) | |
db133cb6 RS |
1889 | (insert "\n}") |
1890 | (and do (insert " while ();"))) | |
f83d2997 | 1891 | (t |
83261a2f | 1892 | (insert (if do " {\n} while ();" " () {\n}")))) |
f83d2997 KH |
1893 | (or (looking-at "[ \t]\\|$") (insert " ")) |
1894 | (cperl-indent-line) | |
1895 | (if dollar (progn (search-backward "$") | |
5c8b7eaf | 1896 | (if my |
db133cb6 RS |
1897 | (forward-char 1) |
1898 | (delete-char 1))) | |
f83d2997 KH |
1899 | (search-backward ")")) |
1900 | (if delete | |
db133cb6 RS |
1901 | (cperl-putback-char cperl-del-back-ch)) |
1902 | (if cperl-message-electric-keyword | |
1903 | (message "Precede char by C-q to avoid expansion")))))) | |
1904 | ||
1905 | (defun cperl-ensure-newlines (n &optional pos) | |
1906 | "Make sure there are N newlines after the point." | |
1907 | (or pos (setq pos (point))) | |
1908 | (if (looking-at "\n") | |
1909 | (forward-char 1) | |
1910 | (insert "\n")) | |
1911 | (if (> n 1) | |
1912 | (cperl-ensure-newlines (1- n) pos) | |
1913 | (goto-char pos))) | |
1914 | ||
1915 | (defun cperl-electric-pod () | |
1916 | "Insert a POD chunk appropriate after a =POD directive." | |
0fe6068f | 1917 | (let ((delete (and (memq last-command-char '(?\ ?\n ?\t ?\f)) |
db133cb6 RS |
1918 | (memq this-command '(self-insert-command newline)))) |
1919 | head1 notlast name p really-delete over) | |
1920 | (and (save-excursion | |
6c389151 | 1921 | (forward-word -1) |
a1506d29 | 1922 | (and |
db133cb6 RS |
1923 | (eq (preceding-char) ?=) |
1924 | (progn | |
6c389151 SM |
1925 | (setq head1 (looking-at "head1\\>[ \t]*$")) |
1926 | (setq over (and (looking-at "over\\>[ \t]*$") | |
1927 | (not (looking-at "over[ \t]*\n\n\n*=item\\>")))) | |
db133cb6 RS |
1928 | (forward-char -1) |
1929 | (bolp)) | |
5c8b7eaf | 1930 | (or |
5bd52f0e | 1931 | (get-text-property (point) 'in-pod) |
db133cb6 RS |
1932 | (cperl-after-expr-p nil "{;:") |
1933 | (and (re-search-backward | |
a1506d29 JB |
1934 | ;; "\\(\\`\n?\\|\n\n\\)=\\sw+" |
1935 | "\\(\\`\n?\\|^\n\\)=\\sw+" | |
6c389151 | 1936 | (point-min) t) |
db133cb6 RS |
1937 | (not (or |
1938 | (looking-at "=cut") | |
1939 | (and cperl-use-syntax-table-text-property | |
1940 | (not (eq (get-text-property (point) 'syntax-type) | |
1941 | 'pod))))))))) | |
1942 | (progn | |
1943 | (save-excursion | |
6c389151 | 1944 | (setq notlast (re-search-forward "^\n=" nil t))) |
db133cb6 RS |
1945 | (or notlast |
1946 | (progn | |
1947 | (insert "\n\n=cut") | |
1948 | (cperl-ensure-newlines 2) | |
6c389151 | 1949 | (forward-word -2) |
a1506d29 JB |
1950 | (if (and head1 |
1951 | (not | |
db133cb6 RS |
1952 | (save-excursion |
1953 | (forward-char -1) | |
1954 | (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>" | |
83261a2f | 1955 | nil t)))) ; Only one |
a1506d29 | 1956 | (progn |
6c389151 | 1957 | (forward-word 1) |
db133cb6 RS |
1958 | (setq name (file-name-sans-extension |
1959 | (file-name-nondirectory (buffer-file-name))) | |
1960 | p (point)) | |
5c8b7eaf | 1961 | (insert " NAME\n\n" name |
029cb4d5 | 1962 | " - \n\n=head1 SYNOPSIS\n\n\n\n" |
db133cb6 RS |
1963 | "=head1 DESCRIPTION") |
1964 | (cperl-ensure-newlines 4) | |
1965 | (goto-char p) | |
6c389151 | 1966 | (forward-word 2) |
db133cb6 RS |
1967 | (end-of-line) |
1968 | (setq really-delete t)) | |
6c389151 | 1969 | (forward-word 1)))) |
db133cb6 RS |
1970 | (if over |
1971 | (progn | |
1972 | (setq p (point)) | |
1973 | (insert "\n\n=item \n\n\n\n" | |
1974 | "=back") | |
1975 | (cperl-ensure-newlines 2) | |
1976 | (goto-char p) | |
6c389151 | 1977 | (forward-word 1) |
db133cb6 RS |
1978 | (end-of-line) |
1979 | (setq really-delete t))) | |
1980 | (if (and delete really-delete) | |
f83d2997 KH |
1981 | (cperl-putback-char cperl-del-back-ch)))))) |
1982 | ||
1983 | (defun cperl-electric-else () | |
db133cb6 RS |
1984 | "Insert a construction appropriate after a keyword. |
1985 | Help message may be switched off by setting `cperl-message-electric-keyword' | |
1986 | to nil." | |
f83d2997 KH |
1987 | (let ((beg (save-excursion (beginning-of-line) (point)))) |
1988 | (and (save-excursion | |
1989 | (backward-sexp 1) | |
1990 | (cperl-after-expr-p nil "{;:")) | |
5c8b7eaf SS |
1991 | (save-excursion |
1992 | (not | |
f83d2997 | 1993 | (re-search-backward |
5bd52f0e | 1994 | "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>" |
f83d2997 KH |
1995 | beg t))) |
1996 | (save-excursion (or (not (re-search-backward "^=" nil t)) | |
db133cb6 RS |
1997 | (looking-at "=cut") |
1998 | (and cperl-use-syntax-table-text-property | |
1999 | (not (eq (get-text-property (point) | |
2000 | 'syntax-type) | |
2001 | 'pod))))) | |
f83d2997 KH |
2002 | (progn |
2003 | (cperl-indent-line) | |
2004 | ;;(insert " {\n\n}") | |
2005 | (cond | |
2006 | (cperl-extra-newline-before-brace | |
2007 | (insert "\n") | |
2008 | (insert "{") | |
2009 | (cperl-indent-line) | |
2010 | (insert "\n\n}")) | |
2011 | (t | |
83261a2f | 2012 | (insert " {\n\n}"))) |
f83d2997 KH |
2013 | (or (looking-at "[ \t]\\|$") (insert " ")) |
2014 | (cperl-indent-line) | |
2015 | (forward-line -1) | |
2016 | (cperl-indent-line) | |
db133cb6 RS |
2017 | (cperl-putback-char cperl-del-back-ch) |
2018 | (setq this-command 'cperl-electric-else) | |
2019 | (if cperl-message-electric-keyword | |
2020 | (message "Precede char by C-q to avoid expansion")))))) | |
f83d2997 KH |
2021 | |
2022 | (defun cperl-linefeed () | |
db133cb6 RS |
2023 | "Go to end of line, open a new line and indent appropriately. |
2024 | If in POD, insert appropriate lines." | |
f83d2997 KH |
2025 | (interactive) |
2026 | (let ((beg (save-excursion (beginning-of-line) (point))) | |
2027 | (end (save-excursion (end-of-line) (point))) | |
db133cb6 | 2028 | (pos (point)) start over cut res) |
f83d2997 | 2029 | (if (and ; Check if we need to split: |
5c8b7eaf | 2030 | ; i.e., on a boundary and inside "{...}" |
f83d2997 | 2031 | (save-excursion (cperl-to-comment-or-eol) |
83261a2f | 2032 | (>= (point) pos)) ; Not in a comment |
f83d2997 KH |
2033 | (or (save-excursion |
2034 | (skip-chars-backward " \t" beg) | |
2035 | (forward-char -1) | |
2036 | (looking-at "[;{]")) ; After { or ; + spaces | |
2037 | (looking-at "[ \t]*}") ; Before } | |
2038 | (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ; | |
2039 | (save-excursion | |
2040 | (and | |
5c8b7eaf | 2041 | (eq (car (parse-partial-sexp pos end -1)) -1) |
f83d2997 KH |
2042 | ; Leave the level of parens |
2043 | (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr | |
2044 | ; Are at end | |
6c389151 | 2045 | (cperl-after-block-p (point-min)) |
f83d2997 KH |
2046 | (progn |
2047 | (backward-sexp 1) | |
2048 | (setq start (point-marker)) | |
db133cb6 | 2049 | (<= start pos))))) ; Redundant? Are after the |
f83d2997 KH |
2050 | ; start of parens group. |
2051 | (progn | |
2052 | (skip-chars-backward " \t") | |
2053 | (or (memq (preceding-char) (append ";{" nil)) | |
2054 | (insert ";")) | |
2055 | (insert "\n") | |
2056 | (forward-line -1) | |
2057 | (cperl-indent-line) | |
2058 | (goto-char start) | |
2059 | (or (looking-at "{[ \t]*$") ; If there is a statement | |
2060 | ; before, move it to separate line | |
2061 | (progn | |
2062 | (forward-char 1) | |
2063 | (insert "\n") | |
2064 | (cperl-indent-line))) | |
2065 | (forward-line 1) ; We are on the target line | |
2066 | (cperl-indent-line) | |
2067 | (beginning-of-line) | |
2068 | (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement | |
83261a2f | 2069 | ; after, move it to separate line |
f83d2997 KH |
2070 | (progn |
2071 | (end-of-line) | |
2072 | (search-backward "}" beg) | |
2073 | (skip-chars-backward " \t") | |
2074 | (or (memq (preceding-char) (append ";{" nil)) | |
2075 | (insert ";")) | |
2076 | (insert "\n") | |
2077 | (cperl-indent-line) | |
2078 | (forward-line -1))) | |
5c8b7eaf | 2079 | (forward-line -1) ; We are on the line before target |
f83d2997 KH |
2080 | (end-of-line) |
2081 | (newline-and-indent)) | |
db133cb6 | 2082 | (end-of-line) ; else - no splitting |
f83d2997 KH |
2083 | (cond |
2084 | ((and (looking-at "\n[ \t]*{$") | |
2085 | (save-excursion | |
2086 | (skip-chars-backward " \t") | |
2087 | (eq (preceding-char) ?\)))) ; Probably if () {} group | |
83261a2f | 2088 | ; with an extra newline. |
f83d2997 KH |
2089 | (forward-line 2) |
2090 | (cperl-indent-line)) | |
db133cb6 RS |
2091 | ((save-excursion ; In POD header |
2092 | (forward-paragraph -1) | |
2093 | ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b") | |
2094 | ;; We are after \n now, so look for the rest | |
2095 | (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+") | |
5c8b7eaf | 2096 | (progn |
db133cb6 RS |
2097 | (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>")) |
2098 | (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>")) | |
2099 | t))) | |
2100 | (if (and over | |
2101 | (progn | |
2102 | (forward-paragraph -1) | |
2103 | (forward-word 1) | |
2104 | (setq pos (point)) | |
2105 | (setq cut (buffer-substring (point) | |
2106 | (save-excursion | |
2107 | (end-of-line) | |
2108 | (point)))) | |
2109 | (delete-char (- (save-excursion (end-of-line) (point)) | |
2110 | (point))) | |
2111 | (setq res (expand-abbrev)) | |
2112 | (save-excursion | |
2113 | (goto-char pos) | |
2114 | (insert cut)) | |
2115 | res)) | |
2116 | nil | |
2117 | (cperl-ensure-newlines (if cut 2 4)) | |
2118 | (forward-line 2))) | |
2119 | ((get-text-property (point) 'in-pod) ; In POD section | |
2120 | (cperl-ensure-newlines 4) | |
2121 | (forward-line 2)) | |
f83d2997 KH |
2122 | ((looking-at "\n[ \t]*$") ; Next line is empty - use it. |
2123 | (forward-line 1) | |
2124 | (cperl-indent-line)) | |
2125 | (t | |
2126 | (newline-and-indent)))))) | |
2127 | ||
2128 | (defun cperl-electric-semi (arg) | |
2129 | "Insert character and correct line's indentation." | |
2130 | (interactive "P") | |
2131 | (if cperl-auto-newline | |
2132 | (cperl-electric-terminator arg) | |
6c389151 SM |
2133 | (self-insert-command (prefix-numeric-value arg)) |
2134 | (if cperl-autoindent-on-semi | |
2135 | (cperl-indent-line)))) | |
f83d2997 KH |
2136 | |
2137 | (defun cperl-electric-terminator (arg) | |
2138 | "Insert character and correct line's indentation." | |
2139 | (interactive "P") | |
83261a2f SM |
2140 | (let ((end (point)) |
2141 | (auto (and cperl-auto-newline | |
2142 | (or (not (eq last-command-char ?:)) | |
2143 | cperl-auto-newline-after-colon))) | |
2144 | insertpos) | |
5c8b7eaf | 2145 | (if (and ;;(not arg) |
f83d2997 KH |
2146 | (eolp) |
2147 | (not (save-excursion | |
2148 | (beginning-of-line) | |
2149 | (skip-chars-forward " \t") | |
2150 | (or | |
2151 | ;; Ignore in comment lines | |
2152 | (= (following-char) ?#) | |
2153 | ;; Colon is special only after a label | |
2154 | ;; So quickly rule out most other uses of colon | |
2155 | ;; and do no indentation for them. | |
2156 | (and (eq last-command-char ?:) | |
2157 | (save-excursion | |
2158 | (forward-word 1) | |
2159 | (skip-chars-forward " \t") | |
2160 | (and (< (point) end) | |
2161 | (progn (goto-char (- end 1)) | |
2162 | (not (looking-at ":")))))) | |
2163 | (progn | |
2164 | (beginning-of-defun) | |
2165 | (let ((pps (parse-partial-sexp (point) end))) | |
2166 | (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
2167 | (progn | |
2168 | (self-insert-command (prefix-numeric-value arg)) | |
2169 | ;;(forward-char -1) | |
2170 | (if auto (setq insertpos (point-marker))) | |
2171 | ;;(forward-char 1) | |
2172 | (cperl-indent-line) | |
2173 | (if auto | |
2174 | (progn | |
2175 | (newline) | |
2176 | (cperl-indent-line))) | |
f83d2997 KH |
2177 | (save-excursion |
2178 | (if insertpos (goto-char (1- (marker-position insertpos))) | |
2179 | (forward-char -1)) | |
2180 | (delete-char 1)))) | |
2181 | (if insertpos | |
2182 | (save-excursion | |
2183 | (goto-char insertpos) | |
2184 | (self-insert-command (prefix-numeric-value arg))) | |
2185 | (self-insert-command (prefix-numeric-value arg))))) | |
2186 | ||
2187 | (defun cperl-electric-backspace (arg) | |
5c8b7eaf | 2188 | "Backspace-untabify, or remove the whitespace around the point inserted |
db133cb6 | 2189 | by an electric key." |
f83d2997 | 2190 | (interactive "p") |
5c8b7eaf SS |
2191 | (if (and cperl-auto-newline |
2192 | (memq last-command '(cperl-electric-semi | |
f83d2997 KH |
2193 | cperl-electric-terminator |
2194 | cperl-electric-lbrace)) | |
2195 | (memq (preceding-char) '(?\ ?\t ?\n))) | |
2196 | (let (p) | |
5c8b7eaf | 2197 | (if (eq last-command 'cperl-electric-lbrace) |
f83d2997 KH |
2198 | (skip-chars-forward " \t\n")) |
2199 | (setq p (point)) | |
2200 | (skip-chars-backward " \t\n") | |
2201 | (delete-region (point) p)) | |
db133cb6 RS |
2202 | (and (eq last-command 'cperl-electric-else) |
2203 | ;; We are removing the whitespace *inside* cperl-electric-else | |
2204 | (setq this-command 'cperl-electric-else-really)) | |
5c8b7eaf | 2205 | (if (and cperl-auto-newline |
db133cb6 RS |
2206 | (eq last-command 'cperl-electric-else-really) |
2207 | (memq (preceding-char) '(?\ ?\t ?\n))) | |
2208 | (let (p) | |
2209 | (skip-chars-forward " \t\n") | |
2210 | (setq p (point)) | |
2211 | (skip-chars-backward " \t\n") | |
2212 | (delete-region (point) p)) | |
2213 | (backward-delete-char-untabify arg)))) | |
f83d2997 KH |
2214 | |
2215 | (defun cperl-inside-parens-p () | |
2216 | (condition-case () | |
2217 | (save-excursion | |
2218 | (save-restriction | |
2219 | (narrow-to-region (point) | |
2220 | (progn (beginning-of-defun) (point))) | |
2221 | (goto-char (point-max)) | |
2222 | (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
2223 | (error nil))) | |
2224 | \f | |
2225 | (defun cperl-indent-command (&optional whole-exp) | |
2226 | "Indent current line as Perl code, or in some cases insert a tab character. | |
5c8b7eaf | 2227 | If `cperl-tab-always-indent' is non-nil (the default), always indent current |
db133cb6 | 2228 | line. Otherwise, indent the current line only if point is at the left margin |
f83d2997 KH |
2229 | or in the line's indentation; otherwise insert a tab. |
2230 | ||
2231 | A numeric argument, regardless of its value, | |
2232 | means indent rigidly all the lines of the expression starting after point | |
2233 | so that this line becomes properly indented. | |
2234 | The relative indentation among the lines of the expression are preserved." | |
2235 | (interactive "P") | |
5bd52f0e | 2236 | (cperl-update-syntaxification (point) (point)) |
f83d2997 KH |
2237 | (if whole-exp |
2238 | ;; If arg, always indent this line as Perl | |
2239 | ;; and shift remaining lines of expression the same amount. | |
2240 | (let ((shift-amt (cperl-indent-line)) | |
2241 | beg end) | |
2242 | (save-excursion | |
2243 | (if cperl-tab-always-indent | |
2244 | (beginning-of-line)) | |
2245 | (setq beg (point)) | |
2246 | (forward-sexp 1) | |
2247 | (setq end (point)) | |
2248 | (goto-char beg) | |
2249 | (forward-line 1) | |
2250 | (setq beg (point))) | |
db133cb6 | 2251 | (if (and shift-amt (> end beg)) |
f83d2997 KH |
2252 | (indent-code-rigidly beg end shift-amt "#"))) |
2253 | (if (and (not cperl-tab-always-indent) | |
2254 | (save-excursion | |
2255 | (skip-chars-backward " \t") | |
2256 | (not (bolp)))) | |
2257 | (insert-tab) | |
2258 | (cperl-indent-line)))) | |
2259 | ||
5bd52f0e | 2260 | (defun cperl-indent-line (&optional parse-data) |
f83d2997 KH |
2261 | "Indent current line as Perl code. |
2262 | Return the amount the indentation changed by." | |
83261a2f SM |
2263 | (let ((case-fold-search nil) |
2264 | (pos (- (point-max) (point))) | |
2265 | indent i beg shift-amt) | |
5bd52f0e | 2266 | (setq indent (cperl-calculate-indent parse-data) |
db133cb6 | 2267 | i indent) |
f83d2997 KH |
2268 | (beginning-of-line) |
2269 | (setq beg (point)) | |
2270 | (cond ((or (eq indent nil) (eq indent t)) | |
db133cb6 | 2271 | (setq indent (current-indentation) i nil)) |
f83d2997 KH |
2272 | ;;((eq indent t) ; Never? |
2273 | ;; (setq indent (cperl-calculate-indent-within-comment))) | |
2274 | ;;((looking-at "[ \t]*#") | |
2275 | ;; (setq indent 0)) | |
2276 | (t | |
2277 | (skip-chars-forward " \t") | |
2278 | (if (listp indent) (setq indent (car indent))) | |
2279 | (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]") | |
2280 | (and (> indent 0) | |
2281 | (setq indent (max cperl-min-label-indent | |
2282 | (+ indent cperl-label-offset))))) | |
2283 | ((= (following-char) ?}) | |
2284 | (setq indent (- indent cperl-indent-level))) | |
2285 | ((memq (following-char) '(?\) ?\])) ; To line up with opening paren. | |
2286 | (setq indent (+ indent cperl-close-paren-offset))) | |
2287 | ((= (following-char) ?{) | |
2288 | (setq indent (+ indent cperl-brace-offset)))))) | |
2289 | (skip-chars-forward " \t") | |
db133cb6 RS |
2290 | (setq shift-amt (and i (- indent (current-column)))) |
2291 | (if (or (not shift-amt) | |
2292 | (zerop shift-amt)) | |
f83d2997 KH |
2293 | (if (> (- (point-max) pos) (point)) |
2294 | (goto-char (- (point-max) pos))) | |
2295 | (delete-region beg (point)) | |
2296 | (indent-to indent) | |
2297 | ;; If initial point was within line's indentation, | |
2298 | ;; position after the indentation. Else stay at same point in text. | |
2299 | (if (> (- (point-max) pos) (point)) | |
2300 | (goto-char (- (point-max) pos)))) | |
2301 | shift-amt)) | |
2302 | ||
2303 | (defun cperl-after-label () | |
2304 | ;; Returns true if the point is after label. Does not do save-excursion. | |
2305 | (and (eq (preceding-char) ?:) | |
2306 | (memq (char-syntax (char-after (- (point) 2))) | |
2307 | '(?w ?_)) | |
2308 | (progn | |
2309 | (backward-sexp) | |
2310 | (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))) | |
2311 | ||
2312 | (defun cperl-get-state (&optional parse-start start-state) | |
5bd52f0e RS |
2313 | ;; returns list (START STATE DEPTH PRESTART), |
2314 | ;; START is a good place to start parsing, or equal to | |
5c8b7eaf | 2315 | ;; PARSE-START if preset, |
5bd52f0e RS |
2316 | ;; STATE is what is returned by `parse-partial-sexp'. |
2317 | ;; DEPTH is true is we are immediately after end of block | |
2318 | ;; which contains START. | |
2319 | ;; PRESTART is the position basing on which START was found. | |
f83d2997 KH |
2320 | (save-excursion |
2321 | (let ((start-point (point)) depth state start prestart) | |
5bd52f0e RS |
2322 | (if (and parse-start |
2323 | (<= parse-start start-point)) | |
f83d2997 | 2324 | (goto-char parse-start) |
5bd52f0e RS |
2325 | (beginning-of-defun) |
2326 | (setq start-state nil)) | |
f83d2997 KH |
2327 | (setq prestart (point)) |
2328 | (if start-state nil | |
2329 | ;; Try to go out, if sub is not on the outermost level | |
2330 | (while (< (point) start-point) | |
2331 | (setq start (point) parse-start start depth nil | |
2332 | state (parse-partial-sexp start start-point -1)) | |
2333 | (if (> (car state) -1) nil | |
2334 | ;; The current line could start like }}}, so the indentation | |
2335 | ;; corresponds to a different level than what we reached | |
2336 | (setq depth t) | |
2337 | (beginning-of-line 2))) ; Go to the next line. | |
2338 | (if start (goto-char start))) ; Not at the start of file | |
2339 | (setq start (point)) | |
f83d2997 KH |
2340 | (or state (setq state (parse-partial-sexp start start-point -1 nil start-state))) |
2341 | (list start state depth prestart)))) | |
2342 | ||
83261a2f | 2343 | (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" ! |
f83d2997 KH |
2344 | ;; Positions is before ?\{. Checks whether it starts a block. |
2345 | ;; No save-excursion! | |
2346 | (cperl-backward-to-noncomment (point-min)) | |
f83d2997 KH |
2347 | (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp |
2348 | ; Label may be mixed up with `$blah :' | |
2349 | (save-excursion (cperl-after-label)) | |
2350 | (and (memq (char-syntax (preceding-char)) '(?w ?_)) | |
2351 | (progn | |
2352 | (backward-sexp) | |
2353 | ;; Need take into account `bless', `return', `tr',... | |
2354 | (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax | |
5bd52f0e | 2355 | (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>"))) |
f83d2997 KH |
2356 | (progn |
2357 | (skip-chars-backward " \t\n\f") | |
2358 | (and (memq (char-syntax (preceding-char)) '(?w ?_)) | |
2359 | (progn | |
2360 | (backward-sexp) | |
5c8b7eaf | 2361 | (looking-at |
f83d2997 KH |
2362 | "sub[ \t]+[a-zA-Z0-9_:]+[ \t\n\f]*\\(([^()]*)[ \t\n\f]*\\)?[#{]"))))))))) |
2363 | ||
2364 | (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group))) | |
2365 | ||
5bd52f0e | 2366 | (defun cperl-calculate-indent (&optional parse-data) ; was parse-start |
f83d2997 KH |
2367 | "Return appropriate indentation for current line as Perl code. |
2368 | In usual case returns an integer: the column to indent to. | |
5bd52f0e RS |
2369 | Returns nil if line starts inside a string, t if in a comment. |
2370 | ||
2371 | Will not correct the indentation for labels, but will correct it for braces | |
0c602a0f | 2372 | and closing parentheses and brackets." |
f83d2997 KH |
2373 | (save-excursion |
2374 | (if (or | |
6c389151 SM |
2375 | (and (memq (get-text-property (point) 'syntax-type) |
2376 | '(pod here-doc here-doc-delim format)) | |
2377 | (not (get-text-property (point) 'indentable))) | |
f83d2997 KH |
2378 | ;; before start of POD - whitespace found since do not have 'pod! |
2379 | (and (looking-at "[ \t]*\n=") | |
83261a2f | 2380 | (error "Spaces before POD section!")) |
f83d2997 KH |
2381 | (and (not cperl-indent-left-aligned-comments) |
2382 | (looking-at "^#"))) | |
2383 | nil | |
83261a2f SM |
2384 | (beginning-of-line) |
2385 | (let ((indent-point (point)) | |
2386 | (char-after (save-excursion | |
2387 | (skip-chars-forward " \t") | |
2388 | (following-char))) | |
2389 | (in-pod (get-text-property (point) 'in-pod)) | |
2390 | (pre-indent-point (point)) | |
2391 | p prop look-prop is-block delim) | |
2392 | (cond | |
2393 | (in-pod | |
2394 | ;; In the verbatim part, probably code example. What to do??? | |
2395 | ) | |
2396 | (t | |
2397 | (save-excursion | |
2398 | ;; Not in POD | |
2399 | (cperl-backward-to-noncomment nil) | |
2400 | (setq p (max (point-min) (1- (point))) | |
2401 | prop (get-text-property p 'syntax-type) | |
2402 | look-prop (or (nth 1 (assoc prop cperl-look-for-prop)) | |
2403 | 'syntax-type)) | |
2404 | (if (memq prop '(pod here-doc format here-doc-delim)) | |
2405 | (progn | |
2406 | (goto-char (or (previous-single-property-change p look-prop) | |
2407 | (point-min))) | |
2408 | (beginning-of-line) | |
2409 | (setq pre-indent-point (point))))))) | |
2410 | (goto-char pre-indent-point) | |
2411 | (let* ((case-fold-search nil) | |
2412 | (s-s (cperl-get-state (car parse-data) (nth 1 parse-data))) | |
2413 | (start (or (nth 2 parse-data) | |
2414 | (nth 0 s-s))) | |
2415 | (state (nth 1 s-s)) | |
2416 | (containing-sexp (car (cdr state))) | |
2417 | old-indent) | |
2418 | (if (and | |
2419 | ;;containing-sexp ;; We are buggy at toplevel :-( | |
2420 | parse-data) | |
f83d2997 | 2421 | (progn |
83261a2f SM |
2422 | (setcar parse-data pre-indent-point) |
2423 | (setcar (cdr parse-data) state) | |
2424 | (or (nth 2 parse-data) | |
2425 | (setcar (cddr parse-data) start)) | |
2426 | ;; Before this point: end of statement | |
2427 | (setq old-indent (nth 3 parse-data)))) | |
2428 | (cond ((get-text-property (point) 'indentable) | |
2429 | ;; indent to just after the surrounding open, | |
2430 | ;; skip blanks if we do not close the expression. | |
2431 | (goto-char (1+ (previous-single-property-change (point) 'indentable))) | |
2432 | (or (memq char-after (append ")]}" nil)) | |
2433 | (looking-at "[ \t]*\\(#\\|$\\)") | |
2434 | (skip-chars-forward " \t")) | |
2435 | (current-column)) | |
2436 | ((or (nth 3 state) (nth 4 state)) | |
2437 | ;; return nil or t if should not change this line | |
2438 | (nth 4 state)) | |
2439 | ;; XXXX Do we need to special-case this? | |
2440 | ((null containing-sexp) | |
2441 | ;; Line is at top level. May be data or function definition, | |
2442 | ;; or may be function argument declaration. | |
2443 | ;; Indent like the previous top level line | |
2444 | ;; unless that ends in a closeparen without semicolon, | |
2445 | ;; in which case this line is the first argument decl. | |
2446 | (skip-chars-forward " \t") | |
2447 | (+ (save-excursion | |
2448 | (goto-char start) | |
2449 | (- (current-indentation) | |
2450 | (if (nth 2 s-s) cperl-indent-level 0))) | |
2451 | (if (= char-after ?{) cperl-continued-brace-offset 0) | |
2452 | (progn | |
2453 | (cperl-backward-to-noncomment (or old-indent (point-min))) | |
2454 | ;; Look at previous line that's at column 0 | |
2455 | ;; to determine whether we are in top-level decls | |
2456 | ;; or function's arg decls. Set basic-indent accordingly. | |
2457 | ;; Now add a little if this is a continuation line. | |
2458 | (if (or (bobp) | |
2459 | (eq (point) old-indent) ; old-indent was at comment | |
2460 | (eq (preceding-char) ?\;) | |
2461 | ;; Had ?\) too | |
2462 | (and (eq (preceding-char) ?\}) | |
2463 | (cperl-after-block-and-statement-beg | |
2464 | (point-min))) ; Was start - too close | |
2465 | (memq char-after (append ")]}" nil)) | |
2466 | (and (eq (preceding-char) ?\:) ; label | |
2467 | (progn | |
2468 | (forward-sexp -1) | |
2469 | (skip-chars-backward " \t") | |
2470 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))) | |
2471 | (progn | |
2472 | (if (and parse-data | |
2473 | (not (eq char-after ?\C-j))) | |
2474 | (setcdr (cddr parse-data) | |
2475 | (list pre-indent-point))) | |
2476 | 0) | |
2477 | cperl-continued-statement-offset)))) | |
2478 | ((not | |
2479 | (or (setq is-block | |
2480 | (and (setq delim (= (char-after containing-sexp) ?{)) | |
2481 | (save-excursion ; Is it a hash? | |
2482 | (goto-char containing-sexp) | |
2483 | (cperl-block-p)))) | |
2484 | cperl-indent-parens-as-block)) | |
2485 | ;; group is an expression, not a block: | |
2486 | ;; indent to just after the surrounding open parens, | |
2487 | ;; skip blanks if we do not close the expression. | |
2488 | (goto-char (1+ containing-sexp)) | |
2489 | (or (memq char-after | |
2490 | (append (if delim "}" ")]}") nil)) | |
2491 | (looking-at "[ \t]*\\(#\\|$\\)") | |
2492 | (skip-chars-forward " \t")) | |
2493 | (+ (current-column) | |
2494 | (if (and delim | |
2495 | (eq char-after ?\})) | |
2496 | ;; Correct indentation of trailing ?\} | |
2497 | (+ cperl-indent-level cperl-close-paren-offset) | |
2498 | 0))) | |
6c389151 SM |
2499 | ;;; ((and (/= (char-after containing-sexp) ?{) |
2500 | ;;; (not cperl-indent-parens-as-block)) | |
2501 | ;;; ;; line is expression, not statement: | |
2502 | ;;; ;; indent to just after the surrounding open, | |
2503 | ;;; ;; skip blanks if we do not close the expression. | |
2504 | ;;; (goto-char (1+ containing-sexp)) | |
2505 | ;;; (or (memq char-after (append ")]}" nil)) | |
2506 | ;;; (looking-at "[ \t]*\\(#\\|$\\)") | |
2507 | ;;; (skip-chars-forward " \t")) | |
2508 | ;;; (current-column)) | |
2509 | ;;; ((progn | |
2510 | ;;; ;; Containing-expr starts with \{. Check whether it is a hash. | |
2511 | ;;; (goto-char containing-sexp) | |
2512 | ;;; (and (not (cperl-block-p)) | |
2513 | ;;; (not cperl-indent-parens-as-block))) | |
2514 | ;;; (goto-char (1+ containing-sexp)) | |
2515 | ;;; (or (eq char-after ?\}) | |
2516 | ;;; (looking-at "[ \t]*\\(#\\|$\\)") | |
2517 | ;;; (skip-chars-forward " \t")) | |
2518 | ;;; (+ (current-column) ; Correct indentation of trailing ?\} | |
2519 | ;;; (if (eq char-after ?\}) (+ cperl-indent-level | |
a1506d29 | 2520 | ;;; cperl-close-paren-offset) |
6c389151 | 2521 | ;;; 0))) |
83261a2f SM |
2522 | (t |
2523 | ;; Statement level. Is it a continuation or a new statement? | |
2524 | ;; Find previous non-comment character. | |
2525 | (goto-char pre-indent-point) | |
2526 | (cperl-backward-to-noncomment containing-sexp) | |
2527 | ;; Back up over label lines, since they don't | |
2528 | ;; affect whether our line is a continuation. | |
2529 | ;; (Had \, too) | |
2530 | (while ;;(or (eq (preceding-char) ?\,) | |
2531 | (and (eq (preceding-char) ?:) | |
2532 | (or ;;(eq (char-after (- (point) 2)) ?\') ; ???? | |
2533 | (memq (char-syntax (char-after (- (point) 2))) | |
2534 | '(?w ?_)))) | |
2535 | ;;) | |
2536 | (if (eq (preceding-char) ?\,) | |
2537 | ;; Will go to beginning of line, essentially. | |
2538 | ;; Will ignore embedded sexpr XXXX. | |
2539 | (cperl-backward-to-start-of-continued-exp containing-sexp)) | |
2540 | (beginning-of-line) | |
2541 | (cperl-backward-to-noncomment containing-sexp)) | |
2542 | ;; Now we get the answer. | |
2543 | (if (not (or (eq (1- (point)) containing-sexp) | |
2544 | (memq (preceding-char) | |
2545 | (append (if is-block " ;{" " ,;{") '(nil))) | |
2546 | (and (eq (preceding-char) ?\}) | |
2547 | (cperl-after-block-and-statement-beg | |
2548 | containing-sexp)))) | |
2549 | ;; This line is continuation of preceding line's statement; | |
2550 | ;; indent `cperl-continued-statement-offset' more than the | |
2551 | ;; previous line of the statement. | |
2552 | ;; | |
2553 | ;; There might be a label on this line, just | |
2554 | ;; consider it bad style and ignore it. | |
f83d2997 | 2555 | (progn |
83261a2f SM |
2556 | (cperl-backward-to-start-of-continued-exp containing-sexp) |
2557 | (+ (if (memq char-after (append "}])" nil)) | |
2558 | 0 ; Closing parenth | |
2559 | cperl-continued-statement-offset) | |
2560 | (if (or is-block | |
2561 | (not delim) | |
2562 | (not (eq char-after ?\}))) | |
2563 | 0 | |
2564 | ;; Now it is a hash reference | |
2565 | (+ cperl-indent-level cperl-close-paren-offset)) | |
2566 | (if (looking-at "\\w+[ \t]*:") | |
2567 | (if (> (current-indentation) cperl-min-label-indent) | |
2568 | (- (current-indentation) cperl-label-offset) | |
2569 | ;; Do not move `parse-data', this should | |
2570 | ;; be quick anyway (this comment comes | |
2571 | ;; from different location): | |
2572 | (cperl-calculate-indent)) | |
2573 | (current-column)) | |
2574 | (if (eq char-after ?\{) | |
2575 | cperl-continued-brace-offset 0))) | |
2576 | ;; This line starts a new statement. | |
2577 | ;; Position following last unclosed open. | |
2578 | (goto-char containing-sexp) | |
2579 | ;; Is line first statement after an open-brace? | |
2580 | (or | |
2581 | ;; If no, find that first statement and indent like | |
2582 | ;; it. If the first statement begins with label, do | |
2583 | ;; not believe when the indentation of the label is too | |
2584 | ;; small. | |
2585 | (save-excursion | |
2586 | (forward-char 1) | |
2587 | (setq old-indent (current-indentation)) | |
2588 | (let ((colon-line-end 0)) | |
2589 | (while (progn (skip-chars-forward " \t\n") | |
2590 | (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]")) | |
2591 | ;; Skip over comments and labels following openbrace. | |
2592 | (cond ((= (following-char) ?\#) | |
2593 | (forward-line 1)) | |
2594 | ;; label: | |
2595 | (t | |
2596 | (save-excursion (end-of-line) | |
2597 | (setq colon-line-end (point))) | |
2598 | (search-forward ":")))) | |
2599 | ;; The first following code counts | |
2600 | ;; if it is before the line we want to indent. | |
2601 | (and (< (point) indent-point) | |
2602 | (if (> colon-line-end (point)) ; After label | |
2603 | (if (> (current-indentation) | |
2604 | cperl-min-label-indent) | |
2605 | (- (current-indentation) cperl-label-offset) | |
2606 | ;; Do not believe: `max' is involved | |
2607 | (+ old-indent cperl-indent-level)) | |
2608 | (current-column))))) | |
2609 | ;; If no previous statement, | |
2610 | ;; indent it relative to line brace is on. | |
2611 | ;; For open brace in column zero, don't let statement | |
2612 | ;; start there too. If cperl-indent-level is zero, | |
2613 | ;; use cperl-brace-offset + cperl-continued-statement-offset instead. | |
2614 | ;; For open-braces not the first thing in a line, | |
2615 | ;; add in cperl-brace-imaginary-offset. | |
2616 | ||
2617 | ;; If first thing on a line: ????? | |
2618 | (+ (if (and (bolp) (zerop cperl-indent-level)) | |
2619 | (+ cperl-brace-offset cperl-continued-statement-offset) | |
2620 | cperl-indent-level) | |
2621 | (if (or is-block | |
2622 | (not delim) | |
2623 | (not (eq char-after ?\}))) | |
2624 | 0 | |
2625 | ;; Now it is a hash reference | |
2626 | (+ cperl-indent-level cperl-close-paren-offset)) | |
2627 | ;; Move back over whitespace before the openbrace. | |
2628 | ;; If openbrace is not first nonwhite thing on the line, | |
2629 | ;; add the cperl-brace-imaginary-offset. | |
2630 | (progn (skip-chars-backward " \t") | |
2631 | (if (bolp) 0 cperl-brace-imaginary-offset)) | |
2632 | ;; If the openbrace is preceded by a parenthesized exp, | |
2633 | ;; move to the beginning of that; | |
2634 | ;; possibly a different line | |
2635 | (progn | |
2636 | (if (eq (preceding-char) ?\)) | |
2637 | (forward-sexp -1)) | |
2638 | ;; In the case it starts a subroutine, indent with | |
2639 | ;; respect to `sub', not with respect to the | |
2640 | ;; first thing on the line, say in the case of | |
2641 | ;; anonymous sub in a hash. | |
2642 | ;; | |
2643 | (skip-chars-backward " \t") | |
2644 | (if (and (eq (preceding-char) ?b) | |
2645 | (progn | |
2646 | (forward-sexp -1) | |
2647 | (looking-at "sub\\>")) | |
2648 | (setq old-indent | |
2649 | (nth 1 | |
2650 | (parse-partial-sexp | |
2651 | (save-excursion (beginning-of-line) (point)) | |
2652 | (point))))) | |
2653 | (progn (goto-char (1+ old-indent)) | |
2654 | (skip-chars-forward " \t") | |
2655 | (current-column)) | |
2656 | ;; Get initial indentation of the line we are on. | |
2657 | ;; If line starts with label, calculate label indentation | |
2658 | (if (save-excursion | |
2659 | (beginning-of-line) | |
2660 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]")) | |
2661 | (if (> (current-indentation) cperl-min-label-indent) | |
2662 | (- (current-indentation) cperl-label-offset) | |
2663 | ;; Do not move `parse-data', this should | |
2664 | ;; be quick anyway: | |
2665 | (cperl-calculate-indent)) | |
2666 | (current-indentation)))))))))))))) | |
f83d2997 KH |
2667 | |
2668 | (defvar cperl-indent-alist | |
2669 | '((string nil) | |
2670 | (comment nil) | |
2671 | (toplevel 0) | |
2672 | (toplevel-after-parenth 2) | |
2673 | (toplevel-continued 2) | |
2674 | (expression 1)) | |
2675 | "Alist of indentation rules for CPerl mode. | |
2676 | The values mean: | |
2677 | nil: do not indent; | |
db133cb6 RS |
2678 | number: add this amount of indentation. |
2679 | ||
2680 | Not finished, not used.") | |
f83d2997 KH |
2681 | |
2682 | (defun cperl-where-am-i (&optional parse-start start-state) | |
2683 | ;; Unfinished | |
2684 | "Return a list of lists ((TYPE POS)...) of good points before the point. | |
db133cb6 RS |
2685 | POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'. |
2686 | ||
2687 | Not finished, not used." | |
f83d2997 KH |
2688 | (save-excursion |
2689 | (let* ((start-point (point)) | |
2690 | (s-s (cperl-get-state)) | |
2691 | (start (nth 0 s-s)) | |
2692 | (state (nth 1 s-s)) | |
2693 | (prestart (nth 3 s-s)) | |
2694 | (containing-sexp (car (cdr state))) | |
2695 | (case-fold-search nil) | |
2696 | (res (list (list 'parse-start start) (list 'parse-prestart prestart)))) | |
2697 | (cond ((nth 3 state) ; In string | |
2698 | (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string | |
2699 | ((nth 4 state) ; In comment | |
2700 | (setq res (cons '(comment) res))) | |
2701 | ((null containing-sexp) | |
5c8b7eaf | 2702 | ;; Line is at top level. |
f83d2997 KH |
2703 | ;; Indent like the previous top level line |
2704 | ;; unless that ends in a closeparen without semicolon, | |
2705 | ;; in which case this line is the first argument decl. | |
2706 | (cperl-backward-to-noncomment (or parse-start (point-min))) | |
2707 | ;;(skip-chars-backward " \t\f\n") | |
2708 | (cond | |
2709 | ((or (bobp) | |
2710 | (memq (preceding-char) (append ";}" nil))) | |
2711 | (setq res (cons (list 'toplevel start) res))) | |
2712 | ((eq (preceding-char) ?\) ) | |
2713 | (setq res (cons (list 'toplevel-after-parenth start) res))) | |
5c8b7eaf | 2714 | (t |
f83d2997 KH |
2715 | (setq res (cons (list 'toplevel-continued start) res))))) |
2716 | ((/= (char-after containing-sexp) ?{) | |
2717 | ;; line is expression, not statement: | |
2718 | ;; indent to just after the surrounding open. | |
2719 | ;; skip blanks if we do not close the expression. | |
2720 | (setq res (cons (list 'expression-blanks | |
2721 | (progn | |
2722 | (goto-char (1+ containing-sexp)) | |
2723 | (or (looking-at "[ \t]*\\(#\\|$\\)") | |
2724 | (skip-chars-forward " \t")) | |
2725 | (point))) | |
2726 | (cons (list 'expression containing-sexp) res)))) | |
2727 | ((progn | |
83261a2f SM |
2728 | ;; Containing-expr starts with \{. Check whether it is a hash. |
2729 | (goto-char containing-sexp) | |
2730 | (not (cperl-block-p))) | |
f83d2997 KH |
2731 | (setq res (cons (list 'expression-blanks |
2732 | (progn | |
2733 | (goto-char (1+ containing-sexp)) | |
2734 | (or (looking-at "[ \t]*\\(#\\|$\\)") | |
2735 | (skip-chars-forward " \t")) | |
2736 | (point))) | |
2737 | (cons (list 'expression containing-sexp) res)))) | |
2738 | (t | |
2739 | ;; Statement level. | |
2740 | (setq res (cons (list 'in-block containing-sexp) res)) | |
2741 | ;; Is it a continuation or a new statement? | |
2742 | ;; Find previous non-comment character. | |
2743 | (cperl-backward-to-noncomment containing-sexp) | |
2744 | ;; Back up over label lines, since they don't | |
2745 | ;; affect whether our line is a continuation. | |
2746 | ;; Back up comma-delimited lines too ????? | |
2747 | (while (or (eq (preceding-char) ?\,) | |
2748 | (save-excursion (cperl-after-label))) | |
2749 | (if (eq (preceding-char) ?\,) | |
2750 | ;; Will go to beginning of line, essentially | |
83261a2f | 2751 | ;; Will ignore embedded sexpr XXXX. |
f83d2997 KH |
2752 | (cperl-backward-to-start-of-continued-exp containing-sexp)) |
2753 | (beginning-of-line) | |
2754 | (cperl-backward-to-noncomment containing-sexp)) | |
2755 | ;; Now we get the answer. | |
2756 | (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\, | |
2757 | ;; This line is continuation of preceding line's statement. | |
2758 | (list (list 'statement-continued containing-sexp)) | |
2759 | ;; This line starts a new statement. | |
2760 | ;; Position following last unclosed open. | |
2761 | (goto-char containing-sexp) | |
2762 | ;; Is line first statement after an open-brace? | |
2763 | (or | |
2764 | ;; If no, find that first statement and indent like | |
2765 | ;; it. If the first statement begins with label, do | |
2766 | ;; not believe when the indentation of the label is too | |
2767 | ;; small. | |
2768 | (save-excursion | |
2769 | (forward-char 1) | |
2770 | (let ((colon-line-end 0)) | |
2771 | (while (progn (skip-chars-forward " \t\n" start-point) | |
2772 | (and (< (point) start-point) | |
2773 | (looking-at | |
2774 | "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))) | |
2775 | ;; Skip over comments and labels following openbrace. | |
2776 | (cond ((= (following-char) ?\#) | |
2777 | ;;(forward-line 1) | |
2778 | (end-of-line)) | |
2779 | ;; label: | |
2780 | (t | |
2781 | (save-excursion (end-of-line) | |
2782 | (setq colon-line-end (point))) | |
2783 | (search-forward ":")))) | |
5c8b7eaf | 2784 | ;; Now at the point, after label, or at start |
f83d2997 KH |
2785 | ;; of first statement in the block. |
2786 | (and (< (point) start-point) | |
5c8b7eaf | 2787 | (if (> colon-line-end (point)) |
f83d2997 | 2788 | ;; Before statement after label |
5c8b7eaf | 2789 | (if (> (current-indentation) |
f83d2997 KH |
2790 | cperl-min-label-indent) |
2791 | (list (list 'label-in-block (point))) | |
2792 | ;; Do not believe: `max' is involved | |
2793 | (list | |
2794 | (list 'label-in-block-min-indent (point)))) | |
2795 | ;; Before statement | |
2796 | (list 'statement-in-block (point)))))) | |
2797 | ;; If no previous statement, | |
2798 | ;; indent it relative to line brace is on. | |
2799 | ;; For open brace in column zero, don't let statement | |
2800 | ;; start there too. If cperl-indent-level is zero, | |
2801 | ;; use cperl-brace-offset + cperl-continued-statement-offset instead. | |
2802 | ;; For open-braces not the first thing in a line, | |
2803 | ;; add in cperl-brace-imaginary-offset. | |
2804 | ||
2805 | ;; If first thing on a line: ????? | |
2806 | (+ (if (and (bolp) (zerop cperl-indent-level)) | |
2807 | (+ cperl-brace-offset cperl-continued-statement-offset) | |
2808 | cperl-indent-level) | |
2809 | ;; Move back over whitespace before the openbrace. | |
2810 | ;; If openbrace is not first nonwhite thing on the line, | |
2811 | ;; add the cperl-brace-imaginary-offset. | |
2812 | (progn (skip-chars-backward " \t") | |
2813 | (if (bolp) 0 cperl-brace-imaginary-offset)) | |
2814 | ;; If the openbrace is preceded by a parenthesized exp, | |
2815 | ;; move to the beginning of that; | |
2816 | ;; possibly a different line | |
2817 | (progn | |
2818 | (if (eq (preceding-char) ?\)) | |
2819 | (forward-sexp -1)) | |
2820 | ;; Get initial indentation of the line we are on. | |
2821 | ;; If line starts with label, calculate label indentation | |
2822 | (if (save-excursion | |
2823 | (beginning-of-line) | |
2824 | (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]")) | |
2825 | (if (> (current-indentation) cperl-min-label-indent) | |
2826 | (- (current-indentation) cperl-label-offset) | |
5bd52f0e | 2827 | (cperl-calculate-indent)) |
f83d2997 KH |
2828 | (current-indentation)))))))) |
2829 | res))) | |
2830 | ||
2831 | (defun cperl-calculate-indent-within-comment () | |
2832 | "Return the indentation amount for line, assuming that | |
2833 | the current line is to be regarded as part of a block comment." | |
2834 | (let (end star-start) | |
2835 | (save-excursion | |
2836 | (beginning-of-line) | |
2837 | (skip-chars-forward " \t") | |
2838 | (setq end (point)) | |
2839 | (and (= (following-char) ?#) | |
2840 | (forward-line -1) | |
2841 | (cperl-to-comment-or-eol) | |
2842 | (setq end (point))) | |
2843 | (goto-char end) | |
2844 | (current-column)))) | |
2845 | ||
2846 | ||
2847 | (defun cperl-to-comment-or-eol () | |
029cb4d5 | 2848 | "Go to position before comment on the current line, or to end of line. |
f83d2997 KH |
2849 | Returns true if comment is found." |
2850 | (let (state stop-in cpoint (lim (progn (end-of-line) (point)))) | |
83261a2f SM |
2851 | (beginning-of-line) |
2852 | (if (or | |
2853 | (eq (get-text-property (point) 'syntax-type) 'pod) | |
2854 | (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)) | |
2855 | (if (eq (preceding-char) ?\#) (progn (backward-char 1) t)) | |
2856 | ;; Else | |
2857 | (while (not stop-in) | |
2858 | (setq state (parse-partial-sexp (point) lim nil nil nil t)) | |
f83d2997 | 2859 | ; stop at comment |
83261a2f SM |
2860 | ;; If fails (beginning-of-line inside sexp), then contains not-comment |
2861 | (if (nth 4 state) ; After `#'; | |
f83d2997 KH |
2862 | ; (nth 2 state) can be |
2863 | ; beginning of m,s,qq and so | |
2864 | ; on | |
83261a2f SM |
2865 | (if (nth 2 state) |
2866 | (progn | |
2867 | (setq cpoint (point)) | |
2868 | (goto-char (nth 2 state)) | |
2869 | (cond | |
2870 | ((looking-at "\\(s\\|tr\\)\\>") | |
2871 | (or (re-search-forward | |
2872 | "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*" | |
2873 | lim 'move) | |
2874 | (setq stop-in t))) | |
2875 | ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>") | |
2876 | (or (re-search-forward | |
2877 | "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#" | |
2878 | lim 'move) | |
2879 | (setq stop-in t))) | |
2880 | (t ; It was fair comment | |
2881 | (setq stop-in t) ; Finish | |
2882 | (goto-char (1- cpoint))))) | |
2883 | (setq stop-in t) ; Finish | |
2884 | (forward-char -1)) | |
2885 | (setq stop-in t))) ; Finish | |
2886 | (nth 4 state)))) | |
f83d2997 KH |
2887 | |
2888 | (defsubst cperl-1- (p) | |
2889 | (max (point-min) (1- p))) | |
2890 | ||
2891 | (defsubst cperl-1+ (p) | |
2892 | (min (point-max) (1+ p))) | |
2893 | ||
f83d2997 KH |
2894 | (defsubst cperl-modify-syntax-type (at how) |
2895 | (if (< at (point-max)) | |
2896 | (progn | |
2897 | (put-text-property at (1+ at) 'syntax-table how) | |
2898 | (put-text-property at (1+ at) 'rear-nonsticky t)))) | |
2899 | ||
2900 | (defun cperl-protect-defun-start (s e) | |
2901 | ;; C code looks for "^\\s(" to skip comment backward in "hard" situations | |
2902 | (save-excursion | |
2903 | (goto-char s) | |
2904 | (while (re-search-forward "^\\s(" e 'to-end) | |
2905 | (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct)))) | |
2906 | ||
5bd52f0e | 2907 | (defun cperl-commentify (bb e string &optional noface) |
5c8b7eaf | 2908 | (if cperl-use-syntax-table-text-property |
5bd52f0e RS |
2909 | (if (eq noface 'n) ; Only immediate |
2910 | nil | |
f83d2997 KH |
2911 | ;; We suppose that e is _after_ the end of construction, as after eol. |
2912 | (setq string (if string cperl-st-sfence cperl-st-cfence)) | |
6c389151 SM |
2913 | (if (> bb (- e 2)) |
2914 | ;; one-char string/comment?! | |
2915 | (cperl-modify-syntax-type bb cperl-st-punct) | |
2916 | (cperl-modify-syntax-type bb string) | |
2917 | (cperl-modify-syntax-type (1- e) string)) | |
f83d2997 | 2918 | (if (and (eq string cperl-st-sfence) (> (- e 2) bb)) |
5c8b7eaf | 2919 | (put-text-property (1+ bb) (1- e) |
f83d2997 | 2920 | 'syntax-table cperl-string-syntax-table)) |
5bd52f0e RS |
2921 | (cperl-protect-defun-start bb e)) |
2922 | ;; Fontify | |
2923 | (or noface | |
2924 | (not cperl-pod-here-fontify) | |
2925 | (put-text-property bb e 'face (if string 'font-lock-string-face | |
2926 | 'font-lock-comment-face))))) | |
6c389151 | 2927 | |
5bd52f0e RS |
2928 | (defvar cperl-starters '(( ?\( . ?\) ) |
2929 | ( ?\[ . ?\] ) | |
2930 | ( ?\{ . ?\} ) | |
2931 | ( ?\< . ?\> ))) | |
f83d2997 KH |
2932 | |
2933 | (defun cperl-forward-re (lim end is-2arg set-st st-l err-l argument | |
2934 | &optional ostart oend) | |
2935 | ;; Works *before* syntax recognition is done | |
2936 | ;; May modify syntax-type text property if the situation is too hard | |
6c389151 | 2937 | (let (b starter ender st i i2 go-forward reset-st) |
f83d2997 KH |
2938 | (skip-chars-forward " \t") |
2939 | ;; ender means matching-char matcher. | |
5c8b7eaf | 2940 | (setq b (point) |
5bd52f0e RS |
2941 | starter (if (eobp) 0 (char-after b)) |
2942 | ender (cdr (assoc starter cperl-starters))) | |
f83d2997 KH |
2943 | ;; What if starter == ?\\ ???? |
2944 | (if set-st | |
2945 | (if (car st-l) | |
2946 | (setq st (car st-l)) | |
2947 | (setcar st-l (make-syntax-table)) | |
2948 | (setq i 0 st (car st-l)) | |
2949 | (while (< i 256) | |
2950 | (modify-syntax-entry i "." st) | |
2951 | (setq i (1+ i))) | |
2952 | (modify-syntax-entry ?\\ "\\" st))) | |
2953 | (setq set-st t) | |
2954 | ;; Whether we have an intermediate point | |
2955 | (setq i nil) | |
2956 | ;; Prepare the syntax table: | |
2957 | (and set-st | |
2958 | (if (not ender) ; m/blah/, s/x//, s/x/y/ | |
2959 | (modify-syntax-entry starter "$" st) | |
2960 | (modify-syntax-entry starter (concat "(" (list ender)) st) | |
2961 | (modify-syntax-entry ender (concat ")" (list starter)) st))) | |
2962 | (condition-case bb | |
2963 | (progn | |
5bd52f0e RS |
2964 | ;; We use `$' syntax class to find matching stuff, but $$ |
2965 | ;; is recognized the same as $, so we need to check this manually. | |
f83d2997 KH |
2966 | (if (and (eq starter (char-after (cperl-1+ b))) |
2967 | (not ender)) | |
2968 | ;; $ has TeXish matching rules, so $$ equiv $... | |
2969 | (forward-char 2) | |
6c389151 | 2970 | (setq reset-st (syntax-table)) |
f83d2997 KH |
2971 | (set-syntax-table st) |
2972 | (forward-sexp 1) | |
6c389151 SM |
2973 | (if (<= (point) (1+ b)) |
2974 | (error "Unfinished regular expression")) | |
2975 | (set-syntax-table reset-st) | |
2976 | (setq reset-st nil) | |
f83d2997 KH |
2977 | ;; Now the problem is with m;blah;; |
2978 | (and (not ender) | |
2979 | (eq (preceding-char) | |
2980 | (char-after (- (point) 2))) | |
2981 | (save-excursion | |
2982 | (forward-char -2) | |
2983 | (= 0 (% (skip-chars-backward "\\\\") 2))) | |
2984 | (forward-char -1))) | |
5bd52f0e | 2985 | ;; Now we are after the first part. |
f83d2997 KH |
2986 | (and is-2arg ; Have trailing part |
2987 | (not ender) | |
2988 | (eq (following-char) starter) ; Empty trailing part | |
2989 | (progn | |
2990 | (or (eq (char-syntax (following-char)) ?.) | |
2991 | ;; Make trailing letter into punctuation | |
2992 | (cperl-modify-syntax-type (point) cperl-st-punct)) | |
2993 | (setq is-2arg nil go-forward t))) ; Ignore the tail | |
2994 | (if is-2arg ; Not number => have second part | |
2995 | (progn | |
2996 | (setq i (point) i2 i) | |
2997 | (if ender | |
2998 | (if (memq (following-char) '(?\ ?\t ?\n ?\f)) | |
2999 | (progn | |
3000 | (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+") | |
3001 | (goto-char (match-end 0)) | |
3002 | (skip-chars-forward " \t\n\f")) | |
3003 | (setq i2 (point)))) | |
3004 | (forward-char -1)) | |
3005 | (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st) | |
5c8b7eaf | 3006 | (if ender (modify-syntax-entry ender "." st)) |
f83d2997 | 3007 | (setq set-st nil) |
5bd52f0e RS |
3008 | (setq ender (cperl-forward-re lim end nil t st-l err-l |
3009 | argument starter ender) | |
f83d2997 KH |
3010 | ender (nth 2 ender))))) |
3011 | (error (goto-char lim) | |
3012 | (setq set-st nil) | |
6c389151 SM |
3013 | (if reset-st |
3014 | (set-syntax-table reset-st)) | |
f83d2997 KH |
3015 | (or end |
3016 | (message | |
5bd52f0e | 3017 | "End of `%s%s%c ... %c' string/RE not found: %s" |
f83d2997 KH |
3018 | argument |
3019 | (if ostart (format "%c ... %c" ostart (or oend ostart)) "") | |
3020 | starter (or ender starter) bb) | |
3021 | (or (car err-l) (setcar err-l b))))) | |
3022 | (if set-st | |
3023 | (progn | |
3024 | (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st) | |
3025 | (if ender (modify-syntax-entry ender "." st)))) | |
5bd52f0e RS |
3026 | ;; i: have 2 args, after end of the first arg |
3027 | ;; i2: start of the second arg, if any (before delim iff `ender'). | |
3028 | ;; ender: the last arg bounded by parens-like chars, the second one of them | |
3029 | ;; starter: the starting delimiter of the first arg | |
5efe6a56 | 3030 | ;; go-forward: has 2 args, and the second part is empty |
f83d2997 KH |
3031 | (list i i2 ender starter go-forward))) |
3032 | ||
83261a2f SM |
3033 | (defvar font-lock-string-face) |
3034 | ;;(defvar font-lock-reference-face) | |
3035 | (defvar font-lock-constant-face) | |
5c8b7eaf | 3036 | (defsubst cperl-postpone-fontification (b e type val &optional now) |
5bd52f0e RS |
3037 | ;; Do after syntactic fontification? |
3038 | (if cperl-syntaxify-by-font-lock | |
3039 | (or now (put-text-property b e 'cperl-postpone (cons type val))) | |
83261a2f | 3040 | (put-text-property b e type val))) |
5bd52f0e RS |
3041 | |
3042 | ;;; Here is how the global structures (those which cannot be | |
3043 | ;;; recognized locally) are marked: | |
5c8b7eaf | 3044 | ;; a) PODs: |
5bd52f0e RS |
3045 | ;; Start-to-end is marked `in-pod' ==> t |
3046 | ;; Each non-literal part is marked `syntax-type' ==> `pod' | |
3047 | ;; Each literal part is marked `syntax-type' ==> `in-pod' | |
5c8b7eaf | 3048 | ;; b) HEREs: |
5bd52f0e RS |
3049 | ;; Start-to-end is marked `here-doc-group' ==> t |
3050 | ;; The body is marked `syntax-type' ==> `here-doc' | |
3051 | ;; The delimiter is marked `syntax-type' ==> `here-doc-delim' | |
5c8b7eaf | 3052 | ;; c) FORMATs: |
5bd52f0e | 3053 | ;; After-initial-line--to-end is marked `syntax-type' ==> `format' |
5c8b7eaf | 3054 | ;; d) 'Q'uoted string: |
5bd52f0e | 3055 | ;; part between markers inclusive is marked `syntax-type' ==> `string' |
6c389151 | 3056 | ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring' |
5bd52f0e RS |
3057 | |
3058 | (defun cperl-unwind-to-safe (before &optional end) | |
3059 | ;; if BEFORE, go to the previous start-of-line on each step of unwinding | |
3060 | (let ((pos (point)) opos) | |
3061 | (setq opos pos) | |
3062 | (while (and pos (get-text-property pos 'syntax-type)) | |
3063 | (setq pos (previous-single-property-change pos 'syntax-type)) | |
3064 | (if pos | |
3065 | (if before | |
3066 | (progn | |
3067 | (goto-char (cperl-1- pos)) | |
3068 | (beginning-of-line) | |
3069 | (setq pos (point))) | |
3070 | (goto-char (setq pos (cperl-1- pos)))) | |
3071 | ;; Up to the start | |
3072 | (goto-char (point-min)))) | |
6c389151 SM |
3073 | ;; Skip empty lines |
3074 | (and (looking-at "\n*=") | |
3075 | (/= 0 (skip-chars-backward "\n")) | |
3076 | (forward-char)) | |
3077 | (setq pos (point)) | |
5bd52f0e RS |
3078 | (if end |
3079 | ;; Do the same for end, going small steps | |
3080 | (progn | |
3081 | (while (and end (get-text-property end 'syntax-type)) | |
3082 | (setq pos end | |
3083 | end (next-single-property-change end 'syntax-type))) | |
3084 | (or end pos))))) | |
3085 | ||
6c389151 SM |
3086 | (defvar cperl-nonoverridable-face) |
3087 | (defvar font-lock-function-name-face) | |
3088 | (defvar font-lock-comment-face) | |
3089 | ||
db133cb6 | 3090 | (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max) |
f83d2997 | 3091 | "Scans the buffer for hard-to-parse Perl constructions. |
5c8b7eaf SS |
3092 | If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify |
3093 | the sections using `cperl-pod-head-face', `cperl-pod-face', | |
f83d2997 KH |
3094 | `cperl-here-face'." |
3095 | (interactive) | |
db133cb6 RS |
3096 | (or min (setq min (point-min) |
3097 | cperl-syntax-state nil | |
3098 | cperl-syntax-done-to min)) | |
f83d2997 | 3099 | (or max (setq max (point-max))) |
83261a2f SM |
3100 | (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend |
3101 | face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb | |
3102 | is-REx is-x-REx REx-comment-start REx-comment-end was-comment i2 | |
3103 | (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t) | |
3104 | (modified (buffer-modified-p)) | |
3105 | (after-change-functions nil) | |
3106 | (use-syntax-state (and cperl-syntax-state | |
3107 | (>= min (car cperl-syntax-state)))) | |
3108 | (state-point (if use-syntax-state | |
3109 | (car cperl-syntax-state) | |
3110 | (point-min))) | |
3111 | (state (if use-syntax-state | |
3112 | (cdr cperl-syntax-state))) | |
3113 | ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call! | |
3114 | (st-l (list nil)) (err-l (list nil)) | |
3115 | ;; Somehow font-lock may be not loaded yet... | |
3116 | (font-lock-string-face (if (boundp 'font-lock-string-face) | |
3117 | font-lock-string-face | |
3118 | 'font-lock-string-face)) | |
3119 | (font-lock-constant-face (if (boundp 'font-lock-constant-face) | |
3120 | font-lock-constant-face | |
3121 | 'font-lock-constant-face)) | |
3122 | (font-lock-function-name-face | |
3123 | (if (boundp 'font-lock-function-name-face) | |
3124 | font-lock-function-name-face | |
3125 | 'font-lock-function-name-face)) | |
3126 | (font-lock-comment-face | |
3127 | (if (boundp 'font-lock-comment-face) | |
3128 | font-lock-comment-face | |
3129 | 'font-lock-comment-face)) | |
3130 | (cperl-nonoverridable-face | |
3131 | (if (boundp 'cperl-nonoverridable-face) | |
3132 | cperl-nonoverridable-face | |
3133 | 'cperl-nonoverridable-face)) | |
3134 | (stop-point (if ignore-max | |
3135 | (point-max) | |
3136 | max)) | |
3137 | (search | |
3138 | (concat | |
3139 | "\\(\\`\n?\\|^\n\\)=" | |
3140 | "\\|" | |
3141 | ;; One extra () before this: | |
3142 | "<<" | |
3143 | "\\(" ; 1 + 1 | |
3144 | ;; First variant "BLAH" or just ``. | |
3145 | "[ \t]*" ; Yes, whitespace is allowed! | |
3146 | "\\([\"'`]\\)" ; 2 + 1 = 3 | |
3147 | "\\([^\"'`\n]*\\)" ; 3 + 1 | |
3148 | "\\3" | |
3149 | "\\|" | |
3150 | ;; Second variant: Identifier or \ID or empty | |
3151 | "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1 | |
3152 | ;; Do not have <<= or << 30 or <<30 or << $blah. | |
3153 | ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1 | |
3154 | "\\(\\)" ; To preserve count of pars :-( 6 + 1 | |
3155 | "\\)" | |
3156 | "\\|" | |
3157 | ;; 1+6 extra () before this: | |
3158 | "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" | |
3159 | (if cperl-use-syntax-table-text-property | |
db133cb6 | 3160 | (concat |
db133cb6 | 3161 | "\\|" |
83261a2f SM |
3162 | ;; 1+6+2=9 extra () before this: |
3163 | "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" | |
3164 | "\\|" | |
3165 | ;; 1+6+2+1=10 extra () before this: | |
3166 | "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob> | |
3167 | "\\|" | |
3168 | ;; 1+6+2+1+1=11 extra () before this: | |
3169 | "\\<sub\\>[ \t]*\\([a-zA-Z_:'0-9]+[ \t]*\\)?\\(([^()]*)\\)" | |
3170 | "\\|" | |
3171 | ;; 1+6+2+1+1+2=13 extra () before this: | |
3172 | "\\$\\(['{]\\)" | |
3173 | "\\|" | |
3174 | ;; 1+6+2+1+1+2+1=14 extra () before this: | |
3175 | "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'" | |
3176 | ;; 1+6+2+1+1+2+1+1=15 extra () before this: | |
3177 | "\\|" | |
3178 | "__\\(END\\|DATA\\)__" | |
3179 | ;; 1+6+2+1+1+2+1+1+1=16 extra () before this: | |
db133cb6 | 3180 | "\\|" |
83261a2f SM |
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 |
83261a2f SM |
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 |
a1506d29 | 3212 | ;; "\\(\\`\n?\\|^\n\\)=" |
6c389151 | 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 | 3237 | (progn |
a1506d29 | 3238 | (remove-text-properties |
6c389151 SM |
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 | |
a1506d29 | 3254 | (if cperl-pod-here-fontify |
5efe6a56 | 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 | 3260 | (cperl-put-do-not-fontify (point) e t) |
a1506d29 JB |
3261 | (if cperl-pod-here-fontify |
3262 | (progn | |
5efe6a56 SM |
3263 | ;; mark the non-literal parts as PODs |
3264 | (cperl-postpone-fontification (point) e 'face face t) | |
3265 | (goto-char bb) | |
a1506d29 | 3266 | (if (looking-at |
5efe6a56 SM |
3267 | "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$") |
3268 | ;; mark the headers | |
a1506d29 | 3269 | (cperl-postpone-fontification |
5efe6a56 | 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 | |
a1506d29 | 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)) | |
83261a2f | 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) |
83261a2f | 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) |
83261a2f | 3341 | 'face here-face))) |
f83d2997 | 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) |
83261a2f | 3383 | 'face font-lock-string-face) |
f83d2997 | 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) |
83261a2f | 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) | |
83261a2f | 3394 | 'face font-lock-string-face) |
f83d2997 | 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 ?\&) | |
83261a2f | 3422 | (not (eq (char-after ; &&m/blah/ |
6c389151 | 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)) |
a1506d29 | 3562 | (cperl-postpone-fontification |
6c389151 | 3563 | e1 (point) 'face 'cperl-nonoverridable-face))) |
5bd52f0e RS |
3564 | ;; Check whether it is m// which means "previous match" |
3565 | ;; and highlight differently | |
a1506d29 | 3566 | (setq is-REx |
6c389151 SM |
3567 | (and (string-match "^\\([sm]?\\|qr\\)$" argument) |
3568 | (or (not (= (length argument) 0)) | |
3569 | (not (eq c ?\<))))) | |
a1506d29 | 3570 | (if (and is-REx |
6c389151 | 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 | "\\((\\?\\\\#\\)\\|\\(\\\\#\\)" | |
83261a2f SM |
3603 | "\\((\\?#\\)\\|\\(#\\)") |
3604 | (if (eq (char-after b) ?\#) | |
3605 | "\\((\\?\\\\#\\)" | |
3606 | "\\((\\?#\\)")) | |
6c389151 SM |
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) | |
a1506d29 | 3620 | (progn |
6c389151 SM |
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) | |
a1506d29 | 3636 | (cperl-postpone-fontification |
6c389151 SM |
3637 | REx-comment-start REx-comment-end |
3638 | 'face font-lock-comment-face)))))) | |
3639 | (if (and is-REx is-x-REx) | |
a1506d29 | 3640 | (put-text-property (1+ b) (1- e) |
6c389151 | 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)) | |
a1506d29 | 3712 | (setq state (parse-partial-sexp |
6c389151 SM |
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) | |
83261a2f SM |
3748 | (or (looking-at "^[ \t]*\\(#\\|$\\)") |
3749 | (progn (cperl-to-comment-or-eol) (bolp)) | |
3750 | (progn | |
3751 | (skip-chars-backward " \t") | |
3752 | (if (< p (point)) (goto-char p)) | |
3753 | (setq stop t))))))) | |
f83d2997 KH |
3754 | |
3755 | (defun cperl-after-block-p (lim) | |
3756 | ;; We suppose that the preceding char is }. | |
3757 | (save-excursion | |
3758 | (condition-case nil | |
3759 | (progn | |
3760 | (forward-sexp -1) | |
3761 | (cperl-backward-to-noncomment lim) | |
bab27c0c RS |
3762 | (or (eq (point) lim) |
3763 | (eq (preceding-char) ?\) ) ; if () {} sub f () {} | |
db133cb6 RS |
3764 | (if (eq (char-syntax (preceding-char)) ?w) ; else {} |
3765 | (save-excursion | |
3766 | (forward-sexp -1) | |
6c389151 | 3767 | (or (looking-at "\\(else\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>") |
db133cb6 RS |
3768 | ;; sub f {} |
3769 | (progn | |
3770 | (cperl-backward-to-noncomment lim) | |
3771 | (and (eq (char-syntax (preceding-char)) ?w) | |
3772 | (progn | |
3773 | (forward-sexp -1) | |
3774 | (looking-at "sub\\>")))))) | |
3775 | (cperl-after-expr-p lim)))) | |
f83d2997 KH |
3776 | (error nil)))) |
3777 | ||
3778 | (defun cperl-after-expr-p (&optional lim chars test) | |
029cb4d5 | 3779 | "Return true if the position is good for start of expression. |
f83d2997 KH |
3780 | TEST is the expression to evaluate at the found position. If absent, |
3781 | CHARS is a string that contains good characters to have before us (however, | |
3782 | `}' is treated \"smartly\" if it is not in the list)." | |
83261a2f SM |
3783 | (let ((lim (or lim (point-min))) |
3784 | stop p) | |
f83d2997 KH |
3785 | (save-excursion |
3786 | (while (and (not stop) (> (point) lim)) | |
3787 | (skip-chars-backward " \t\n\f" lim) | |
3788 | (setq p (point)) | |
3789 | (beginning-of-line) | |
3790 | (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip | |
5bd52f0e | 3791 | ;; Else: last iteration, or a label |
5c8b7eaf | 3792 | (cperl-to-comment-or-eol) |
f83d2997 KH |
3793 | (skip-chars-backward " \t") |
3794 | (if (< p (point)) (goto-char p)) | |
5bd52f0e RS |
3795 | (setq p (point)) |
3796 | (if (and (eq (preceding-char) ?:) | |
3797 | (progn | |
3798 | (forward-char -1) | |
3799 | (skip-chars-backward " \t\n\f" lim) | |
3800 | (eq (char-syntax (preceding-char)) ?w))) | |
3801 | (forward-sexp -1) ; Possibly label. Skip it | |
3802 | (goto-char p) | |
3803 | (setq stop t)))) | |
bab27c0c RS |
3804 | (or (bobp) ; ???? Needed |
3805 | (eq (point) lim) | |
029cb4d5 | 3806 | (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes |
f83d2997 KH |
3807 | (progn |
3808 | (if test (eval test) | |
3809 | (or (memq (preceding-char) (append (or chars "{;") nil)) | |
3810 | (and (eq (preceding-char) ?\}) | |
3811 | (cperl-after-block-p lim))))))))) | |
3812 | ||
3813 | (defun cperl-backward-to-start-of-continued-exp (lim) | |
3814 | (if (memq (preceding-char) (append ")]}\"'`" nil)) | |
3815 | (forward-sexp -1)) | |
3816 | (beginning-of-line) | |
3817 | (if (<= (point) lim) | |
3818 | (goto-char (1+ lim))) | |
3819 | (skip-chars-forward " \t")) | |
3820 | ||
db133cb6 RS |
3821 | (defun cperl-after-block-and-statement-beg (lim) |
3822 | ;; We assume that we are after ?\} | |
5c8b7eaf | 3823 | (and |
db133cb6 RS |
3824 | (cperl-after-block-p lim) |
3825 | (save-excursion | |
3826 | (forward-sexp -1) | |
3827 | (cperl-backward-to-noncomment (point-min)) | |
3828 | (or (bobp) | |
bab27c0c | 3829 | (eq (point) lim) |
db133cb6 RS |
3830 | (not (= (char-syntax (preceding-char)) ?w)) |
3831 | (progn | |
3832 | (forward-sexp -1) | |
5c8b7eaf | 3833 | (not |
db133cb6 RS |
3834 | (looking-at |
3835 | "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>"))))))) | |
3836 | ||
f83d2997 | 3837 | \f |
f83d2997 KH |
3838 | (defun cperl-indent-exp () |
3839 | "Simple variant of indentation of continued-sexp. | |
5bd52f0e RS |
3840 | |
3841 | Will not indent comment if it starts at `comment-indent' or looks like | |
3842 | continuation of the comment on the previous line. | |
db133cb6 | 3843 | |
5c8b7eaf | 3844 | If `cperl-indent-region-fix-constructs', will improve spacing on |
db133cb6 | 3845 | conditional/loop constructs." |
f83d2997 KH |
3846 | (interactive) |
3847 | (save-excursion | |
3848 | (let ((tmp-end (progn (end-of-line) (point))) top done) | |
3849 | (save-excursion | |
3850 | (beginning-of-line) | |
3851 | (while (null done) | |
3852 | (setq top (point)) | |
3853 | (while (= (nth 0 (parse-partial-sexp (point) tmp-end | |
3854 | -1)) -1) | |
3855 | (setq top (point))) ; Get the outermost parenths in line | |
3856 | (goto-char top) | |
3857 | (while (< (point) tmp-end) | |
3858 | (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol | |
3859 | (or (eolp) (forward-sexp 1))) | |
5bd52f0e RS |
3860 | (if (> (point) tmp-end) |
3861 | (save-excursion | |
3862 | (end-of-line) | |
3863 | (setq tmp-end (point))) | |
f83d2997 KH |
3864 | (setq done t))) |
3865 | (goto-char tmp-end) | |
3866 | (setq tmp-end (point-marker))) | |
db133cb6 RS |
3867 | (if cperl-indent-region-fix-constructs |
3868 | (cperl-fix-line-spacing tmp-end)) | |
f83d2997 KH |
3869 | (cperl-indent-region (point) tmp-end)))) |
3870 | ||
5bd52f0e RS |
3871 | (defun cperl-fix-line-spacing (&optional end parse-data) |
3872 | "Improve whitespace in a conditional/loop construct. | |
3873 | Returns some position at the last line." | |
db133cb6 RS |
3874 | (interactive) |
3875 | (or end | |
3876 | (setq end (point-max))) | |
83261a2f SM |
3877 | (let ((ee (save-excursion (end-of-line) (point))) |
3878 | (cperl-indent-region-fix-constructs | |
3879 | (or cperl-indent-region-fix-constructs 1)) | |
3880 | p pp ml have-brace ret) | |
db133cb6 RS |
3881 | (save-excursion |
3882 | (beginning-of-line) | |
5bd52f0e | 3883 | (setq ret (point)) |
5c8b7eaf | 3884 | ;; }? continue |
5bd52f0e | 3885 | ;; blah; } |
5c8b7eaf | 3886 | (if (not |
5bd52f0e RS |
3887 | (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)") |
3888 | (setq have-brace (save-excursion (search-forward "}" ee t))))) | |
3889 | nil ; Do not need to do anything | |
83261a2f SM |
3890 | ;; Looking at: |
3891 | ;; } | |
3892 | ;; else | |
3893 | (if (and cperl-merge-trailing-else | |
3894 | (looking-at | |
3895 | "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")) | |
3896 | (progn | |
3897 | (search-forward "}") | |
3898 | (setq p (point)) | |
3899 | (skip-chars-forward " \t\n") | |
3900 | (delete-region p (point)) | |
3901 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3902 | (beginning-of-line))) | |
3903 | ;; Looking at: | |
3904 | ;; } else | |
3905 | (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>") | |
3906 | (progn | |
3907 | (search-forward "}") | |
3908 | (delete-horizontal-space) | |
3909 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3910 | (beginning-of-line))) | |
3911 | ;; Looking at: | |
3912 | ;; else { | |
3913 | (if (looking-at | |
3914 | "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]") | |
3915 | (progn | |
3916 | (forward-word 1) | |
3917 | (delete-horizontal-space) | |
3918 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3919 | (beginning-of-line))) | |
3920 | ;; Looking at: | |
3921 | ;; foreach my $var | |
3922 | (if (looking-at | |
3923 | "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]") | |
3924 | (progn | |
3925 | (forward-word 2) | |
3926 | (delete-horizontal-space) | |
3927 | (insert (make-string cperl-indent-region-fix-constructs ?\ )) | |
3928 | (beginning-of-line))) | |
3929 | ;; Looking at: | |
3930 | ;; foreach my $var ( | |
3931 | (if (looking-at | |
6c389151 | 3932 | "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]") |
83261a2f SM |
3933 | (progn |
3934 | (forward-word 3) | |
3935 | (delete-horizontal-space) | |
3936 | (insert | |
3937 | (make-string cperl-indent-region-fix-constructs ?\ )) | |
3938 | (beginning-of-line))) | |
3939 | ;; Looking at: | |
3940 | ;; } foreach my $var () { | |
3941 | (if (looking-at | |
ce22dd53 | 3942 | "[ \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]*{") |
83261a2f SM |
3943 | (progn |
3944 | (setq ml (match-beginning 8)) | |
3945 | (re-search-forward "[({]") | |
3946 | (forward-char -1) | |
3947 | (setq p (point)) | |
3948 | (if (eq (following-char) ?\( ) | |
3949 | (progn | |
3950 | (forward-sexp 1) | |
3951 | (setq pp (point))) | |
3952 | ;; after `else' or nothing | |
3953 | (if ml ; after `else' | |
3954 | (skip-chars-backward " \t\n") | |
3955 | (beginning-of-line)) | |
3956 | (setq pp nil)) | |
3957 | ;; Now after the sexp before the brace | |
3958 | ;; Multiline expr should be special | |
3959 | (setq ml (and pp (save-excursion (goto-char p) | |
3960 | (search-forward "\n" pp t)))) | |
3961 | (if (and (or (not pp) (< pp end)) | |
3962 | (looking-at "[ \t\n]*{")) | |
3963 | (progn | |
3964 | (cond | |
3965 | ((bolp) ; Were before `{', no if/else/etc | |
3966 | nil) | |
3967 | ((looking-at "\\(\t*\\| [ \t]+\\){") | |
3968 | (delete-horizontal-space) | |
3969 | (if (if ml | |
3970 | cperl-extra-newline-before-brace-multiline | |
3971 | cperl-extra-newline-before-brace) | |
3972 | (progn | |
3973 | (delete-horizontal-space) | |
3974 | (insert "\n") | |
3975 | (setq ret (point)) | |
3976 | (if (cperl-indent-line parse-data) | |
3977 | (progn | |
3978 | (cperl-fix-line-spacing end parse-data) | |
3979 | (setq ret (point))))) | |
3980 | (insert | |
3981 | (make-string cperl-indent-region-fix-constructs ?\ )))) | |
3982 | ((and (looking-at "[ \t]*\n") | |
3983 | (not (if ml | |
3984 | cperl-extra-newline-before-brace-multiline | |
3985 | cperl-extra-newline-before-brace))) | |
3986 | (setq pp (point)) | |
3987 | (skip-chars-forward " \t\n") | |
3988 | (delete-region pp (point)) | |
db133cb6 RS |
3989 | (insert |
3990 | (make-string cperl-indent-region-fix-constructs ?\ )))) | |
83261a2f SM |
3991 | ;; Now we are before `{' |
3992 | (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]") | |
3993 | (progn | |
3994 | (skip-chars-forward " \t\n") | |
3995 | (setq pp (point)) | |
3996 | (forward-sexp 1) | |
3997 | (setq p (point)) | |
3998 | (goto-char pp) | |
3999 | (setq ml (search-forward "\n" p t)) | |
4000 | (if (or cperl-break-one-line-blocks-when-indent ml) | |
4001 | ;; not good: multi-line BLOCK | |
4002 | (progn | |
4003 | (goto-char (1+ pp)) | |
4004 | (delete-horizontal-space) | |
4005 | (insert "\n") | |
4006 | (setq ret (point)) | |
4007 | (if (cperl-indent-line parse-data) | |
4008 | (setq ret (cperl-fix-line-spacing end parse-data))))))))))) | |
4009 | (beginning-of-line) | |
4010 | (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee. | |
4011 | ;; Now check whether there is a hanging `}' | |
4012 | ;; Looking at: | |
4013 | ;; } blah | |
4014 | (if (and | |
4015 | cperl-fix-hanging-brace-when-indent | |
4016 | have-brace | |
4017 | (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)")) | |
4018 | (condition-case nil | |
4019 | (progn | |
4020 | (up-list 1) | |
4021 | (if (and (<= (point) pp) | |
4022 | (eq (preceding-char) ?\} ) | |
4023 | (cperl-after-block-and-statement-beg (point-min))) | |
4024 | t | |
4025 | (goto-char p) | |
4026 | nil)) | |
4027 | (error nil))) | |
4028 | (progn | |
4029 | (forward-char -1) | |
4030 | (skip-chars-backward " \t") | |
4031 | (if (bolp) | |
4032 | ;; `}' was the first thing on the line, insert NL *after* it. | |
4033 | (progn | |
4034 | (cperl-indent-line parse-data) | |
4035 | (search-forward "}") | |
4036 | (delete-horizontal-space) | |
4037 | (insert "\n")) | |
4038 | (delete-horizontal-space) | |
4039 | (or (eq (preceding-char) ?\;) | |
4040 | (bolp) | |
4041 | (and (eq (preceding-char) ?\} ) | |
4042 | (cperl-after-block-p (point-min))) | |
4043 | (insert ";")) | |
4044 | (insert "\n") | |
4045 | (setq ret (point))) | |
4046 | (if (cperl-indent-line parse-data) | |
4047 | (setq ret (cperl-fix-line-spacing end parse-data))) | |
4048 | (beginning-of-line))))) | |
5bd52f0e RS |
4049 | ret)) |
4050 | ||
4051 | (defvar cperl-update-start) ; Do not need to make them local | |
4052 | (defvar cperl-update-end) | |
4053 | (defun cperl-delay-update-hook (beg end old-len) | |
4054 | (setq cperl-update-start (min beg (or cperl-update-start (point-max)))) | |
4055 | (setq cperl-update-end (max end (or cperl-update-end (point-min))))) | |
db133cb6 | 4056 | |
f83d2997 KH |
4057 | (defun cperl-indent-region (start end) |
4058 | "Simple variant of indentation of region in CPerl mode. | |
5c8b7eaf | 4059 | Should be slow. Will not indent comment if it starts at `comment-indent' |
f83d2997 | 4060 | or looks like continuation of the comment on the previous line. |
5c8b7eaf SS |
4061 | Indents all the lines whose first character is between START and END |
4062 | inclusive. | |
db133cb6 | 4063 | |
5c8b7eaf | 4064 | If `cperl-indent-region-fix-constructs', will improve spacing on |
db133cb6 | 4065 | conditional/loop constructs." |
f83d2997 | 4066 | (interactive "r") |
5bd52f0e | 4067 | (cperl-update-syntaxification end end) |
f83d2997 | 4068 | (save-excursion |
5bd52f0e | 4069 | (let (cperl-update-start cperl-update-end (h-a-c after-change-functions)) |
83261a2f SM |
4070 | (let ((indent-info (if cperl-emacs-can-parse |
4071 | (list nil nil nil) ; Cannot use '(), since will modify | |
4072 | nil)) | |
4073 | (pm 0) (imenu-scanning-message "Indenting... (%3d%%)") | |
4074 | after-change-functions ; Speed it up! | |
4075 | st comm old-comm-indent new-comm-indent p pp i empty) | |
5bd52f0e | 4076 | (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook)) |
83261a2f SM |
4077 | (goto-char start) |
4078 | (setq old-comm-indent (and (cperl-to-comment-or-eol) | |
4079 | (current-column)) | |
4080 | new-comm-indent old-comm-indent) | |
4081 | (goto-char start) | |
4082 | (setq end (set-marker (make-marker) end)) ; indentation changes pos | |
4083 | (or (bolp) (beginning-of-line 2)) | |
4084 | (or (fboundp 'imenu-progress-message) | |
4085 | (message "Indenting... For feedback load `imenu'...")) | |
4086 | (while (and (<= (point) end) (not (eobp))) ; bol to check start | |
4087 | (and (fboundp 'imenu-progress-message) | |
4088 | (imenu-progress-message | |
4089 | pm (/ (* 100 (- (point) start)) (- end start -1)))) | |
5bd52f0e RS |
4090 | (setq st (point)) |
4091 | (if (or | |
4092 | (setq empty (looking-at "[ \t]*\n")) | |
4093 | (and (setq comm (looking-at "[ \t]*#")) | |
83261a2f SM |
4094 | (or (eq (current-indentation) (or old-comm-indent |
4095 | comment-column)) | |
5bd52f0e | 4096 | (setq old-comm-indent nil)))) |
f83d2997 | 4097 | (if (and old-comm-indent |
5bd52f0e | 4098 | (not empty) |
f83d2997 | 4099 | (= (current-indentation) old-comm-indent) |
5bd52f0e RS |
4100 | (not (eq (get-text-property (point) 'syntax-type) 'pod)) |
4101 | (not (eq (get-text-property (point) 'syntax-table) | |
4102 | cperl-st-cfence))) | |
83261a2f SM |
4103 | (let ((comment-column new-comm-indent)) |
4104 | (indent-for-comment))) | |
4105 | (progn | |
5bd52f0e | 4106 | (setq i (cperl-indent-line indent-info)) |
f83d2997 | 4107 | (or comm |
db133cb6 | 4108 | (not i) |
f83d2997 | 4109 | (progn |
db133cb6 | 4110 | (if cperl-indent-region-fix-constructs |
5bd52f0e | 4111 | (goto-char (cperl-fix-line-spacing end indent-info))) |
83261a2f SM |
4112 | (if (setq old-comm-indent |
4113 | (and (cperl-to-comment-or-eol) | |
4114 | (not (memq (get-text-property (point) | |
4115 | 'syntax-type) | |
4116 | '(pod here-doc))) | |
5c8b7eaf | 4117 | (not (eq (get-text-property (point) |
5bd52f0e RS |
4118 | 'syntax-table) |
4119 | cperl-st-cfence)) | |
f83d2997 KH |
4120 | (current-column))) |
4121 | (progn (indent-for-comment) | |
4122 | (skip-chars-backward " \t") | |
4123 | (skip-chars-backward "#") | |
4124 | (setq new-comm-indent (current-column)))))))) | |
4125 | (beginning-of-line 2)) | |
4126 | (if (fboundp 'imenu-progress-message) | |
4127 | (imenu-progress-message pm 100) | |
5bd52f0e RS |
4128 | (message nil))) |
4129 | ;; Now run the update hooks | |
83261a2f SM |
4130 | (and after-change-functions |
4131 | cperl-update-end | |
4132 | (save-excursion | |
4133 | (goto-char cperl-update-end) | |
4134 | (insert " ") | |
4135 | (delete-char -1) | |
4136 | (goto-char cperl-update-start) | |
4137 | (insert " ") | |
4138 | (delete-char -1)))))) | |
f83d2997 | 4139 | |
f83d2997 KH |
4140 | ;; Stolen from lisp-mode with a lot of improvements |
4141 | ||
4142 | (defun cperl-fill-paragraph (&optional justify iteration) | |
4143 | "Like \\[fill-paragraph], but handle CPerl comments. | |
4144 | If any of the current line is a comment, fill the comment or the | |
4145 | block of it that point is in, preserving the comment's initial | |
4146 | indentation and initial hashes. Behaves usually outside of comment." | |
4147 | (interactive "P") | |
83261a2f | 4148 | (let (;; Non-nil if the current line contains a comment. |
f83d2997 KH |
4149 | has-comment |
4150 | ||
4151 | ;; If has-comment, the appropriate fill-prefix for the comment. | |
4152 | comment-fill-prefix | |
4153 | ;; Line that contains code and comment (or nil) | |
4154 | start | |
4155 | c spaces len dc (comment-column comment-column)) | |
4156 | ;; Figure out what kind of comment we are looking at. | |
4157 | (save-excursion | |
4158 | (beginning-of-line) | |
4159 | (cond | |
4160 | ||
4161 | ;; A line with nothing but a comment on it? | |
4162 | ((looking-at "[ \t]*#[# \t]*") | |
4163 | (setq has-comment t | |
4164 | comment-fill-prefix (buffer-substring (match-beginning 0) | |
4165 | (match-end 0)))) | |
4166 | ||
4167 | ;; A line with some code, followed by a comment? Remember that the | |
4168 | ;; semi which starts the comment shouldn't be part of a string or | |
4169 | ;; character. | |
4170 | ((cperl-to-comment-or-eol) | |
4171 | (setq has-comment t) | |
4172 | (looking-at "#+[ \t]*") | |
5c8b7eaf | 4173 | (setq start (point) c (current-column) |
f83d2997 KH |
4174 | comment-fill-prefix |
4175 | (concat (make-string (current-column) ?\ ) | |
4176 | (buffer-substring (match-beginning 0) (match-end 0))) | |
5c8b7eaf | 4177 | spaces (progn (skip-chars-backward " \t") |
f83d2997 | 4178 | (buffer-substring (point) start)) |
5c8b7eaf | 4179 | dc (- c (current-column)) len (- start (point)) |
f83d2997 KH |
4180 | start (point-marker)) |
4181 | (delete-char len) | |
4182 | (insert (make-string dc ?-))))) | |
4183 | (if (not has-comment) | |
83261a2f | 4184 | (fill-paragraph justify) ; Do the usual thing outside of comment |
f83d2997 KH |
4185 | ;; Narrow to include only the comment, and then fill the region. |
4186 | (save-restriction | |
4187 | (narrow-to-region | |
4188 | ;; Find the first line we should include in the region to fill. | |
4189 | (if start (progn (beginning-of-line) (point)) | |
4190 | (save-excursion | |
4191 | (while (and (zerop (forward-line -1)) | |
4192 | (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]"))) | |
4193 | ;; We may have gone to far. Go forward again. | |
4194 | (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]") | |
4195 | (forward-line 1)) | |
4196 | (point))) | |
4197 | ;; Find the beginning of the first line past the region to fill. | |
4198 | (save-excursion | |
4199 | (while (progn (forward-line 1) | |
4200 | (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]"))) | |
4201 | (point))) | |
4202 | ;; Remove existing hashes | |
4203 | (goto-char (point-min)) | |
4204 | (while (progn (forward-line 1) (< (point) (point-max))) | |
4205 | (skip-chars-forward " \t") | |
5c8b7eaf | 4206 | (and (looking-at "#+") |
f83d2997 KH |
4207 | (delete-char (- (match-end 0) (match-beginning 0))))) |
4208 | ||
4209 | ;; Lines with only hashes on them can be paragraph boundaries. | |
4210 | (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$")) | |
4211 | (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$")) | |
4212 | (fill-prefix comment-fill-prefix)) | |
4213 | (fill-paragraph justify))) | |
4214 | (if (and start) | |
5c8b7eaf | 4215 | (progn |
f83d2997 KH |
4216 | (goto-char start) |
4217 | (if (> dc 0) | |
83261a2f | 4218 | (progn (delete-char dc) (insert spaces))) |
f83d2997 KH |
4219 | (if (or (= (current-column) c) iteration) nil |
4220 | (setq comment-column c) | |
4221 | (indent-for-comment) | |
4222 | ;; Repeat once more, flagging as iteration | |
4223 | (cperl-fill-paragraph justify t))))))) | |
4224 | ||
4225 | (defun cperl-do-auto-fill () | |
4226 | ;; Break out if the line is short enough | |
4227 | (if (> (save-excursion | |
4228 | (end-of-line) | |
4229 | (current-column)) | |
4230 | fill-column) | |
83261a2f SM |
4231 | (let ((c (save-excursion (beginning-of-line) |
4232 | (cperl-to-comment-or-eol) (point))) | |
4233 | (s (memq (following-char) '(?\ ?\t))) marker) | |
4234 | (if (>= c (point)) nil | |
4235 | (setq marker (point-marker)) | |
4236 | (cperl-fill-paragraph) | |
4237 | (goto-char marker) | |
4238 | ;; Is not enough, sometimes marker is a start of line | |
4239 | (if (bolp) (progn (re-search-forward "#+[ \t]*") | |
4240 | (goto-char (match-end 0)))) | |
4241 | ;; Following space could have gone: | |
4242 | (if (or (not s) (memq (following-char) '(?\ ?\t))) nil | |
4243 | (insert " ") | |
4244 | (backward-char 1)) | |
4245 | ;; Previous space could have gone: | |
4246 | (or (memq (preceding-char) '(?\ ?\t)) (insert " ")))))) | |
f83d2997 | 4247 | |
f83d2997 KH |
4248 | (defun cperl-imenu-addback (lst &optional isback name) |
4249 | ;; We suppose that the lst is a DAG, unless the first element only | |
4250 | ;; loops back, and ISBACK is set. Thus this function cannot be | |
4251 | ;; applied twice without ISBACK set. | |
4252 | (cond ((not cperl-imenu-addback) lst) | |
4253 | (t | |
5c8b7eaf | 4254 | (or name |
f83d2997 | 4255 | (setq name "+++BACK+++")) |
83261a2f SM |
4256 | (mapcar (lambda (elt) |
4257 | (if (and (listp elt) (listp (cdr elt))) | |
4258 | (progn | |
4259 | ;; In the other order it goes up | |
4260 | ;; one level only ;-( | |
4261 | (setcdr elt (cons (cons name lst) | |
4262 | (cdr elt))) | |
4263 | (cperl-imenu-addback (cdr elt) t name)))) | |
f83d2997 KH |
4264 | (if isback (cdr lst) lst)) |
4265 | lst))) | |
4266 | ||
80585273 | 4267 | (defun cperl-imenu--create-perl-index (&optional regexp) |
f83d2997 | 4268 | (require 'imenu) ; May be called from TAGS creator |
5c8b7eaf | 4269 | (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '()) |
f83d2997 KH |
4270 | (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function)) |
4271 | (index-meth-alist '()) meth | |
6c389151 | 4272 | packages ends-ranges p marker |
f83d2997 KH |
4273 | (prev-pos 0) char fchar index index1 name (end-range 0) package) |
4274 | (goto-char (point-min)) | |
4275 | (if noninteractive | |
4276 | (message "Scanning Perl for index") | |
4277 | (imenu-progress-message prev-pos 0)) | |
6c389151 | 4278 | (cperl-update-syntaxification (point-max) (point-max)) |
f83d2997 KH |
4279 | ;; Search for the function |
4280 | (progn ;;save-match-data | |
4281 | (while (re-search-forward | |
80585273 | 4282 | (or regexp cperl-imenu--function-name-regexp-perl) |
f83d2997 KH |
4283 | nil t) |
4284 | (or noninteractive | |
4285 | (imenu-progress-message prev-pos)) | |
f83d2997 KH |
4286 | (cond |
4287 | ((and ; Skip some noise if building tags | |
4288 | (match-beginning 2) ; package or sub | |
4289 | (eq (char-after (match-beginning 2)) ?p) ; package | |
4290 | (not (save-match-data | |
83261a2f | 4291 | (looking-at "[ \t\n]*;")))) ; Plain text word 'package' |
f83d2997 KH |
4292 | nil) |
4293 | ((and | |
4294 | (match-beginning 2) ; package or sub | |
6c389151 | 4295 | ;; Skip if quoted (will not skip multi-line ''-strings :-(): |
f83d2997 KH |
4296 | (null (get-text-property (match-beginning 1) 'syntax-table)) |
4297 | (null (get-text-property (match-beginning 1) 'syntax-type)) | |
4298 | (null (get-text-property (match-beginning 1) 'in-pod))) | |
4299 | (save-excursion | |
4300 | (goto-char (match-beginning 2)) | |
83261a2f | 4301 | (setq fchar (following-char))) |
f83d2997 KH |
4302 | ;; (if (looking-at "([^()]*)[ \t\n\f]*") |
4303 | ;; (goto-char (match-end 0))) ; Messes what follows | |
6c389151 | 4304 | (setq char (following-char) ; ?\; for "sub foo () ;" |
f83d2997 KH |
4305 | meth nil |
4306 | p (point)) | |
4307 | (while (and ends-ranges (>= p (car ends-ranges))) | |
4308 | ;; delete obsolete entries | |
4309 | (setq ends-ranges (cdr ends-ranges) packages (cdr packages))) | |
4310 | (setq package (or (car packages) "") | |
4311 | end-range (or (car ends-ranges) 0)) | |
4312 | (if (eq fchar ?p) | |
4313 | (setq name (buffer-substring (match-beginning 3) (match-end 3)) | |
4314 | name (progn | |
4315 | (set-text-properties 0 (length name) nil name) | |
4316 | name) | |
5c8b7eaf | 4317 | package (concat name "::") |
f83d2997 | 4318 | name (concat "package " name) |
5c8b7eaf | 4319 | end-range |
f83d2997 KH |
4320 | (save-excursion |
4321 | (parse-partial-sexp (point) (point-max) -1) (point)) | |
4322 | ends-ranges (cons end-range ends-ranges) | |
4323 | packages (cons package packages))) | |
4324 | ;; ) | |
4325 | ;; Skip this function name if it is a prototype declaration. | |
4326 | (if (and (eq fchar ?s) (eq char ?\;)) nil | |
6c389151 SM |
4327 | (setq name (buffer-substring (match-beginning 3) (match-end 3)) |
4328 | marker (make-marker)) | |
4329 | (set-text-properties 0 (length name) nil name) | |
4330 | (set-marker marker (match-end 3)) | |
a1506d29 | 4331 | (if (eq fchar ?p) |
6c389151 | 4332 | (setq name (concat "package " name)) |
f83d2997 KH |
4333 | (cond ((string-match "[:']" name) |
4334 | (setq meth t)) | |
4335 | ((> p end-range) nil) | |
5c8b7eaf | 4336 | (t |
f83d2997 | 4337 | (setq name (concat package name) meth t)))) |
6c389151 | 4338 | (setq index (cons name marker)) |
a1506d29 | 4339 | (if (eq fchar ?p) |
f83d2997 KH |
4340 | (push index index-pack-alist) |
4341 | (push index index-alist)) | |
4342 | (if meth (push index index-meth-alist)) | |
4343 | (push index index-unsorted-alist))) | |
83261a2f | 4344 | ((match-beginning 5) ; POD section |
f83d2997 KH |
4345 | ;; (beginning-of-line) |
4346 | (setq index (imenu-example--name-and-position) | |
4347 | name (buffer-substring (match-beginning 6) (match-end 6))) | |
4348 | (set-text-properties 0 (length name) nil name) | |
4349 | (if (eq (char-after (match-beginning 5)) ?2) | |
4350 | (setq name (concat " " name))) | |
4351 | (setcar index name) | |
4352 | (setq index1 (cons (concat "=" name) (cdr index))) | |
4353 | (push index index-pod-alist) | |
4354 | (push index1 index-unsorted-alist))))) | |
4355 | (or noninteractive | |
4356 | (imenu-progress-message prev-pos 100)) | |
5c8b7eaf | 4357 | (setq index-alist |
f83d2997 KH |
4358 | (if (default-value 'imenu-sort-function) |
4359 | (sort index-alist (default-value 'imenu-sort-function)) | |
83261a2f | 4360 | (nreverse index-alist))) |
f83d2997 KH |
4361 | (and index-pod-alist |
4362 | (push (cons "+POD headers+..." | |
4363 | (nreverse index-pod-alist)) | |
4364 | index-alist)) | |
4365 | (and (or index-pack-alist index-meth-alist) | |
4366 | (let ((lst index-pack-alist) hier-list pack elt group name) | |
4367 | ;; Remove "package ", reverse and uniquify. | |
4368 | (while lst | |
4369 | (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8)) | |
4370 | (if (assoc name hier-list) nil | |
4371 | (setq hier-list (cons (cons name (cdr elt)) hier-list)))) | |
4372 | (setq lst index-meth-alist) | |
4373 | (while lst | |
4374 | (setq elt (car lst) lst (cdr lst)) | |
4375 | (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt)) | |
4376 | (setq pack (substring (car elt) 0 (match-beginning 0))) | |
5c8b7eaf | 4377 | (if (setq group (assoc pack hier-list)) |
f83d2997 KH |
4378 | (if (listp (cdr group)) |
4379 | ;; Have some functions already | |
5c8b7eaf SS |
4380 | (setcdr group |
4381 | (cons (cons (substring | |
f83d2997 KH |
4382 | (car elt) |
4383 | (+ 2 (match-beginning 0))) | |
4384 | (cdr elt)) | |
4385 | (cdr group))) | |
5c8b7eaf | 4386 | (setcdr group (list (cons (substring |
f83d2997 KH |
4387 | (car elt) |
4388 | (+ 2 (match-beginning 0))) | |
4389 | (cdr elt))))) | |
5c8b7eaf SS |
4390 | (setq hier-list |
4391 | (cons (cons pack | |
4392 | (list (cons (substring | |
f83d2997 KH |
4393 | (car elt) |
4394 | (+ 2 (match-beginning 0))) | |
4395 | (cdr elt)))) | |
4396 | hier-list)))))) | |
4397 | (push (cons "+Hierarchy+..." | |
4398 | hier-list) | |
4399 | index-alist))) | |
4400 | (and index-pack-alist | |
4401 | (push (cons "+Packages+..." | |
4402 | (nreverse index-pack-alist)) | |
4403 | index-alist)) | |
5c8b7eaf | 4404 | (and (or index-pack-alist index-pod-alist |
f83d2997 KH |
4405 | (default-value 'imenu-sort-function)) |
4406 | index-unsorted-alist | |
4407 | (push (cons "+Unsorted List+..." | |
4408 | (nreverse index-unsorted-alist)) | |
4409 | index-alist)) | |
4410 | (cperl-imenu-addback index-alist))) | |
4411 | ||
6c389151 | 4412 | \f |
6c389151 SM |
4413 | ;; Suggested by Mark A. Hershberger |
4414 | (defun cperl-outline-level () | |
4415 | (looking-at outline-regexp) | |
4416 | (cond ((not (match-beginning 1)) 0) ; beginning-of-file | |
4417 | ((match-beginning 2) | |
4418 | (if (eq (char-after (match-beginning 2)) ?p) | |
4419 | 0 ; package | |
4420 | 1)) ; sub | |
4421 | ((match-beginning 5) | |
4422 | (if (eq (char-after (match-beginning 5)) ?1) | |
4423 | 1 ; head1 | |
4424 | 2)) ; head2 | |
4425 | (t 3))) ; should not happen | |
4426 | ||
4427 | \f | |
a1506d29 | 4428 | (defvar cperl-compilation-error-regexp-alist |
f83d2997 KH |
4429 | ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK). |
4430 | '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]" | |
4431 | 2 3)) | |
4432 | "Alist that specifies how to match errors in perl output.") | |
4433 | ||
4434 | (if (fboundp 'eval-after-load) | |
4435 | (eval-after-load | |
83261a2f SM |
4436 | "mode-compile" |
4437 | '(setq perl-compilation-error-regexp-alist | |
4438 | cperl-compilation-error-regexp-alist))) | |
f83d2997 KH |
4439 | |
4440 | ||
f83d2997 KH |
4441 | (defun cperl-windowed-init () |
4442 | "Initialization under windowed version." | |
db133cb6 RS |
4443 | (if (or (featurep 'ps-print) cperl-faces-init) |
4444 | ;; Need to init anyway: | |
4445 | (or cperl-faces-init (cperl-init-faces)) | |
4446 | (add-hook 'font-lock-mode-hook | |
4447 | (function | |
4448 | (lambda () | |
996e2616 | 4449 | (if (memq major-mode '(perl-mode cperl-mode)) |
db133cb6 RS |
4450 | (progn |
4451 | (or cperl-faces-init (cperl-init-faces))))))) | |
4452 | (if (fboundp 'eval-after-load) | |
4453 | (eval-after-load | |
83261a2f SM |
4454 | "ps-print" |
4455 | '(or cperl-faces-init (cperl-init-faces)))))) | |
db133cb6 | 4456 | |
5efe6a56 | 4457 | (defvar cperl-font-lock-keywords-1 nil |
80585273 | 4458 | "Additional expressions to highlight in Perl mode. Minimal set.") |
5efe6a56 | 4459 | (defvar cperl-font-lock-keywords nil |
80585273 | 4460 | "Additional expressions to highlight in Perl mode. Default set.") |
5efe6a56 | 4461 | (defvar cperl-font-lock-keywords-2 nil |
80585273 DL |
4462 | "Additional expressions to highlight in Perl mode. Maximal set") |
4463 | ||
db133cb6 RS |
4464 | (defun cperl-load-font-lock-keywords () |
4465 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4466 | cperl-font-lock-keywords) |
db133cb6 RS |
4467 | |
4468 | (defun cperl-load-font-lock-keywords-1 () | |
4469 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4470 | cperl-font-lock-keywords-1) |
db133cb6 RS |
4471 | |
4472 | (defun cperl-load-font-lock-keywords-2 () | |
4473 | (or cperl-faces-init (cperl-init-faces)) | |
5efe6a56 | 4474 | cperl-font-lock-keywords-2) |
f83d2997 | 4475 | |
5bd52f0e RS |
4476 | (defun cperl-init-faces-weak () |
4477 | ;; Allow `cperl-find-pods-heres' to run. | |
4478 | (or (boundp 'font-lock-constant-face) | |
4479 | (cperl-force-face font-lock-constant-face | |
4480 | "Face for constant and label names") | |
4481 | ;;(setq font-lock-constant-face 'font-lock-constant-face) | |
4482 | )) | |
4483 | ||
f83d2997 | 4484 | (defun cperl-init-faces () |
5bd52f0e | 4485 | (condition-case errs |
f83d2997 KH |
4486 | (progn |
4487 | (require 'font-lock) | |
4488 | (and (fboundp 'font-lock-fontify-anchored-keywords) | |
4489 | (featurep 'font-lock-extra) | |
4490 | (message "You have an obsolete package `font-lock-extra'. Install `choose-color'.")) | |
4491 | (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored) | |
f83d2997 KH |
4492 | (if (fboundp 'font-lock-fontify-anchored-keywords) |
4493 | (setq font-lock-anchored t)) | |
5c8b7eaf | 4494 | (setq |
f83d2997 KH |
4495 | t-font-lock-keywords |
4496 | (list | |
ac6857fb | 4497 | `("[ \t]+$" 0 ',cperl-invalid-face t) |
f83d2997 KH |
4498 | (cons |
4499 | (concat | |
4500 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
4501 | (mapconcat | |
4502 | 'identity | |
4503 | '("if" "until" "while" "elsif" "else" "unless" "for" | |
4504 | "foreach" "continue" "exit" "die" "last" "goto" "next" | |
4505 | "redo" "return" "local" "exec" "sub" "do" "dump" "use" | |
6c389151 | 4506 | "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT") |
f83d2997 KH |
4507 | "\\|") ; Flow control |
4508 | "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]" | |
4509 | ; In what follows we use `type' style | |
4510 | ; for overwritable builtins | |
4511 | (list | |
4512 | (concat | |
4513 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
4514 | ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm" | |
4515 | ;; "and" "atan2" "bind" "binmode" "bless" "caller" | |
4516 | ;; "chdir" "chmod" "chown" "chr" "chroot" "close" | |
4517 | ;; "closedir" "cmp" "connect" "continue" "cos" "crypt" | |
4518 | ;; "dbmclose" "dbmopen" "die" "dump" "endgrent" | |
4519 | ;; "endhostent" "endnetent" "endprotoent" "endpwent" | |
4520 | ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl" | |
4521 | ;; "fileno" "flock" "fork" "formline" "ge" "getc" | |
4522 | ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr" | |
4523 | ;; "gethostbyname" "gethostent" "getlogin" | |
4524 | ;; "getnetbyaddr" "getnetbyname" "getnetent" | |
4525 | ;; "getpeername" "getpgrp" "getppid" "getpriority" | |
4526 | ;; "getprotobyname" "getprotobynumber" "getprotoent" | |
4527 | ;; "getpwent" "getpwnam" "getpwuid" "getservbyname" | |
4528 | ;; "getservbyport" "getservent" "getsockname" | |
4529 | ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int" | |
4530 | ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length" | |
5bd52f0e | 4531 | ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt" |
f83d2997 KH |
4532 | ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne" |
4533 | ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe" | |
4534 | ;; "quotemeta" "rand" "read" "readdir" "readline" | |
4535 | ;; "readlink" "readpipe" "recv" "ref" "rename" "require" | |
4536 | ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek" | |
4537 | ;; "seekdir" "select" "semctl" "semget" "semop" "send" | |
4538 | ;; "setgrent" "sethostent" "setnetent" "setpgrp" | |
4539 | ;; "setpriority" "setprotoent" "setpwent" "setservent" | |
4540 | ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite" | |
4541 | ;; "shutdown" "sin" "sleep" "socket" "socketpair" | |
4542 | ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink" | |
6c389151 | 4543 | ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell" |
f83d2997 KH |
4544 | ;; "telldir" "time" "times" "truncate" "uc" "ucfirst" |
4545 | ;; "umask" "unlink" "unpack" "utime" "values" "vec" | |
4546 | ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor" | |
5c8b7eaf | 4547 | "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|" |
f83d2997 KH |
4548 | "b\\(in\\(d\\|mode\\)\\|less\\)\\|" |
4549 | "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|" | |
4550 | "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|" | |
4551 | "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|" | |
4552 | "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|" | |
4553 | "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|" | |
4554 | "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|" | |
4555 | "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|" | |
4556 | "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w" | |
4557 | "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|" | |
4558 | "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|" | |
4559 | "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|" | |
4560 | "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|" | |
4561 | "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|" | |
4562 | "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e" | |
5bd52f0e | 4563 | "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|" |
f83d2997 KH |
4564 | "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|" |
4565 | "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|" | |
4566 | "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin" | |
4567 | "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name" | |
4568 | "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r" | |
4569 | "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|" | |
4570 | "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|" | |
4571 | "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|" | |
6c389151 | 4572 | "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|" |
f83d2997 KH |
4573 | "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|" |
4574 | "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|" | |
4575 | "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|" | |
4576 | "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|" | |
4577 | "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)" | |
4578 | "\\)\\>") 2 'font-lock-type-face) | |
4579 | ;; In what follows we use `other' style | |
4580 | ;; for nonoverwritable builtins | |
4581 | ;; Somehow 's', 'm' are not auto-generated??? | |
4582 | (list | |
4583 | (concat | |
4584 | "\\(^\\|[^$@%&\\]\\)\\<\\(" | |
6c389151 | 4585 | ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp" |
f83d2997 KH |
4586 | ;; "chop" "defined" "delete" "do" "each" "else" "elsif" |
4587 | ;; "eval" "exists" "for" "foreach" "format" "goto" | |
4588 | ;; "grep" "if" "keys" "last" "local" "map" "my" "next" | |
4589 | ;; "no" "package" "pop" "pos" "print" "printf" "push" | |
4590 | ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift" | |
4591 | ;; "sort" "splice" "split" "study" "sub" "tie" "tr" | |
4592 | ;; "undef" "unless" "unshift" "untie" "until" "use" | |
4593 | ;; "while" "y" | |
6c389151 | 4594 | "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|" |
f83d2997 | 4595 | "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|" |
6c389151 SM |
4596 | "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|" |
4597 | "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|" | |
f83d2997 | 4598 | "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|" |
5bd52f0e | 4599 | "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|" |
f83d2997 KH |
4600 | "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|" |
4601 | "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|" | |
4602 | "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually | |
4603 | "\\|[sm]" ; Added manually | |
5bd52f0e | 4604 | "\\)\\>") 2 'cperl-nonoverridable-face) |
f83d2997 KH |
4605 | ;; (mapconcat 'identity |
4606 | ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if" | |
4607 | ;; "#include" "#define" "#undef") | |
4608 | ;; "\\|") | |
4609 | '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0 | |
4610 | font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]" | |
5bd52f0e | 4611 | '("\\<sub[ \t]+\\([^ \t{;()]+\\)[ \t]*\\(([^()]*)[ \t]*\\)?[#{\n]" 1 |
f83d2997 KH |
4612 | font-lock-function-name-face) |
4613 | '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B; | |
4614 | 2 font-lock-function-name-face) | |
4615 | '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$" | |
4616 | 1 font-lock-function-name-face) | |
4617 | (cond ((featurep 'font-lock-extra) | |
5c8b7eaf | 4618 | '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" |
f83d2997 KH |
4619 | (2 font-lock-string-face t) |
4620 | (0 '(restart 2 t)))) ; To highlight $a{bc}{ef} | |
4621 | (font-lock-anchored | |
4622 | '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4623 | (2 font-lock-string-face t) | |
4624 | ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4625 | nil nil | |
4626 | (1 font-lock-string-face t)))) | |
4627 | (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}" | |
4628 | 2 font-lock-string-face t))) | |
db133cb6 | 4629 | '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1 |
f83d2997 | 4630 | font-lock-string-face t) |
5c8b7eaf | 4631 | '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1 |
83261a2f | 4632 | font-lock-constant-face) ; labels |
f83d2997 | 4633 | '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets |
883212ce | 4634 | 2 font-lock-constant-face) |
6c389151 SM |
4635 | ;; Uncomment to get perl-mode-like vars |
4636 | ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face) | |
4637 | ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)" | |
4638 | ;;; (2 (cons font-lock-variable-name-face '(underline)))) | |
f83d2997 | 4639 | (cond ((featurep 'font-lock-extra) |
6c389151 | 4640 | '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?" |
f83d2997 KH |
4641 | (3 font-lock-variable-name-face) |
4642 | (4 '(another 4 nil | |
4643 | ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?" | |
4644 | (1 font-lock-variable-name-face) | |
5c8b7eaf | 4645 | (2 '(restart 2 nil) nil t))) |
f83d2997 KH |
4646 | nil t))) ; local variables, multiple |
4647 | (font-lock-anchored | |
6c389151 | 4648 | '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)" |
f83d2997 KH |
4649 | (3 font-lock-variable-name-face) |
4650 | ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)" | |
4651 | nil nil | |
4652 | (1 font-lock-variable-name-face)))) | |
6c389151 | 4653 | (t '("^[ \t{}]*\\(my\\|local\\our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)" |
f83d2997 | 4654 | 3 font-lock-variable-name-face))) |
6c389151 SM |
4655 | '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*(" |
4656 | 4 font-lock-variable-name-face))) | |
a1506d29 | 4657 | (setq |
f83d2997 KH |
4658 | t-font-lock-keywords-1 |
4659 | (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock | |
83261a2f | 4660 | (not cperl-xemacs-p) ; not yet as of XEmacs 19.12 |
f83d2997 KH |
4661 | '( |
4662 | ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1 | |
4663 | (if (eq (char-after (match-beginning 2)) ?%) | |
5bd52f0e RS |
4664 | cperl-hash-face |
4665 | cperl-array-face) | |
f83d2997 KH |
4666 | t) ; arrays and hashes |
4667 | ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)" | |
4668 | 1 | |
5c8b7eaf | 4669 | (if (= (- (match-end 2) (match-beginning 2)) 1) |
f83d2997 | 4670 | (if (eq (char-after (match-beginning 3)) ?{) |
5bd52f0e RS |
4671 | cperl-hash-face |
4672 | cperl-array-face) ; arrays and hashes | |
f83d2997 KH |
4673 | font-lock-variable-name-face) ; Just to put something |
4674 | t) | |
4675 | ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2") | |
4676 | ;;; Too much noise from \s* @s[ and friends | |
5c8b7eaf | 4677 | ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)" |
f83d2997 KH |
4678 | ;;(3 font-lock-function-name-face t t) |
4679 | ;;(4 | |
4680 | ;; (if (cperl-slash-is-regexp) | |
4681 | ;; font-lock-function-name-face 'default) nil t)) | |
4682 | ))) | |
6c389151 SM |
4683 | (if cperl-highlight-variables-indiscriminately |
4684 | (setq t-font-lock-keywords-1 | |
4685 | (append t-font-lock-keywords-1 | |
4686 | (list '("[$*]{?\\(\\sw+\\)" 1 | |
4687 | font-lock-variable-name-face))))) | |
a1506d29 | 4688 | (setq cperl-font-lock-keywords-1 |
5bd52f0e RS |
4689 | (if cperl-syntaxify-by-font-lock |
4690 | (cons 'cperl-fontify-update | |
4691 | t-font-lock-keywords) | |
4692 | t-font-lock-keywords) | |
5efe6a56 SM |
4693 | cperl-font-lock-keywords cperl-font-lock-keywords-1 |
4694 | cperl-font-lock-keywords-2 (append | |
6c389151 SM |
4695 | cperl-font-lock-keywords-1 |
4696 | t-font-lock-keywords-1))) | |
f83d2997 KH |
4697 | (if (fboundp 'ps-print-buffer) (cperl-ps-print-init)) |
4698 | (if (or (featurep 'choose-color) (featurep 'font-lock-extra)) | |
db133cb6 | 4699 | (eval ; Avoid a warning |
83261a2f SM |
4700 | '(font-lock-require-faces |
4701 | (list | |
4702 | ;; Color-light Color-dark Gray-light Gray-dark Mono | |
4703 | (list 'font-lock-comment-face | |
4704 | ["Firebrick" "OrangeRed" "DimGray" "Gray80"] | |
4705 | nil | |
4706 | [nil nil t t t] | |
4707 | [nil nil t t t] | |
4708 | nil) | |
4709 | (list 'font-lock-string-face | |
4710 | ["RosyBrown" "LightSalmon" "Gray50" "LightGray"] | |
4711 | nil | |
4712 | nil | |
4713 | [nil nil t t t] | |
4714 | nil) | |
4715 | (list 'font-lock-function-name-face | |
4716 | (vector | |
4717 | "Blue" "LightSkyBlue" "Gray50" "LightGray" | |
4718 | (cdr (assq 'background-color ; if mono | |
4719 | (frame-parameters)))) | |
4720 | (vector | |
4721 | nil nil nil nil | |
4722 | (cdr (assq 'foreground-color ; if mono | |
4723 | (frame-parameters)))) | |
4724 | [nil nil t t t] | |
4725 | nil | |
4726 | nil) | |
4727 | (list 'font-lock-variable-name-face | |
4728 | ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"] | |
4729 | nil | |
4730 | [nil nil t t t] | |
4731 | [nil nil t t t] | |
4732 | nil) | |
4733 | (list 'font-lock-type-face | |
4734 | ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"] | |
4735 | nil | |
4736 | [nil nil t t t] | |
4737 | nil | |
4738 | [nil nil t t t]) | |
4739 | (list 'font-lock-constant-face | |
4740 | ["CadetBlue" "Aquamarine" "Gray50" "LightGray"] | |
4741 | nil | |
4742 | [nil nil t t t] | |
4743 | nil | |
4744 | [nil nil t t t]) | |
4745 | (list 'cperl-nonoverridable-face | |
4746 | ["chartreuse3" ("orchid1" "orange") | |
4747 | nil "Gray80"] | |
4748 | [nil nil "gray90"] | |
4749 | [nil nil nil t t] | |
4750 | [nil nil t t] | |
4751 | [nil nil t t t]) | |
4752 | (list 'cperl-array-face | |
4753 | ["blue" "yellow" nil "Gray80"] | |
4754 | ["lightyellow2" ("navy" "os2blue" "darkgreen") | |
4755 | "gray90"] | |
4756 | t | |
4757 | nil | |
4758 | nil) | |
4759 | (list 'cperl-hash-face | |
4760 | ["red" "red" nil "Gray80"] | |
4761 | ["lightyellow2" ("navy" "os2blue" "darkgreen") | |
4762 | "gray90"] | |
4763 | t | |
4764 | t | |
4765 | nil)))) | |
5bd52f0e | 4766 | ;; Do it the dull way, without choose-color |
f83d2997 KH |
4767 | (defvar cperl-guessed-background nil |
4768 | "Display characteristics as guessed by cperl.") | |
83261a2f SM |
4769 | ;; (or (fboundp 'x-color-defined-p) |
4770 | ;; (defalias 'x-color-defined-p | |
4771 | ;; (cond ((fboundp 'color-defined-p) 'color-defined-p) | |
4772 | ;; ;; XEmacs >= 19.12 | |
4773 | ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p) | |
4774 | ;; ;; XEmacs 19.11 | |
4775 | ;; (t 'x-valid-color-name-p)))) | |
5c8b7eaf | 4776 | (cperl-force-face font-lock-constant-face |
5bd52f0e RS |
4777 | "Face for constant and label names") |
4778 | (cperl-force-face font-lock-variable-name-face | |
4779 | "Face for variable names") | |
4780 | (cperl-force-face font-lock-type-face | |
4781 | "Face for data types") | |
4782 | (cperl-force-face cperl-nonoverridable-face | |
4783 | "Face for data types from another group") | |
4784 | (cperl-force-face font-lock-comment-face | |
4785 | "Face for comments") | |
4786 | (cperl-force-face font-lock-function-name-face | |
4787 | "Face for function names") | |
4788 | (cperl-force-face cperl-hash-face | |
4789 | "Face for hashes") | |
4790 | (cperl-force-face cperl-array-face | |
4791 | "Face for arrays") | |
4792 | ;;(defvar font-lock-constant-face 'font-lock-constant-face) | |
4793 | ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face) | |
4794 | ;;(or (boundp 'font-lock-type-face) | |
4795 | ;; (defconst font-lock-type-face | |
4796 | ;; 'font-lock-type-face | |
4797 | ;; "Face to use for data types.")) | |
4798 | ;;(or (boundp 'cperl-nonoverridable-face) | |
4799 | ;; (defconst cperl-nonoverridable-face | |
4800 | ;; 'cperl-nonoverridable-face | |
4801 | ;; "Face to use for data types from another group.")) | |
4802 | ;;(if (not cperl-xemacs-p) nil | |
4803 | ;; (or (boundp 'font-lock-comment-face) | |
4804 | ;; (defconst font-lock-comment-face | |
4805 | ;; 'font-lock-comment-face | |
4806 | ;; "Face to use for comments.")) | |
4807 | ;; (or (boundp 'font-lock-keyword-face) | |
4808 | ;; (defconst font-lock-keyword-face | |
4809 | ;; 'font-lock-keyword-face | |
4810 | ;; "Face to use for keywords.")) | |
4811 | ;; (or (boundp 'font-lock-function-name-face) | |
4812 | ;; (defconst font-lock-function-name-face | |
4813 | ;; 'font-lock-function-name-face | |
4814 | ;; "Face to use for function names."))) | |
4815 | (if (and | |
5c8b7eaf SS |
4816 | (not (cperl-is-face 'cperl-array-face)) |
4817 | (cperl-is-face 'font-lock-emphasized-face)) | |
5bd52f0e RS |
4818 | (copy-face 'font-lock-emphasized-face 'cperl-array-face)) |
4819 | (if (and | |
5c8b7eaf SS |
4820 | (not (cperl-is-face 'cperl-hash-face)) |
4821 | (cperl-is-face 'font-lock-other-emphasized-face)) | |
4822 | (copy-face 'font-lock-other-emphasized-face | |
5bd52f0e RS |
4823 | 'cperl-hash-face)) |
4824 | (if (and | |
5c8b7eaf SS |
4825 | (not (cperl-is-face 'cperl-nonoverridable-face)) |
4826 | (cperl-is-face 'font-lock-other-type-face)) | |
4827 | (copy-face 'font-lock-other-type-face | |
5bd52f0e RS |
4828 | 'cperl-nonoverridable-face)) |
4829 | ;;(or (boundp 'cperl-hash-face) | |
4830 | ;; (defconst cperl-hash-face | |
4831 | ;; 'cperl-hash-face | |
4832 | ;; "Face to use for hashes.")) | |
4833 | ;;(or (boundp 'cperl-array-face) | |
4834 | ;; (defconst cperl-array-face | |
4835 | ;; 'cperl-array-face | |
4836 | ;; "Face to use for arrays.")) | |
f83d2997 KH |
4837 | ;; Here we try to guess background |
4838 | (let ((background | |
4839 | (if (boundp 'font-lock-background-mode) | |
4840 | font-lock-background-mode | |
5c8b7eaf | 4841 | 'light)) |
83261a2f | 4842 | (face-list (and (fboundp 'face-list) (face-list)))) |
5bd52f0e RS |
4843 | ;;;; (fset 'cperl-is-face |
4844 | ;;;; (cond ((fboundp 'find-face) | |
4845 | ;;;; (symbol-function 'find-face)) | |
4846 | ;;;; (face-list | |
4847 | ;;;; (function (lambda (face) (member face face-list)))) | |
4848 | ;;;; (t | |
4849 | ;;;; (function (lambda (face) (boundp face)))))) | |
f83d2997 KH |
4850 | (defvar cperl-guessed-background |
4851 | (if (and (boundp 'font-lock-display-type) | |
4852 | (eq font-lock-display-type 'grayscale)) | |
4853 | 'gray | |
4854 | background) | |
4855 | "Background as guessed by CPerl mode") | |
83261a2f SM |
4856 | (and (not (cperl-is-face 'font-lock-constant-face)) |
4857 | (cperl-is-face 'font-lock-reference-face) | |
4858 | (copy-face 'font-lock-reference-face 'font-lock-constant-face)) | |
db133cb6 | 4859 | (if (cperl-is-face 'font-lock-type-face) nil |
f83d2997 KH |
4860 | (copy-face 'default 'font-lock-type-face) |
4861 | (cond | |
4862 | ((eq background 'light) | |
4863 | (set-face-foreground 'font-lock-type-face | |
4864 | (if (x-color-defined-p "seagreen") | |
4865 | "seagreen" | |
4866 | "sea green"))) | |
4867 | ((eq background 'dark) | |
4868 | (set-face-foreground 'font-lock-type-face | |
4869 | (if (x-color-defined-p "os2pink") | |
4870 | "os2pink" | |
4871 | "pink"))) | |
4872 | (t | |
4873 | (set-face-background 'font-lock-type-face "gray90")))) | |
5bd52f0e | 4874 | (if (cperl-is-face 'cperl-nonoverridable-face) |
f83d2997 | 4875 | nil |
5bd52f0e | 4876 | (copy-face 'font-lock-type-face 'cperl-nonoverridable-face) |
f83d2997 KH |
4877 | (cond |
4878 | ((eq background 'light) | |
5bd52f0e | 4879 | (set-face-foreground 'cperl-nonoverridable-face |
f83d2997 KH |
4880 | (if (x-color-defined-p "chartreuse3") |
4881 | "chartreuse3" | |
4882 | "chartreuse"))) | |
4883 | ((eq background 'dark) | |
5bd52f0e | 4884 | (set-face-foreground 'cperl-nonoverridable-face |
f83d2997 KH |
4885 | (if (x-color-defined-p "orchid1") |
4886 | "orchid1" | |
4887 | "orange"))))) | |
5bd52f0e RS |
4888 | ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil |
4889 | ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face) | |
4890 | ;;; (cond | |
4891 | ;;; ((eq background 'light) | |
4892 | ;;; (set-face-background 'font-lock-other-emphasized-face | |
4893 | ;;; (if (x-color-defined-p "lightyellow2") | |
4894 | ;;; "lightyellow2" | |
4895 | ;;; (if (x-color-defined-p "lightyellow") | |
4896 | ;;; "lightyellow" | |
4897 | ;;; "light yellow")))) | |
4898 | ;;; ((eq background 'dark) | |
4899 | ;;; (set-face-background 'font-lock-other-emphasized-face | |
4900 | ;;; (if (x-color-defined-p "navy") | |
4901 | ;;; "navy" | |
4902 | ;;; (if (x-color-defined-p "darkgreen") | |
4903 | ;;; "darkgreen" | |
4904 | ;;; "dark green")))) | |
4905 | ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90")))) | |
4906 | ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil | |
4907 | ;;; (copy-face 'bold 'font-lock-emphasized-face) | |
4908 | ;;; (cond | |
4909 | ;;; ((eq background 'light) | |
4910 | ;;; (set-face-background 'font-lock-emphasized-face | |
4911 | ;;; (if (x-color-defined-p "lightyellow2") | |
4912 | ;;; "lightyellow2" | |
4913 | ;;; "lightyellow"))) | |
4914 | ;;; ((eq background 'dark) | |
4915 | ;;; (set-face-background 'font-lock-emphasized-face | |
4916 | ;;; (if (x-color-defined-p "navy") | |
4917 | ;;; "navy" | |
4918 | ;;; (if (x-color-defined-p "darkgreen") | |
4919 | ;;; "darkgreen" | |
4920 | ;;; "dark green")))) | |
4921 | ;;; (t (set-face-background 'font-lock-emphasized-face "gray90")))) | |
db133cb6 | 4922 | (if (cperl-is-face 'font-lock-variable-name-face) nil |
f83d2997 | 4923 | (copy-face 'italic 'font-lock-variable-name-face)) |
db133cb6 | 4924 | (if (cperl-is-face 'font-lock-constant-face) nil |
883212ce | 4925 | (copy-face 'italic 'font-lock-constant-face)))) |
f83d2997 | 4926 | (setq cperl-faces-init t)) |
5bd52f0e | 4927 | (error (message "cperl-init-faces (ignored): %s" errs)))) |
f83d2997 KH |
4928 | |
4929 | ||
4930 | (defun cperl-ps-print-init () | |
4931 | "Initialization of `ps-print' components for faces used in CPerl." | |
5bd52f0e RS |
4932 | (eval-after-load "ps-print" |
4933 | '(setq ps-bold-faces | |
5c8b7eaf | 4934 | ;; font-lock-variable-name-face |
5bd52f0e RS |
4935 | ;; font-lock-constant-face |
4936 | (append '(cperl-array-face | |
5c8b7eaf | 4937 | cperl-hash-face) |
5bd52f0e RS |
4938 | ps-bold-faces) |
4939 | ps-italic-faces | |
4940 | ;; font-lock-constant-face | |
4941 | (append '(cperl-nonoverridable-face | |
4942 | cperl-hash-face) | |
4943 | ps-italic-faces) | |
4944 | ps-underlined-faces | |
4945 | ;; font-lock-type-face | |
4946 | (append '(cperl-array-face | |
4947 | cperl-hash-face | |
4948 | underline | |
4949 | cperl-nonoverridable-face) | |
4950 | ps-underlined-faces)))) | |
4951 | ||
4952 | (defvar ps-print-face-extension-alist) | |
4953 | ||
4954 | (defun cperl-ps-print (&optional file) | |
4955 | "Pretty-print in CPerl style. | |
4956 | If optional argument FILE is an empty string, prints to printer, otherwise | |
4957 | to the file FILE. If FILE is nil, prompts for a file name. | |
4958 | ||
4959 | Style of printout regulated by the variable `cperl-ps-print-face-properties'." | |
4960 | (interactive) | |
5c8b7eaf SS |
4961 | (or file |
4962 | (setq file (read-from-minibuffer | |
5bd52f0e RS |
4963 | "Print to file (if empty - to printer): " |
4964 | (concat (buffer-file-name) ".ps") | |
4965 | nil nil 'file-name-history))) | |
4966 | (or (> (length file) 0) | |
4967 | (setq file nil)) | |
4968 | (require 'ps-print) ; To get ps-print-face-extension-alist | |
4969 | (let ((ps-print-color-p t) | |
4970 | (ps-print-face-extension-alist ps-print-face-extension-alist)) | |
4971 | (cperl-ps-extend-face-list cperl-ps-print-face-properties) | |
4972 | (ps-print-buffer-with-faces file))) | |
4973 | ||
4974 | ;;; (defun cperl-ps-print-init () | |
4975 | ;;; "Initialization of `ps-print' components for faces used in CPerl." | |
4976 | ;;; ;; Guard against old versions | |
4977 | ;;; (defvar ps-underlined-faces nil) | |
4978 | ;;; (defvar ps-bold-faces nil) | |
4979 | ;;; (defvar ps-italic-faces nil) | |
4980 | ;;; (setq ps-bold-faces | |
4981 | ;;; (append '(font-lock-emphasized-face | |
4982 | ;;; cperl-array-face | |
5c8b7eaf SS |
4983 | ;;; font-lock-keyword-face |
4984 | ;;; font-lock-variable-name-face | |
4985 | ;;; font-lock-constant-face | |
4986 | ;;; font-lock-reference-face | |
5bd52f0e | 4987 | ;;; font-lock-other-emphasized-face |
5c8b7eaf | 4988 | ;;; cperl-hash-face) |
5bd52f0e RS |
4989 | ;;; ps-bold-faces)) |
4990 | ;;; (setq ps-italic-faces | |
4991 | ;;; (append '(cperl-nonoverridable-face | |
5c8b7eaf SS |
4992 | ;;; font-lock-constant-face |
4993 | ;;; font-lock-reference-face | |
5bd52f0e RS |
4994 | ;;; font-lock-other-emphasized-face |
4995 | ;;; cperl-hash-face) | |
4996 | ;;; ps-italic-faces)) | |
4997 | ;;; (setq ps-underlined-faces | |
4998 | ;;; (append '(font-lock-emphasized-face | |
4999 | ;;; cperl-array-face | |
5000 | ;;; font-lock-other-emphasized-face | |
5001 | ;;; cperl-hash-face | |
5002 | ;;; cperl-nonoverridable-face font-lock-type-face) | |
5003 | ;;; ps-underlined-faces)) | |
5004 | ;;; (cons 'font-lock-type-face ps-underlined-faces)) | |
f83d2997 KH |
5005 | |
5006 | ||
5007 | (if (cperl-enable-font-lock) (cperl-windowed-init)) | |
5008 | ||
db133cb6 | 5009 | (defconst cperl-styles-entries |
5c8b7eaf SS |
5010 | '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset |
5011 | cperl-label-offset cperl-extra-newline-before-brace | |
bab27c0c | 5012 | cperl-merge-trailing-else |
db133cb6 RS |
5013 | cperl-continued-statement-offset)) |
5014 | ||
5015 | (defconst cperl-style-alist | |
83261a2f | 5016 | '(("CPerl" ; =GNU without extra-newline-before-brace |
db133cb6 RS |
5017 | (cperl-indent-level . 2) |
5018 | (cperl-brace-offset . 0) | |
5019 | (cperl-continued-brace-offset . 0) | |
5020 | (cperl-label-offset . -2) | |
5021 | (cperl-extra-newline-before-brace . nil) | |
bab27c0c | 5022 | (cperl-merge-trailing-else . t) |
db133cb6 | 5023 | (cperl-continued-statement-offset . 2)) |
83261a2f | 5024 | ("PerlStyle" ; CPerl with 4 as indent |
db133cb6 RS |
5025 | (cperl-indent-level . 4) |
5026 | (cperl-brace-offset . 0) | |
5027 | (cperl-continued-brace-offset . 0) | |
5028 | (cperl-label-offset . -4) | |
5029 | (cperl-extra-newline-before-brace . nil) | |
bab27c0c | 5030 | (cperl-merge-trailing-else . t) |
db133cb6 RS |
5031 | (cperl-continued-statement-offset . 4)) |
5032 | ("GNU" | |
5033 | (cperl-indent-level . 2) | |
5034 | (cperl-brace-offset . 0) | |
5035 | (cperl-continued-brace-offset . 0) | |
5036 | (cperl-label-offset . -2) | |
5037 | (cperl-extra-newline-before-brace . t) | |
bab27c0c | 5038 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5039 | (cperl-continued-statement-offset . 2)) |
5040 | ("K&R" | |
5041 | (cperl-indent-level . 5) | |
5042 | (cperl-brace-offset . 0) | |
5043 | (cperl-continued-brace-offset . -5) | |
5044 | (cperl-label-offset . -5) | |
5045 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
bab27c0c | 5046 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5047 | (cperl-continued-statement-offset . 5)) |
5048 | ("BSD" | |
5049 | (cperl-indent-level . 4) | |
5050 | (cperl-brace-offset . 0) | |
5051 | (cperl-continued-brace-offset . -4) | |
5052 | (cperl-label-offset . -4) | |
5053 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
5054 | (cperl-continued-statement-offset . 4)) | |
5055 | ("C++" | |
5056 | (cperl-indent-level . 4) | |
5057 | (cperl-brace-offset . 0) | |
5058 | (cperl-continued-brace-offset . -4) | |
5059 | (cperl-label-offset . -4) | |
5060 | (cperl-continued-statement-offset . 4) | |
bab27c0c | 5061 | (cperl-merge-trailing-else . nil) |
db133cb6 RS |
5062 | (cperl-extra-newline-before-brace . t)) |
5063 | ("Current") | |
5064 | ("Whitesmith" | |
5065 | (cperl-indent-level . 4) | |
5066 | (cperl-brace-offset . 0) | |
5067 | (cperl-continued-brace-offset . 0) | |
5068 | (cperl-label-offset . -4) | |
5069 | ;;(cperl-extra-newline-before-brace . nil) ; ??? | |
5070 | (cperl-continued-statement-offset . 4))) | |
5071 | "(Experimental) list of variables to set to get a particular indentation style. | |
5bd52f0e | 5072 | Should be used via `cperl-set-style' or via Perl menu.") |
db133cb6 | 5073 | |
f83d2997 | 5074 | (defun cperl-set-style (style) |
f94a632a | 5075 | "Set CPerl mode variables to use one of several different indentation styles. |
f83d2997 | 5076 | The arguments are a string representing the desired style. |
5c8b7eaf | 5077 | The list of styles is in `cperl-style-alist', available styles |
db133cb6 RS |
5078 | are GNU, K&R, BSD, C++ and Whitesmith. |
5079 | ||
5080 | The current value of style is memorized (unless there is a memorized | |
5081 | data already), may be restored by `cperl-set-style-back'. | |
5082 | ||
5083 | Chosing \"Current\" style will not change style, so this may be used for | |
5084 | side-effect of memorizing only." | |
5c8b7eaf | 5085 | (interactive |
83261a2f | 5086 | (let ((list (mapcar (function (lambda (elt) (list (car elt)))) |
db133cb6 | 5087 | cperl-style-alist))) |
f83d2997 | 5088 | (list (completing-read "Enter style: " list nil 'insist)))) |
db133cb6 RS |
5089 | (or cperl-old-style |
5090 | (setq cperl-old-style | |
5091 | (mapcar (function | |
5092 | (lambda (name) | |
5093 | (cons name (eval name)))) | |
5094 | cperl-styles-entries))) | |
5095 | (let ((style (cdr (assoc style cperl-style-alist))) setting str sym) | |
f83d2997 KH |
5096 | (while style |
5097 | (setq setting (car style) style (cdr style)) | |
db133cb6 RS |
5098 | (set (car setting) (cdr setting))))) |
5099 | ||
5100 | (defun cperl-set-style-back () | |
5101 | "Restore a style memorised by `cperl-set-style'." | |
5102 | (interactive) | |
5103 | (or cperl-old-style (error "The style was not changed")) | |
5104 | (let (setting) | |
5105 | (while cperl-old-style | |
5c8b7eaf | 5106 | (setq setting (car cperl-old-style) |
db133cb6 RS |
5107 | cperl-old-style (cdr cperl-old-style)) |
5108 | (set (car setting) (cdr setting))))) | |
f83d2997 KH |
5109 | |
5110 | (defun cperl-check-syntax () | |
5111 | (interactive) | |
5112 | (require 'mode-compile) | |
db133cb6 RS |
5113 | (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc"))) |
5114 | (eval '(mode-compile)))) ; Avoid a warning | |
f83d2997 KH |
5115 | |
5116 | (defun cperl-info-buffer (type) | |
5117 | ;; Returns buffer with documentation. Creates if missing. | |
5118 | ;; If TYPE, this vars buffer. | |
5119 | ;; Special care is taken to not stomp over an existing info buffer | |
5120 | (let* ((bname (if type "*info-perl-var*" "*info-perl*")) | |
5121 | (info (get-buffer bname)) | |
5122 | (oldbuf (get-buffer "*info*"))) | |
5123 | (if info info | |
5124 | (save-window-excursion | |
5125 | ;; Get Info running | |
5126 | (require 'info) | |
5127 | (cond (oldbuf | |
5128 | (set-buffer oldbuf) | |
5129 | (rename-buffer "*info-perl-tmp*"))) | |
5130 | (save-window-excursion | |
5131 | (info)) | |
5132 | (Info-find-node cperl-info-page (if type "perlvar" "perlfunc")) | |
5133 | (set-buffer "*info*") | |
5134 | (rename-buffer bname) | |
5135 | (cond (oldbuf | |
5136 | (set-buffer "*info-perl-tmp*") | |
5137 | (rename-buffer "*info*") | |
5138 | (set-buffer bname))) | |
029cb4d5 | 5139 | (make-local-variable 'window-min-height) |
f83d2997 KH |
5140 | (setq window-min-height 2) |
5141 | (current-buffer))))) | |
5142 | ||
5143 | (defun cperl-word-at-point (&optional p) | |
f94a632a | 5144 | "Return the word at point or at P." |
f83d2997 KH |
5145 | (save-excursion |
5146 | (if p (goto-char p)) | |
5147 | (or (cperl-word-at-point-hard) | |
5148 | (progn | |
5149 | (require 'etags) | |
5150 | (funcall (or (and (boundp 'find-tag-default-function) | |
5151 | find-tag-default-function) | |
5152 | (get major-mode 'find-tag-default-function) | |
5153 | ;; XEmacs 19.12 has `find-tag-default-hook'; it is | |
5154 | ;; automatically used within `find-tag-default': | |
5155 | 'find-tag-default)))))) | |
5156 | ||
5157 | (defun cperl-info-on-command (command) | |
f94a632a | 5158 | "Show documentation for Perl command COMMAND in other window. |
f83d2997 KH |
5159 | If perl-info buffer is shown in some frame, uses this frame. |
5160 | Customized by setting variables `cperl-shrink-wrap-info-frame', | |
5161 | `cperl-max-help-size'." | |
5c8b7eaf | 5162 | (interactive |
f83d2997 | 5163 | (let* ((default (cperl-word-at-point)) |
5c8b7eaf | 5164 | (read (read-string |
83261a2f SM |
5165 | (format "Find doc for Perl function (default %s): " |
5166 | default)))) | |
5c8b7eaf | 5167 | (list (if (equal read "") |
83261a2f SM |
5168 | default |
5169 | read)))) | |
f83d2997 KH |
5170 | |
5171 | (let ((buffer (current-buffer)) | |
5172 | (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///" | |
5173 | pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner | |
5174 | max-height char-height buf-list) | |
5175 | (if (string-match "^-[a-zA-Z]$" command) | |
5176 | (setq cmd-desc "^-X[ \t\n]")) | |
5177 | (setq isvar (string-match "^[$@%]" command) | |
5178 | buf (cperl-info-buffer isvar) | |
5179 | iniwin (selected-window) | |
5180 | fr1 (window-frame iniwin)) | |
5181 | (set-buffer buf) | |
5182 | (beginning-of-buffer) | |
5c8b7eaf | 5183 | (or isvar |
f83d2997 KH |
5184 | (progn (re-search-forward "^-X[ \t\n]") |
5185 | (forward-line -1))) | |
5186 | (if (re-search-forward cmd-desc nil t) | |
5187 | (progn | |
5188 | ;; Go back to beginning of the group (ex, for qq) | |
5189 | (if (re-search-backward "^[ \t\n\f]") | |
5190 | (forward-line 1)) | |
5191 | (beginning-of-line) | |
5c8b7eaf | 5192 | ;; Get some of |
f83d2997 KH |
5193 | (setq pos (point) |
5194 | buf-list (list buf "*info-perl-var*" "*info-perl*")) | |
5195 | (while (and (not win) buf-list) | |
5196 | (setq win (get-buffer-window (car buf-list) t)) | |
5197 | (setq buf-list (cdr buf-list))) | |
5198 | (or (not win) | |
5199 | (eq (window-buffer win) buf) | |
5200 | (set-window-buffer win buf)) | |
5201 | (and win (setq fr2 (window-frame win))) | |
5202 | (if (or (not fr2) (eq fr1 fr2)) | |
5203 | (pop-to-buffer buf) | |
5204 | (special-display-popup-frame buf) ; Make it visible | |
5205 | (select-window win)) | |
5206 | (goto-char pos) ; Needed (?!). | |
5207 | ;; Resize | |
5208 | (setq iniheight (window-height) | |
5209 | frheight (frame-height) | |
5210 | not-loner (< iniheight (1- frheight))) ; Are not alone | |
5c8b7eaf | 5211 | (cond ((if not-loner cperl-max-help-size |
f83d2997 | 5212 | cperl-shrink-wrap-info-frame) |
5c8b7eaf SS |
5213 | (setq height |
5214 | (+ 2 | |
5215 | (count-lines | |
5216 | pos | |
f83d2997 KH |
5217 | (save-excursion |
5218 | (if (re-search-forward | |
5219 | "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t) | |
5220 | (match-beginning 0) (point-max))))) | |
5c8b7eaf | 5221 | max-height |
f83d2997 KH |
5222 | (if not-loner |
5223 | (/ (* (- frheight 3) cperl-max-help-size) 100) | |
5224 | (setq char-height (frame-char-height)) | |
5225 | ;; Non-functioning under OS/2: | |
5226 | (if (eq char-height 1) (setq char-height 18)) | |
5227 | ;; Title, menubar, + 2 for slack | |
83261a2f | 5228 | (- (/ (x-display-pixel-height) char-height) 4))) |
f83d2997 KH |
5229 | (if (> height max-height) (setq height max-height)) |
5230 | ;;(message "was %s doing %s" iniheight height) | |
5231 | (if not-loner | |
5232 | (enlarge-window (- height iniheight)) | |
5233 | (set-frame-height (window-frame win) (1+ height))))) | |
5234 | (set-window-start (selected-window) pos)) | |
5235 | (message "No entry for %s found." command)) | |
5236 | ;;(pop-to-buffer buffer) | |
5237 | (select-window iniwin))) | |
5238 | ||
5239 | (defun cperl-info-on-current-command () | |
029cb4d5 | 5240 | "Show documentation for Perl command at point in other window." |
f83d2997 KH |
5241 | (interactive) |
5242 | (cperl-info-on-command (cperl-word-at-point))) | |
5243 | ||
5244 | (defun cperl-imenu-info-imenu-search () | |
5245 | (if (looking-at "^-X[ \t\n]") nil | |
5246 | (re-search-backward | |
5247 | "^\n\\([-a-zA-Z_]+\\)[ \t\n]") | |
5248 | (forward-line 1))) | |
5249 | ||
5c8b7eaf | 5250 | (defun cperl-imenu-info-imenu-name () |
f83d2997 KH |
5251 | (buffer-substring |
5252 | (match-beginning 1) (match-end 1))) | |
5253 | ||
5254 | (defun cperl-imenu-on-info () | |
5255 | (interactive) | |
5256 | (let* ((buffer (current-buffer)) | |
5257 | imenu-create-index-function | |
5c8b7eaf SS |
5258 | imenu-prev-index-position-function |
5259 | imenu-extract-index-name-function | |
f83d2997 KH |
5260 | (index-item (save-restriction |
5261 | (save-window-excursion | |
5262 | (set-buffer (cperl-info-buffer nil)) | |
5c8b7eaf | 5263 | (setq imenu-create-index-function |
f83d2997 KH |
5264 | 'imenu-default-create-index-function |
5265 | imenu-prev-index-position-function | |
5266 | 'cperl-imenu-info-imenu-search | |
5267 | imenu-extract-index-name-function | |
5268 | 'cperl-imenu-info-imenu-name) | |
5269 | (imenu-choose-buffer-index))))) | |
5270 | (and index-item | |
5271 | (progn | |
5272 | (push-mark) | |
5273 | (pop-to-buffer "*info-perl*") | |
5274 | (cond | |
5275 | ((markerp (cdr index-item)) | |
5276 | (goto-char (marker-position (cdr index-item)))) | |
5277 | (t | |
5278 | (goto-char (cdr index-item)))) | |
5279 | (set-window-start (selected-window) (point)) | |
5280 | (pop-to-buffer buffer))))) | |
5281 | ||
5282 | (defun cperl-lineup (beg end &optional step minshift) | |
5283 | "Lineup construction in a region. | |
5284 | Beginning of region should be at the start of a construction. | |
5285 | All first occurrences of this construction in the lines that are | |
5286 | partially contained in the region are lined up at the same column. | |
5287 | ||
5288 | MINSHIFT is the minimal amount of space to insert before the construction. | |
5289 | STEP is the tabwidth to position constructions. | |
029cb4d5 | 5290 | If STEP is nil, `cperl-lineup-step' will be used |
f83d2997 KH |
5291 | \(or `cperl-indent-level', if `cperl-lineup-step' is `nil'). |
5292 | Will not move the position at the start to the left." | |
5293 | (interactive "r") | |
5294 | (let (search col tcol seen b e) | |
5295 | (save-excursion | |
5296 | (goto-char end) | |
5297 | (end-of-line) | |
5298 | (setq end (point-marker)) | |
5299 | (goto-char beg) | |
5300 | (skip-chars-forward " \t\f") | |
5301 | (setq beg (point-marker)) | |
5302 | (indent-region beg end nil) | |
5303 | (goto-char beg) | |
5304 | (setq col (current-column)) | |
5305 | (if (looking-at "[a-zA-Z0-9_]") | |
5306 | (if (looking-at "\\<[a-zA-Z0-9_]+\\>") | |
5307 | (setq search | |
5c8b7eaf SS |
5308 | (concat "\\<" |
5309 | (regexp-quote | |
f83d2997 KH |
5310 | (buffer-substring (match-beginning 0) |
5311 | (match-end 0))) "\\>")) | |
5312 | (error "Cannot line up in a middle of the word")) | |
5313 | (if (looking-at "$") | |
5314 | (error "Cannot line up end of line")) | |
5315 | (setq search (regexp-quote (char-to-string (following-char))))) | |
5316 | (setq step (or step cperl-lineup-step cperl-indent-level)) | |
5317 | (or minshift (setq minshift 1)) | |
5318 | (while (progn | |
5319 | (beginning-of-line 2) | |
5c8b7eaf | 5320 | (and (< (point) end) |
f83d2997 KH |
5321 | (re-search-forward search end t) |
5322 | (goto-char (match-beginning 0)))) | |
5323 | (setq tcol (current-column) seen t) | |
5324 | (if (> tcol col) (setq col tcol))) | |
5325 | (or seen | |
5326 | (error "The construction to line up occurred only once")) | |
5327 | (goto-char beg) | |
5328 | (setq col (+ col minshift)) | |
5329 | (if (/= (% col step) 0) (setq step (* step (1+ (/ col step))))) | |
5c8b7eaf | 5330 | (while |
f83d2997 KH |
5331 | (progn |
5332 | (setq e (point)) | |
5333 | (skip-chars-backward " \t") | |
5334 | (delete-region (point) e) | |
83261a2f | 5335 | (indent-to-column col) ;(make-string (- col (current-column)) ?\ )) |
5c8b7eaf SS |
5336 | (beginning-of-line 2) |
5337 | (and (< (point) end) | |
f83d2997 KH |
5338 | (re-search-forward search end t) |
5339 | (goto-char (match-beginning 0)))))))) ; No body | |
5340 | ||
5341 | (defun cperl-etags (&optional add all files) | |
5342 | "Run etags with appropriate options for Perl files. | |
5343 | If optional argument ALL is `recursive', will process Perl files | |
5344 | in subdirectories too." | |
5345 | (interactive) | |
5346 | (let ((cmd "etags") | |
5347 | (args '("-l" "none" "-r" "/\\<\\(package\\|sub\\)[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([{#]\\|$\\)\\)/\\4/")) | |
5348 | res) | |
5349 | (if add (setq args (cons "-a" args))) | |
5350 | (or files (setq files (list buffer-file-name))) | |
5351 | (cond | |
5352 | ((eq all 'recursive) | |
5353 | ;;(error "Not implemented: recursive") | |
5c8b7eaf | 5354 | (setq args (append (list "-e" |
f83d2997 KH |
5355 | "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/} |
5356 | use File::Find; | |
5357 | find(\\&wanted, '.'); | |
5c8b7eaf | 5358 | exec @ARGV;" |
f83d2997 KH |
5359 | cmd) args) |
5360 | cmd "perl")) | |
5c8b7eaf | 5361 | (all |
f83d2997 | 5362 | ;;(error "Not implemented: all") |
5c8b7eaf | 5363 | (setq args (append (list "-e" |
f83d2997 | 5364 | "push @ARGV, <*.PL *.pl *.pm>; |
5c8b7eaf | 5365 | exec @ARGV;" |
f83d2997 KH |
5366 | cmd) args) |
5367 | cmd "perl")) | |
5368 | (t | |
5369 | (setq args (append args files)))) | |
5370 | (setq res (apply 'call-process cmd nil nil nil args)) | |
5371 | (or (eq res 0) | |
5372 | (message "etags returned \"%s\"" res)))) | |
5373 | ||
5374 | (defun cperl-toggle-auto-newline () | |
5375 | "Toggle the state of `cperl-auto-newline'." | |
5376 | (interactive) | |
5377 | (setq cperl-auto-newline (not cperl-auto-newline)) | |
5c8b7eaf | 5378 | (message "Newlines will %sbe auto-inserted now." |
f83d2997 KH |
5379 | (if cperl-auto-newline "" "not "))) |
5380 | ||
5381 | (defun cperl-toggle-abbrev () | |
5382 | "Toggle the state of automatic keyword expansion in CPerl mode." | |
5383 | (interactive) | |
5384 | (abbrev-mode (if abbrev-mode 0 1)) | |
5c8b7eaf | 5385 | (message "Perl control structure will %sbe auto-inserted now." |
f83d2997 KH |
5386 | (if abbrev-mode "" "not "))) |
5387 | ||
5388 | ||
5389 | (defun cperl-toggle-electric () | |
5390 | "Toggle the state of parentheses doubling in CPerl mode." | |
5391 | (interactive) | |
5392 | (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t)) | |
5c8b7eaf | 5393 | (message "Parentheses will %sbe auto-doubled now." |
f83d2997 KH |
5394 | (if (cperl-val 'cperl-electric-parens) "" "not "))) |
5395 | ||
db133cb6 RS |
5396 | (defun cperl-toggle-autohelp () |
5397 | "Toggle the state of automatic help message in CPerl mode. | |
5398 | See `cperl-lazy-help-time' too." | |
5399 | (interactive) | |
5400 | (if (fboundp 'run-with-idle-timer) | |
5401 | (progn | |
5402 | (if cperl-lazy-installed | |
5403 | (eval '(cperl-lazy-unstall)) | |
5404 | (cperl-lazy-install)) | |
5c8b7eaf | 5405 | (message "Perl help messages will %sbe automatically shown now." |
db133cb6 RS |
5406 | (if cperl-lazy-installed "" "not "))) |
5407 | (message "Cannot automatically show Perl help messages - run-with-idle-timer missing."))) | |
5408 | ||
5409 | (defun cperl-toggle-construct-fix () | |
5410 | "Toggle whether `indent-region'/`indent-sexp' fix whitespace too." | |
5411 | (interactive) | |
5c8b7eaf | 5412 | (setq cperl-indent-region-fix-constructs |
5bd52f0e RS |
5413 | (if cperl-indent-region-fix-constructs |
5414 | nil | |
5415 | 1)) | |
5c8b7eaf | 5416 | (message "indent-region/indent-sexp will %sbe automatically fix whitespace." |
db133cb6 RS |
5417 | (if cperl-indent-region-fix-constructs "" "not "))) |
5418 | ||
f83d2997 KH |
5419 | ;;;; Tags file creation. |
5420 | ||
5421 | (defvar cperl-tmp-buffer " *cperl-tmp*") | |
5422 | ||
5423 | (defun cperl-setup-tmp-buf () | |
5424 | (set-buffer (get-buffer-create cperl-tmp-buffer)) | |
5425 | (set-syntax-table cperl-mode-syntax-table) | |
5426 | (buffer-disable-undo) | |
5427 | (auto-fill-mode 0) | |
5428 | (if cperl-use-syntax-table-text-property-for-tags | |
5429 | (progn | |
029cb4d5 | 5430 | (make-local-variable 'parse-sexp-lookup-properties) |
f83d2997 KH |
5431 | ;; Do not introduce variable if not needed, we check it! |
5432 | (set 'parse-sexp-lookup-properties t)))) | |
5433 | ||
5434 | (defun cperl-xsub-scan () | |
f83d2997 | 5435 | (require 'imenu) |
5c8b7eaf | 5436 | (let ((index-alist '()) |
f83d2997 KH |
5437 | (prev-pos 0) index index1 name package prefix) |
5438 | (goto-char (point-min)) | |
5439 | (if noninteractive | |
5440 | (message "Scanning XSUB for index") | |
5441 | (imenu-progress-message prev-pos 0)) | |
5442 | ;; Search for the function | |
5443 | (progn ;;save-match-data | |
5444 | (while (re-search-forward | |
5445 | "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)" | |
5446 | nil t) | |
5447 | (or noninteractive | |
5448 | (imenu-progress-message prev-pos)) | |
5449 | (cond | |
83261a2f | 5450 | ((match-beginning 2) ; SECTION |
f83d2997 KH |
5451 | (setq package (buffer-substring (match-beginning 2) (match-end 2))) |
5452 | (goto-char (match-beginning 0)) | |
5453 | (skip-chars-forward " \t") | |
5454 | (forward-char 1) | |
5455 | (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>") | |
5456 | (setq prefix (buffer-substring (match-beginning 1) (match-end 1))) | |
5457 | (setq prefix nil))) | |
5458 | ((not package) nil) ; C language section | |
5459 | ((match-beginning 3) ; XSUB | |
5460 | (goto-char (1+ (match-beginning 3))) | |
5461 | (setq index (imenu-example--name-and-position)) | |
5462 | (setq name (buffer-substring (match-beginning 3) (match-end 3))) | |
5463 | (if (and prefix (string-match (concat "^" prefix) name)) | |
5464 | (setq name (substring name (length prefix)))) | |
5465 | (cond ((string-match "::" name) nil) | |
5466 | (t | |
5467 | (setq index1 (cons (concat package "::" name) (cdr index))) | |
5468 | (push index1 index-alist))) | |
5469 | (setcar index name) | |
5470 | (push index index-alist)) | |
5471 | (t ; BOOT: section | |
5472 | ;; (beginning-of-line) | |
5473 | (setq index (imenu-example--name-and-position)) | |
5474 | (setcar index (concat package "::BOOT:")) | |
5475 | (push index index-alist))))) | |
5476 | (or noninteractive | |
5477 | (imenu-progress-message prev-pos 100)) | |
f83d2997 KH |
5478 | index-alist)) |
5479 | ||
6c389151 SM |
5480 | (defvar cperl-unreadable-ok nil) |
5481 | ||
5482 | (defun cperl-find-tags (ifile xs topdir) | |
83261a2f SM |
5483 | (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel |
5484 | (cperl-pod-here-fontify nil) f file) | |
f83d2997 KH |
5485 | (save-excursion |
5486 | (if b (set-buffer b) | |
83261a2f | 5487 | (cperl-setup-tmp-buf)) |
f83d2997 | 5488 | (erase-buffer) |
6c389151 SM |
5489 | (condition-case err |
5490 | (setq file (car (insert-file-contents ifile))) | |
5491 | (error (if cperl-unreadable-ok nil | |
5492 | (if (y-or-n-p | |
5493 | (format "File %s unreadable. Continue? " ifile)) | |
5494 | (setq cperl-unreadable-ok t) | |
5495 | (error "Aborting: unreadable file %s" ifile))))) | |
a1506d29 | 5496 | (if (not file) |
6c389151 | 5497 | (message "Unreadable file %s" ifile) |
83261a2f SM |
5498 | (message "Scanning file %s ..." file) |
5499 | (if (and cperl-use-syntax-table-text-property-for-tags | |
5500 | (not xs)) | |
5501 | (condition-case err ; after __END__ may have garbage | |
5502 | (cperl-find-pods-heres nil nil noninteractive) | |
5503 | (error (message "While scanning for syntax: %s" err)))) | |
5504 | (if xs | |
5505 | (setq lst (cperl-xsub-scan)) | |
5506 | (setq ind (cperl-imenu--create-perl-index)) | |
5507 | (setq lst (cdr (assoc "+Unsorted List+..." ind)))) | |
5508 | (setq lst | |
5509 | (mapcar | |
5510 | (function | |
5511 | (lambda (elt) | |
5512 | (cond ((string-match "^[_a-zA-Z]" (car elt)) | |
5513 | (goto-char (cdr elt)) | |
5514 | (beginning-of-line) ; pos should be of the start of the line | |
5515 | (list (car elt) | |
5516 | (point) | |
5517 | (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l | |
5518 | (buffer-substring (progn | |
5519 | (goto-char (cdr elt)) | |
5520 | ;; After name now... | |
5521 | (or (eolp) (forward-char 1)) | |
5522 | (point)) | |
5523 | (progn | |
5524 | (beginning-of-line) | |
5525 | (point)))))))) | |
5526 | lst)) | |
5527 | (erase-buffer) | |
5528 | (while lst | |
5529 | (setq elt (car lst) lst (cdr lst)) | |
5530 | (if elt | |
5531 | (progn | |
5532 | (insert (elt elt 3) | |
5533 | 127 | |
5534 | (if (string-match "^package " (car elt)) | |
5535 | (substring (car elt) 8) | |
5536 | (car elt) ) | |
5537 | 1 | |
5538 | (number-to-string (elt elt 2)) ; Line | |
5539 | "," | |
5540 | (number-to-string (1- (elt elt 1))) ; Char pos 0-based | |
5541 | "\n") | |
5542 | (if (and (string-match "^[_a-zA-Z]+::" (car elt)) | |
5543 | (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]" | |
5544 | (elt elt 3))) | |
5545 | ;; Need to insert the name without package as well | |
5546 | (setq lst (cons (cons (substring (elt elt 3) | |
5547 | (match-beginning 1) | |
5548 | (match-end 1)) | |
5549 | (cdr elt)) | |
5550 | lst)))))) | |
5551 | (setq pos (point)) | |
5552 | (goto-char 1) | |
5553 | (setq rel file) | |
5554 | ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties | |
5555 | (set-text-properties 0 (length rel) nil rel) | |
5556 | (and (equal topdir (substring rel 0 (length topdir))) | |
5557 | (setq rel (substring file (length topdir)))) | |
5558 | (insert "\f\n" rel "," (number-to-string (1- pos)) "\n") | |
5559 | (setq ret (buffer-substring 1 (point-max))) | |
5560 | (erase-buffer) | |
5561 | (or noninteractive | |
5562 | (message "Scanning file %s finished" file)) | |
5563 | ret)))) | |
f83d2997 KH |
5564 | |
5565 | (defun cperl-add-tags-recurse-noxs () | |
5566 | "Add to TAGS data for Perl and XSUB files in the current directory and kids. | |
5567 | Use as | |
5568 | emacs -batch -q -no-site-file -l emacs/cperl-mode.el \ | |
5c8b7eaf | 5569 | -f cperl-add-tags-recurse |
f83d2997 KH |
5570 | " |
5571 | (cperl-write-tags nil nil t t nil t)) | |
5572 | ||
5573 | (defun cperl-add-tags-recurse () | |
5574 | "Add to TAGS file data for Perl files in the current directory and kids. | |
5575 | Use as | |
5576 | emacs -batch -q -no-site-file -l emacs/cperl-mode.el \ | |
5c8b7eaf | 5577 | -f cperl-add-tags-recurse |
f83d2997 KH |
5578 | " |
5579 | (cperl-write-tags nil nil t t)) | |
5580 | ||
5581 | (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir) | |
5582 | ;; If INBUFFER, do not select buffer, and do not save | |
5583 | ;; If ERASE is `ignore', do not erase, and do not try to delete old info. | |
5584 | (require 'etags) | |
5585 | (if file nil | |
5586 | (setq file (if dir default-directory (buffer-file-name))) | |
5587 | (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!"))) | |
5588 | (or topdir | |
5589 | (setq topdir default-directory)) | |
5590 | (let ((tags-file-name "TAGS") | |
5591 | (case-fold-search (eq system-type 'emx)) | |
6c389151 | 5592 | xs rel tm) |
f83d2997 KH |
5593 | (save-excursion |
5594 | (cond (inbuffer nil) ; Already there | |
5595 | ((file-exists-p tags-file-name) | |
5bd52f0e RS |
5596 | (if cperl-xemacs-p |
5597 | (visit-tags-table-buffer) | |
83261a2f | 5598 | (visit-tags-table-buffer tags-file-name))) |
f83d2997 KH |
5599 | (t (set-buffer (find-file-noselect tags-file-name)))) |
5600 | (cond | |
5601 | (dir | |
5602 | (cond ((eq erase 'ignore)) | |
5603 | (erase | |
5604 | (erase-buffer) | |
5605 | (setq erase 'ignore))) | |
a1506d29 | 5606 | (let ((files |
6c389151 | 5607 | (condition-case err |
a1506d29 | 5608 | (directory-files file t |
6c389151 SM |
5609 | (if recurse nil cperl-scan-files-regexp) |
5610 | t) | |
5611 | (error | |
5612 | (if cperl-unreadable-ok nil | |
5613 | (if (y-or-n-p | |
5614 | (format "Directory %s unreadable. Continue? " file)) | |
a1506d29 | 5615 | (setq cperl-unreadable-ok t |
83261a2f | 5616 | tm nil) ; Return empty list |
6c389151 | 5617 | (error "Aborting: unreadable directory %s" file))))))) |
83261a2f SM |
5618 | (mapcar (function |
5619 | (lambda (file) | |
5620 | (cond | |
5621 | ((string-match cperl-noscan-files-regexp file) | |
5622 | nil) | |
5623 | ((not (file-directory-p file)) | |
5624 | (if (string-match cperl-scan-files-regexp file) | |
5625 | (cperl-write-tags file erase recurse nil t noxs topdir))) | |
5626 | ((not recurse) nil) | |
5627 | (t (cperl-write-tags file erase recurse t t noxs topdir))))) | |
5628 | files))) | |
f83d2997 KH |
5629 | (t |
5630 | (setq xs (string-match "\\.xs$" file)) | |
5631 | (if (not (and xs noxs)) | |
5632 | (progn | |
5633 | (cond ((eq erase 'ignore) (goto-char (point-max))) | |
83261a2f SM |
5634 | (erase (erase-buffer)) |
5635 | (t | |
5636 | (goto-char 1) | |
5637 | (setq rel file) | |
5638 | ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties | |
5639 | (set-text-properties 0 (length rel) nil rel) | |
5640 | (and (equal topdir (substring rel 0 (length topdir))) | |
5641 | (setq rel (substring file (length topdir)))) | |
5642 | (if (search-forward (concat "\f\n" rel ",") nil t) | |
5643 | (progn | |
5644 | (search-backward "\f\n") | |
5645 | (delete-region (point) | |
5646 | (save-excursion | |
5647 | (forward-char 1) | |
5648 | (if (search-forward "\f\n" | |
5649 | nil 'toend) | |
5650 | (- (point) 2) | |
5651 | (point-max))))) | |
5652 | (goto-char (point-max))))) | |
f83d2997 | 5653 | (insert (cperl-find-tags file xs topdir)))))) |
83261a2f SM |
5654 | (if inbuffer nil ; Delegate to the caller |
5655 | (save-buffer 0) ; No backup | |
f83d2997 KH |
5656 | (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs? |
5657 | (initialize-new-tags-table)))))) | |
5658 | ||
5659 | (defvar cperl-tags-hier-regexp-list | |
5c8b7eaf | 5660 | (concat |
f83d2997 KH |
5661 | "^\\(" |
5662 | "\\(package\\)\\>" | |
5663 | "\\|" | |
5664 | "sub\\>[^\n]+::" | |
5665 | "\\|" | |
5666 | "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB? | |
5667 | "\\|" | |
5668 | "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section | |
5669 | "\\)")) | |
5670 | ||
5671 | (defvar cperl-hierarchy '(() ()) | |
f94a632a | 5672 | "Global hierarchy of classes.") |
f83d2997 KH |
5673 | |
5674 | (defun cperl-tags-hier-fill () | |
5675 | ;; Suppose we are in a tag table cooked by cperl. | |
5676 | (goto-char 1) | |
5677 | (let (type pack name pos line chunk ord cons1 file str info fileind) | |
5678 | (while (re-search-forward cperl-tags-hier-regexp-list nil t) | |
5c8b7eaf | 5679 | (setq pos (match-beginning 0) |
f83d2997 KH |
5680 | pack (match-beginning 2)) |
5681 | (beginning-of-line) | |
5682 | (if (looking-at (concat | |
5683 | "\\([^\n]+\\)" | |
5684 | "\C-?" | |
5685 | "\\([^\n]+\\)" | |
5686 | "\C-a" | |
5687 | "\\([0-9]+\\)" | |
5688 | "," | |
5689 | "\\([0-9]+\\)")) | |
5690 | (progn | |
5691 | (setq ;;str (buffer-substring (match-beginning 1) (match-end 1)) | |
5692 | name (buffer-substring (match-beginning 2) (match-end 2)) | |
5693 | ;;pos (buffer-substring (match-beginning 3) (match-end 3)) | |
5bd52f0e | 5694 | line (buffer-substring (match-beginning 3) (match-end 3)) |
f83d2997 | 5695 | ord (if pack 1 0) |
f83d2997 | 5696 | file (file-of-tag) |
5bd52f0e RS |
5697 | fileind (format "%s:%s" file line) |
5698 | ;; Moves to beginning of the next line: | |
5699 | info (cperl-etags-snarf-tag file line)) | |
f83d2997 KH |
5700 | ;; Move back |
5701 | (forward-char -1) | |
5702 | ;; Make new member of hierarchy name ==> file ==> pos if needed | |
5703 | (if (setq cons1 (assoc name (nth ord cperl-hierarchy))) | |
5704 | ;; Name known | |
5705 | (setcdr cons1 (cons (cons fileind (vector file info)) | |
5706 | (cdr cons1))) | |
5707 | ;; First occurrence of the name, start alist | |
5708 | (setq cons1 (cons name (list (cons fileind (vector file info))))) | |
5c8b7eaf | 5709 | (if pack |
f83d2997 KH |
5710 | (setcar (cdr cperl-hierarchy) |
5711 | (cons cons1 (nth 1 cperl-hierarchy))) | |
5712 | (setcar cperl-hierarchy | |
5713 | (cons cons1 (car cperl-hierarchy))))))) | |
5714 | (end-of-line)))) | |
5715 | ||
5716 | (defun cperl-tags-hier-init (&optional update) | |
5717 | "Show hierarchical menu of classes and methods. | |
5718 | Finds info about classes by a scan of loaded TAGS files. | |
5719 | Supposes that the TAGS files contain fully qualified function names. | |
5720 | One may build such TAGS files from CPerl mode menu." | |
5721 | (interactive) | |
5722 | (require 'etags) | |
5723 | (require 'imenu) | |
5724 | (if (or update (null (nth 2 cperl-hierarchy))) | |
83261a2f SM |
5725 | (let ((remover (function (lambda (elt) ; (name (file1...) (file2..)) |
5726 | (or (nthcdr 2 elt) | |
5727 | ;; Only in one file | |
5728 | (setcdr elt (cdr (nth 1 elt))))))) | |
5729 | pack name cons1 to l1 l2 l3 l4 b) | |
f83d2997 KH |
5730 | ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later! |
5731 | (setq cperl-hierarchy (list l1 l2 l3)) | |
5bd52f0e RS |
5732 | (if cperl-xemacs-p ; Not checked |
5733 | (progn | |
5734 | (or tags-file-name | |
5735 | ;; Does this work in XEmacs? | |
f83d2997 KH |
5736 | (call-interactively 'visit-tags-table)) |
5737 | (message "Updating list of classes...") | |
5bd52f0e RS |
5738 | (set-buffer (get-file-buffer tags-file-name)) |
5739 | (cperl-tags-hier-fill)) | |
5740 | (or tags-table-list | |
5741 | (call-interactively 'visit-tags-table)) | |
5c8b7eaf | 5742 | (mapcar |
f83d2997 KH |
5743 | (function |
5744 | (lambda (tagsfile) | |
5bd52f0e | 5745 | (message "Updating list of classes... %s" tagsfile) |
f83d2997 KH |
5746 | (set-buffer (get-file-buffer tagsfile)) |
5747 | (cperl-tags-hier-fill))) | |
5748 | tags-table-list) | |
5bd52f0e | 5749 | (message "Updating list of classes... postprocessing...")) |
f83d2997 KH |
5750 | (mapcar remover (car cperl-hierarchy)) |
5751 | (mapcar remover (nth 1 cperl-hierarchy)) | |
5752 | (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy)) | |
5753 | (cons "Methods: " (car cperl-hierarchy)))) | |
5754 | (cperl-tags-treeify to 1) | |
5755 | (setcar (nthcdr 2 cperl-hierarchy) | |
5756 | (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to)))) | |
5757 | (message "Updating list of classes: done, requesting display...") | |
5758 | ;;(cperl-imenu-addback (nth 2 cperl-hierarchy)) | |
5759 | )) | |
5760 | (or (nth 2 cperl-hierarchy) | |
5761 | (error "No items found")) | |
5762 | (setq update | |
5763 | ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy)) | |
83261a2f SM |
5764 | (if (if (fboundp 'display-popup-menus-p) |
5765 | (let ((f 'display-popup-menus-p)) | |
5766 | (funcall f)) | |
5767 | window-system) | |
f83d2997 KH |
5768 | (x-popup-menu t (nth 2 cperl-hierarchy)) |
5769 | (require 'tmm) | |
5770 | (tmm-prompt (nth 2 cperl-hierarchy)))) | |
5771 | (if (and update (listp update)) | |
5772 | (progn (while (cdr update) (setq update (cdr update))) | |
5773 | (setq update (car update)))) ; Get the last from the list | |
5c8b7eaf | 5774 | (if (vectorp update) |
f83d2997 KH |
5775 | (progn |
5776 | (find-file (elt update 0)) | |
5bd52f0e | 5777 | (cperl-etags-goto-tag-location (elt update 1)))) |
f83d2997 KH |
5778 | (if (eq update -999) (cperl-tags-hier-init t))) |
5779 | ||
5780 | (defun cperl-tags-treeify (to level) | |
5781 | ;; cadr of `to' is read-write. On start it is a cons | |
5c8b7eaf | 5782 | (let* ((regexp (concat "^\\(" (mapconcat |
f83d2997 KH |
5783 | 'identity |
5784 | (make-list level "[_a-zA-Z0-9]+") | |
5785 | "::") | |
5786 | "\\)\\(::\\)?")) | |
5787 | (packages (cdr (nth 1 to))) | |
5788 | (methods (cdr (nth 2 to))) | |
5789 | l1 head tail cons1 cons2 ord writeto packs recurse | |
5790 | root-packages root-functions ms many_ms same_name ps | |
5791 | (move-deeper | |
5c8b7eaf | 5792 | (function |
f83d2997 KH |
5793 | (lambda (elt) |
5794 | (cond ((and (string-match regexp (car elt)) | |
5795 | (or (eq ord 1) (match-end 2))) | |
5796 | (setq head (substring (car elt) 0 (match-end 1)) | |
5c8b7eaf | 5797 | tail (if (match-end 2) (substring (car elt) |
f83d2997 KH |
5798 | (match-end 2))) |
5799 | recurse t) | |
5800 | (if (setq cons1 (assoc head writeto)) nil | |
5801 | ;; Need to init new head | |
5802 | (setcdr writeto (cons (list head (list "Packages: ") | |
5803 | (list "Methods: ")) | |
5804 | (cdr writeto))) | |
5805 | (setq cons1 (nth 1 writeto))) | |
5806 | (setq cons2 (nth ord cons1)) ; Either packs or meths | |
5807 | (setcdr cons2 (cons elt (cdr cons2)))) | |
5808 | ((eq ord 2) | |
5809 | (setq root-functions (cons elt root-functions))) | |
5810 | (t | |
5811 | (setq root-packages (cons elt root-packages)))))))) | |
5812 | (setcdr to l1) ; Init to dynamic space | |
5813 | (setq writeto to) | |
5814 | (setq ord 1) | |
5815 | (mapcar move-deeper packages) | |
5816 | (setq ord 2) | |
5817 | (mapcar move-deeper methods) | |
5818 | (if recurse | |
5819 | (mapcar (function (lambda (elt) | |
5820 | (cperl-tags-treeify elt (1+ level)))) | |
5821 | (cdr to))) | |
5822 | ;;Now clean up leaders with one child only | |
5823 | (mapcar (function (lambda (elt) | |
5c8b7eaf | 5824 | (if (not (and (listp (cdr elt)) |
f83d2997 KH |
5825 | (eq (length elt) 2))) nil |
5826 | (setcar elt (car (nth 1 elt))) | |
5827 | (setcdr elt (cdr (nth 1 elt)))))) | |
5828 | (cdr to)) | |
5829 | ;; Sort the roots of subtrees | |
5830 | (if (default-value 'imenu-sort-function) | |
5831 | (setcdr to | |
5832 | (sort (cdr to) (default-value 'imenu-sort-function)))) | |
5833 | ;; Now add back functions removed from display | |
5834 | (mapcar (function (lambda (elt) | |
5835 | (setcdr to (cons elt (cdr to))))) | |
5836 | (if (default-value 'imenu-sort-function) | |
5837 | (nreverse | |
5838 | (sort root-functions (default-value 'imenu-sort-function))) | |
5839 | root-functions)) | |
5840 | ;; Now add back packages removed from display | |
5841 | (mapcar (function (lambda (elt) | |
5c8b7eaf SS |
5842 | (setcdr to (cons (cons (concat "package " (car elt)) |
5843 | (cdr elt)) | |
f83d2997 KH |
5844 | (cdr to))))) |
5845 | (if (default-value 'imenu-sort-function) | |
5c8b7eaf | 5846 | (nreverse |
f83d2997 | 5847 | (sort root-packages (default-value 'imenu-sort-function))) |
83261a2f | 5848 | root-packages)))) |
f83d2997 KH |
5849 | |
5850 | ;;;(x-popup-menu t | |
5c8b7eaf | 5851 | ;;; '(keymap "Name1" |
f83d2997 | 5852 | ;;; ("Ret1" "aa") |
5c8b7eaf SS |
5853 | ;;; ("Head1" "ab" |
5854 | ;;; keymap "Name2" | |
f83d2997 KH |
5855 | ;;; ("Tail1" "x") ("Tail2" "y")))) |
5856 | ||
5857 | (defun cperl-list-fold (list name limit) | |
5858 | (let (list1 list2 elt1 (num 0)) | |
5859 | (if (<= (length list) limit) list | |
5860 | (setq list1 nil list2 nil) | |
5861 | (while list | |
5c8b7eaf | 5862 | (setq num (1+ num) |
f83d2997 KH |
5863 | elt1 (car list) |
5864 | list (cdr list)) | |
5865 | (if (<= num imenu-max-items) | |
5866 | (setq list2 (cons elt1 list2)) | |
5867 | (setq list1 (cons (cons name | |
5868 | (nreverse list2)) | |
5869 | list1) | |
5870 | list2 (list elt1) | |
5871 | num 1))) | |
5872 | (nreverse (cons (cons name | |
5873 | (nreverse list2)) | |
5874 | list1))))) | |
5875 | ||
5876 | (defun cperl-menu-to-keymap (menu &optional name) | |
5877 | (let (list) | |
5c8b7eaf SS |
5878 | (cons 'keymap |
5879 | (mapcar | |
5880 | (function | |
f83d2997 KH |
5881 | (lambda (elt) |
5882 | (cond ((listp (cdr elt)) | |
5883 | (setq list (cperl-list-fold | |
5884 | (cdr elt) (car elt) imenu-max-items)) | |
5885 | (cons nil | |
5886 | (cons (car elt) | |
5887 | (cperl-menu-to-keymap list)))) | |
5888 | (t | |
5889 | (list (cdr elt) (car elt) t))))) ; t is needed in 19.34 | |
5890 | (cperl-list-fold menu "Root" imenu-max-items))))) | |
5891 | ||
5892 | \f | |
5893 | (defvar cperl-bad-style-regexp | |
5894 | (mapconcat 'identity | |
83261a2f SM |
5895 | '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign |
5896 | "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char | |
5897 | "\\|") | |
f83d2997 KH |
5898 | "Finds places such that insertion of a whitespace may help a lot.") |
5899 | ||
5c8b7eaf | 5900 | (defvar cperl-not-bad-style-regexp |
83261a2f SM |
5901 | (mapconcat |
5902 | 'identity | |
f83d2997 KH |
5903 | '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++ |
5904 | "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used. | |
5905 | "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field) | |
5906 | "<\\$?\\sw+\\(\\.\\sw+\\)?>" ; <IN> <stdin.h> | |
5bd52f0e | 5907 | "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN |
f83d2997 KH |
5908 | "-[0-9]" ; -5 |
5909 | "\\+\\+" ; ++var | |
5910 | "--" ; --var | |
5911 | ".->" ; a->b | |
5912 | "->" ; a SPACE ->b | |
5913 | "\\[-" ; a[-1] | |
5bd52f0e | 5914 | "\\\\[&$@*\\\\]" ; \&func |
f83d2997 | 5915 | "^=" ; =head |
5bd52f0e RS |
5916 | "\\$." ; $| |
5917 | "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO' | |
f83d2997 KH |
5918 | "||" |
5919 | "&&" | |
5920 | "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text> | |
83261a2f | 5921 | "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value |
f83d2997 KH |
5922 | ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below |
5923 | ;;"[*/+-|&<.]+=" | |
5924 | ) | |
5925 | "\\|") | |
5926 | "If matches at the start of match found by `my-bad-c-style-regexp', | |
5927 | insertion of a whitespace will not help.") | |
5928 | ||
5929 | (defvar found-bad) | |
5930 | ||
5931 | (defun cperl-find-bad-style () | |
5932 | "Find places in the buffer where insertion of a whitespace may help. | |
5933 | Prompts user for insertion of spaces. | |
5934 | Currently it is tuned to C and Perl syntax." | |
5935 | (interactive) | |
5936 | (let (found-bad (p (point))) | |
5937 | (setq last-nonmenu-event 13) ; To disable popup | |
5938 | (beginning-of-buffer) | |
5939 | (map-y-or-n-p "Insert space here? " | |
83261a2f | 5940 | (lambda (arg) (insert " ")) |
f83d2997 | 5941 | 'cperl-next-bad-style |
5c8b7eaf | 5942 | '("location" "locations" "insert a space into") |
f83d2997 KH |
5943 | '((?\C-r (lambda (arg) |
5944 | (let ((buffer-quit-function | |
5945 | 'exit-recursive-edit)) | |
5946 | (message "Exit with Esc Esc") | |
5947 | (recursive-edit) | |
5948 | t)) ; Consider acted upon | |
5c8b7eaf | 5949 | "edit, exit with Esc Esc") |
f83d2997 KH |
5950 | (?e (lambda (arg) |
5951 | (let ((buffer-quit-function | |
5952 | 'exit-recursive-edit)) | |
5953 | (message "Exit with Esc Esc") | |
5954 | (recursive-edit) | |
5955 | t)) ; Consider acted upon | |
5956 | "edit, exit with Esc Esc")) | |
5957 | t) | |
5958 | (if found-bad (goto-char found-bad) | |
5959 | (goto-char p) | |
5960 | (message "No appropriate place found")))) | |
5961 | ||
5962 | (defun cperl-next-bad-style () | |
5963 | (let (p (not-found t) (point (point)) found) | |
5964 | (while (and not-found | |
5965 | (re-search-forward cperl-bad-style-regexp nil 'to-end)) | |
5966 | (setq p (point)) | |
5967 | (goto-char (match-beginning 0)) | |
5968 | (if (or | |
5969 | (looking-at cperl-not-bad-style-regexp) | |
5970 | ;; Check for a < -b and friends | |
5971 | (and (eq (following-char) ?\-) | |
5972 | (save-excursion | |
5973 | (skip-chars-backward " \t\n") | |
07cb2aa3 | 5974 | (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{)))) |
f83d2997 KH |
5975 | ;; Now check for syntax type |
5976 | (save-match-data | |
5977 | (setq found (point)) | |
5978 | (beginning-of-defun) | |
5979 | (let ((pps (parse-partial-sexp (point) found))) | |
5980 | (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))) | |
5981 | (goto-char (match-end 0)) | |
5982 | (goto-char (1- p)) | |
5983 | (setq not-found nil | |
5984 | found-bad found))) | |
5985 | (not not-found))) | |
5986 | ||
f1d851ae | 5987 | \f |
f83d2997 | 5988 | ;;; Getting help |
5c8b7eaf | 5989 | (defvar cperl-have-help-regexp |
f83d2997 KH |
5990 | ;;(concat "\\(" |
5991 | (mapconcat | |
5992 | 'identity | |
83261a2f | 5993 | '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable |
f83d2997 KH |
5994 | "[$@]\\^[a-zA-Z]" ; Special variable |
5995 | "[$@][^ \n\t]" ; Special variable | |
5996 | "-[a-zA-Z]" ; File test | |
5997 | "\\\\[a-zA-Z0]" ; Special chars | |
83261a2f | 5998 | "^=[a-z][a-zA-Z0-9_]*" ; POD sections |
f83d2997 KH |
5999 | "[-!&*+,-./<=>?\\\\^|~]+" ; Operator |
6000 | "[a-zA-Z_0-9:]+" ; symbol or number | |
6001 | "x=" | |
83261a2f | 6002 | "#!") |
f83d2997 | 6003 | ;;"\\)\\|\\(" |
83261a2f SM |
6004 | "\\|") |
6005 | ;;"\\)" | |
6006 | ;;) | |
f83d2997 KH |
6007 | "Matches places in the buffer we can find help for.") |
6008 | ||
6009 | (defvar cperl-message-on-help-error t) | |
6010 | (defvar cperl-help-from-timer nil) | |
6011 | ||
6012 | (defun cperl-word-at-point-hard () | |
6013 | ;; Does not save-excursion | |
6014 | ;; Get to the something meaningful | |
6015 | (or (eobp) (eolp) (forward-char 1)) | |
5c8b7eaf | 6016 | (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]" |
f83d2997 KH |
6017 | (save-excursion (beginning-of-line) (point)) |
6018 | 'to-beg) | |
6019 | ;; (cond | |
6020 | ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol | |
6021 | ;; (skip-chars-backward " \n\t\r({[]});,") | |
6022 | ;; (or (bobp) (backward-char 1)))) | |
6023 | ;; Try to backtrace | |
6024 | (cond | |
6025 | ((looking-at "[a-zA-Z0-9_:]") ; symbol | |
6026 | (skip-chars-backward "a-zA-Z0-9_:") | |
5c8b7eaf | 6027 | (cond |
f83d2997 KH |
6028 | ((and (eq (preceding-char) ?^) ; $^I |
6029 | (eq (char-after (- (point) 2)) ?\$)) | |
6030 | (forward-char -2)) | |
6031 | ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob | |
6032 | (forward-char -1)) | |
6033 | ((and (eq (preceding-char) ?\=) | |
6034 | (eq (current-column) 1)) | |
6035 | (forward-char -1))) ; =head1 | |
6036 | (if (and (eq (preceding-char) ?\<) | |
6037 | (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH> | |
6038 | (forward-char -1))) | |
6039 | ((and (looking-at "=") (eq (preceding-char) ?x)) ; x= | |
6040 | (forward-char -1)) | |
6041 | ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I | |
6042 | (forward-char -1)) | |
6043 | ((looking-at "[-!&*+,-./<=>?\\\\^|~]") | |
6044 | (skip-chars-backward "-!&*+,-./<=>?\\\\^|~") | |
6045 | (cond | |
6046 | ((and (eq (preceding-char) ?\$) | |
6047 | (not (eq (char-after (- (point) 2)) ?\$))) ; $- | |
6048 | (forward-char -1)) | |
6049 | ((and (eq (following-char) ?\>) | |
6050 | (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char))) | |
6051 | (save-excursion | |
6052 | (forward-sexp -1) | |
6053 | (and (eq (preceding-char) ?\<) | |
6054 | (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH> | |
6055 | (search-backward "<")))) | |
6056 | ((and (eq (following-char) ?\$) | |
6057 | (eq (preceding-char) ?\<) | |
6058 | (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh> | |
6059 | (forward-char -1))) | |
6060 | (if (looking-at cperl-have-help-regexp) | |
6061 | (buffer-substring (match-beginning 0) (match-end 0)))) | |
6062 | ||
6063 | (defun cperl-get-help () | |
6064 | "Get one-line docs on the symbol at the point. | |
6065 | The data for these docs is a little bit obsolete and may be in fact longer | |
6066 | than a line. Your contribution to update/shorten it is appreciated." | |
6067 | (interactive) | |
6068 | (save-match-data ; May be called "inside" query-replace | |
6069 | (save-excursion | |
6070 | (let ((word (cperl-word-at-point-hard))) | |
6071 | (if word | |
6072 | (if (and cperl-help-from-timer ; Bail out if not in mainland | |
6073 | (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings. | |
6074 | (or (memq (get-text-property (point) 'face) | |
6075 | '(font-lock-comment-face font-lock-string-face)) | |
6076 | (memq (get-text-property (point) 'syntax-type) | |
6077 | '(pod here-doc format)))) | |
6078 | nil | |
6079 | (cperl-describe-perl-symbol word)) | |
6080 | (if cperl-message-on-help-error | |
5c8b7eaf | 6081 | (message "Nothing found for %s..." |
f83d2997 KH |
6082 | (buffer-substring (point) (min (+ 5 (point)) (point-max)))))))))) |
6083 | ||
6084 | ;;; Stolen from perl-descr.el by Johan Vromans: | |
6085 | ||
6086 | (defvar cperl-doc-buffer " *perl-doc*" | |
6087 | "Where the documentation can be found.") | |
6088 | ||
6089 | (defun cperl-describe-perl-symbol (val) | |
6090 | "Display the documentation of symbol at point, a Perl operator." | |
6091 | (let ((enable-recursive-minibuffers t) | |
6092 | args-file regexp) | |
6093 | (cond | |
83261a2f SM |
6094 | ((string-match "^[&*][a-zA-Z_]" val) |
6095 | (setq val (concat (substring val 0 1) "NAME"))) | |
6096 | ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val) | |
6097 | (setq val (concat "@" (substring val 1 (match-end 1))))) | |
6098 | ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val) | |
6099 | (setq val (concat "%" (substring val 1 (match-end 1))))) | |
6100 | ((and (string= val "x") (string-match "^x=" val)) | |
6101 | (setq val "x=")) | |
6102 | ((string-match "^\\$[\C-a-\C-z]" val) | |
6103 | (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1)))))) | |
6104 | ((string-match "^CORE::" val) | |
6105 | (setq val "CORE::")) | |
6106 | ((string-match "^SUPER::" val) | |
6107 | (setq val "SUPER::")) | |
6108 | ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val)) | |
6109 | (setq val "<NAME>"))) | |
5c8b7eaf | 6110 | (setq regexp (concat "^" |
f83d2997 | 6111 | "\\([^a-zA-Z0-9_:]+[ \t]+\\)?" |
5c8b7eaf | 6112 | (regexp-quote val) |
f83d2997 KH |
6113 | "\\([ \t([/]\\|$\\)")) |
6114 | ||
6115 | ;; get the buffer with the documentation text | |
6116 | (cperl-switch-to-doc-buffer) | |
6117 | ||
6118 | ;; lookup in the doc | |
6119 | (goto-char (point-min)) | |
6120 | (let ((case-fold-search nil)) | |
5c8b7eaf | 6121 | (list |
f83d2997 KH |
6122 | (if (re-search-forward regexp (point-max) t) |
6123 | (save-excursion | |
6124 | (beginning-of-line 1) | |
6125 | (let ((lnstart (point))) | |
6126 | (end-of-line) | |
6127 | (message "%s" (buffer-substring lnstart (point))))) | |
6128 | (if cperl-message-on-help-error | |
6129 | (message "No definition for %s" val))))))) | |
6130 | ||
83261a2f | 6131 | (defvar cperl-short-docs 'please-ignore-this-line |
f83d2997 KH |
6132 | ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl) |
6133 | "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5] | |
5c8b7eaf | 6134 | ! ... Logical negation. |
f83d2997 KH |
6135 | ... != ... Numeric inequality. |
6136 | ... !~ ... Search pattern, substitution, or translation (negated). | |
6137 | $! In numeric context: errno. In a string context: error string. | |
6138 | $\" The separator which joins elements of arrays interpolated in strings. | |
6139 | $# The output format for printed numbers. Initial value is %.15g or close. | |
6140 | $$ Process number of this script. Changes in the fork()ed child process. | |
6141 | $% The current page number of the currently selected output channel. | |
6142 | ||
6143 | The following variables are always local to the current block: | |
6144 | ||
6145 | $1 Match of the 1st set of parentheses in the last match (auto-local). | |
6146 | $2 Match of the 2nd set of parentheses in the last match (auto-local). | |
6147 | $3 Match of the 3rd set of parentheses in the last match (auto-local). | |
6148 | $4 Match of the 4th set of parentheses in the last match (auto-local). | |
6149 | $5 Match of the 5th set of parentheses in the last match (auto-local). | |
6150 | $6 Match of the 6th set of parentheses in the last match (auto-local). | |
6151 | $7 Match of the 7th set of parentheses in the last match (auto-local). | |
6152 | $8 Match of the 8th set of parentheses in the last match (auto-local). | |
6153 | $9 Match of the 9th set of parentheses in the last match (auto-local). | |
6154 | $& The string matched by the last pattern match (auto-local). | |
6155 | $' The string after what was matched by the last match (auto-local). | |
6156 | $` The string before what was matched by the last match (auto-local). | |
6157 | ||
6158 | $( The real gid of this process. | |
6159 | $) The effective gid of this process. | |
6160 | $* Deprecated: Set to 1 to do multiline matching within a string. | |
6161 | $+ The last bracket matched by the last search pattern. | |
6162 | $, The output field separator for the print operator. | |
6163 | $- The number of lines left on the page. | |
6164 | $. The current input line number of the last filehandle that was read. | |
6165 | $/ The input record separator, newline by default. | |
6166 | $0 Name of the file containing the perl script being executed. May be set. | |
6167 | $: String may be broken after these characters to fill ^-lines in a format. | |
6168 | $; Subscript separator for multi-dim array emulation. Default \"\\034\". | |
6169 | $< The real uid of this process. | |
6170 | $= The page length of the current output channel. Default is 60 lines. | |
6171 | $> The effective uid of this process. | |
6172 | $? The status returned by the last ``, pipe close or `system'. | |
6173 | $@ The perl error message from the last eval or do @var{EXPR} command. | |
6174 | $ARGV The name of the current file used with <> . | |
6175 | $[ Deprecated: The index of the first element/char in an array/string. | |
6176 | $\\ The output record separator for the print operator. | |
6177 | $] The perl version string as displayed with perl -v. | |
6178 | $^ The name of the current top-of-page format. | |
6179 | $^A The current value of the write() accumulator for format() lines. | |
6180 | $^D The value of the perl debug (-D) flags. | |
6181 | $^E Information about the last system error other than that provided by $!. | |
6182 | $^F The highest system file descriptor, ordinarily 2. | |
6183 | $^H The current set of syntax checks enabled by `use strict'. | |
6184 | $^I The value of the in-place edit extension (perl -i option). | |
6185 | $^L What formats output to perform a formfeed. Default is \f. | |
5bd52f0e | 6186 | $^M A buffer for emergency memory allocation when running out of memory. |
f83d2997 KH |
6187 | $^O The operating system name under which this copy of Perl was built. |
6188 | $^P Internal debugging flag. | |
6189 | $^T The time the script was started. Used by -A/-M/-C file tests. | |
6190 | $^W True if warnings are requested (perl -w flag). | |
6191 | $^X The name under which perl was invoked (argv[0] in C-speech). | |
6192 | $_ The default input and pattern-searching space. | |
5c8b7eaf | 6193 | $| Auto-flush after write/print on current output channel? Default 0. |
f83d2997 KH |
6194 | $~ The name of the current report format. |
6195 | ... % ... Modulo division. | |
6196 | ... %= ... Modulo division assignment. | |
6197 | %ENV Contains the current environment. | |
6198 | %INC List of files that have been require-d or do-ne. | |
6199 | %SIG Used to set signal handlers for various signals. | |
6200 | ... & ... Bitwise and. | |
6201 | ... && ... Logical and. | |
6202 | ... &&= ... Logical and assignment. | |
6203 | ... &= ... Bitwise and assignment. | |
6204 | ... * ... Multiplication. | |
6205 | ... ** ... Exponentiation. | |
6206 | *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2. | |
6207 | &NAME(arg0, ...) Subroutine call. Arguments go to @_. | |
6208 | ... + ... Addition. +EXPR Makes EXPR into scalar context. | |
6209 | ++ Auto-increment (magical on strings). ++EXPR EXPR++ | |
6210 | ... += ... Addition assignment. | |
6211 | , Comma operator. | |
6212 | ... - ... Subtraction. | |
6213 | -- Auto-decrement (NOT magical on strings). --EXPR EXPR-- | |
6214 | ... -= ... Subtraction assignment. | |
6215 | -A Access time in days since script started. | |
6216 | -B File is a non-text (binary) file. | |
6217 | -C Inode change time in days since script started. | |
6218 | -M Age in days since script started. | |
6219 | -O File is owned by real uid. | |
6220 | -R File is readable by real uid. | |
6221 | -S File is a socket . | |
6222 | -T File is a text file. | |
6223 | -W File is writable by real uid. | |
6224 | -X File is executable by real uid. | |
6225 | -b File is a block special file. | |
6226 | -c File is a character special file. | |
6227 | -d File is a directory. | |
6228 | -e File exists . | |
6229 | -f File is a plain file. | |
6230 | -g File has setgid bit set. | |
6231 | -k File has sticky bit set. | |
6232 | -l File is a symbolic link. | |
6233 | -o File is owned by effective uid. | |
6234 | -p File is a named pipe (FIFO). | |
6235 | -r File is readable by effective uid. | |
6236 | -s File has non-zero size. | |
6237 | -t Tests if filehandle (STDIN by default) is opened to a tty. | |
6238 | -u File has setuid bit set. | |
6239 | -w File is writable by effective uid. | |
6240 | -x File is executable by effective uid. | |
6241 | -z File has zero size. | |
6242 | . Concatenate strings. | |
6243 | .. Alternation, also range operator. | |
6244 | .= Concatenate assignment strings | |
6245 | ... / ... Division. /PATTERN/ioxsmg Pattern match | |
6246 | ... /= ... Division assignment. | |
6247 | /PATTERN/ioxsmg Pattern match. | |
6248 | ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well. | |
6249 | <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword). | |
6250 | <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>). | |
6251 | <> Reads line from union of files in @ARGV (= command line) and STDIN. | |
6252 | ... << ... Bitwise shift left. << start of HERE-DOCUMENT. | |
6253 | ... <= ... Numeric less than or equal to. | |
6254 | ... <=> ... Numeric compare. | |
6255 | ... = ... Assignment. | |
6256 | ... == ... Numeric equality. | |
6257 | ... =~ ... Search pattern, substitution, or translation | |
6258 | ... > ... Numeric greater than. | |
6259 | ... >= ... Numeric greater than or equal to. | |
6260 | ... >> ... Bitwise shift right. | |
6261 | ... >>= ... Bitwise shift right assignment. | |
6262 | ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match. | |
6263 | ?PATTERN? One-time pattern match. | |
6264 | @ARGV Command line arguments (not including the command name - see $0). | |
6265 | @INC List of places to look for perl scripts during do/include/use. | |
6266 | @_ Parameter array for subroutines. Also used by split unless in array context. | |
6267 | \\ Creates reference to what follows, like \$var, or quotes non-\w in strings. | |
6268 | \\0 Octal char, e.g. \\033. | |
6269 | \\E Case modification terminator. See \\Q, \\L, and \\U. | |
6270 | \\L Lowercase until \\E . See also \l, lc. | |
6271 | \\U Upcase until \\E . See also \u, uc. | |
6272 | \\Q Quote metacharacters until \\E . See also quotemeta. | |
6273 | \\a Alarm character (octal 007). | |
6274 | \\b Backspace character (octal 010). | |
6275 | \\c Control character, e.g. \\c[ . | |
6276 | \\e Escape character (octal 033). | |
6277 | \\f Formfeed character (octal 014). | |
6278 | \\l Lowercase the next character. See also \\L and \\u, lcfirst. | |
6279 | \\n Newline character (octal 012 on most systems). | |
6280 | \\r Return character (octal 015 on most systems). | |
6281 | \\t Tab character (octal 011). | |
6282 | \\u Upcase the next character. See also \\U and \\l, ucfirst. | |
6283 | \\x Hex character, e.g. \\x1b. | |
6284 | ... ^ ... Bitwise exclusive or. | |
6285 | __END__ Ends program source. | |
6286 | __DATA__ Ends program source. | |
6287 | __FILE__ Current (source) filename. | |
6288 | __LINE__ Current line in current source. | |
6289 | __PACKAGE__ Current package. | |
6290 | ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>. | |
6291 | ARGVOUT Output filehandle with -i flag. | |
6292 | BEGIN { ... } Immediately executed (during compilation) piece of code. | |
6293 | END { ... } Pseudo-subroutine executed after the script finishes. | |
6c389151 SM |
6294 | CHECK { ... } Pseudo-subroutine executed after the script is compiled. |
6295 | INIT { ... } Pseudo-subroutine executed before the script starts running. | |
f83d2997 KH |
6296 | DATA Input filehandle for what follows after __END__ or __DATA__. |
6297 | accept(NEWSOCKET,GENERICSOCKET) | |
6298 | alarm(SECONDS) | |
6299 | atan2(X,Y) | |
6300 | bind(SOCKET,NAME) | |
6301 | binmode(FILEHANDLE) | |
6302 | caller[(LEVEL)] | |
6303 | chdir(EXPR) | |
6304 | chmod(LIST) | |
6305 | chop[(LIST|VAR)] | |
6306 | chown(LIST) | |
6307 | chroot(FILENAME) | |
6308 | close(FILEHANDLE) | |
6309 | closedir(DIRHANDLE) | |
6310 | ... cmp ... String compare. | |
6311 | connect(SOCKET,NAME) | |
6312 | continue of { block } continue { block }. Is executed after `next' or at end. | |
6313 | cos(EXPR) | |
6314 | crypt(PLAINTEXT,SALT) | |
6315 | dbmclose(%HASH) | |
6316 | dbmopen(%HASH,DBNAME,MODE) | |
6317 | defined(EXPR) | |
6318 | delete($HASH{KEY}) | |
6319 | die(LIST) | |
6320 | do { ... }|SUBR while|until EXPR executes at least once | |
6321 | do(EXPR|SUBR([LIST])) (with while|until executes at least once) | |
6322 | dump LABEL | |
6323 | each(%HASH) | |
6324 | endgrent | |
6325 | endhostent | |
6326 | endnetent | |
6327 | endprotoent | |
6328 | endpwent | |
6329 | endservent | |
6330 | eof[([FILEHANDLE])] | |
6331 | ... eq ... String equality. | |
6332 | eval(EXPR) or eval { BLOCK } | |
6333 | exec(LIST) | |
6334 | exit(EXPR) | |
6335 | exp(EXPR) | |
6336 | fcntl(FILEHANDLE,FUNCTION,SCALAR) | |
6337 | fileno(FILEHANDLE) | |
6338 | flock(FILEHANDLE,OPERATION) | |
6339 | for (EXPR;EXPR;EXPR) { ... } | |
6340 | foreach [VAR] (@ARRAY) { ... } | |
6341 | fork | |
6342 | ... ge ... String greater than or equal. | |
6343 | getc[(FILEHANDLE)] | |
6344 | getgrent | |
6345 | getgrgid(GID) | |
6346 | getgrnam(NAME) | |
6347 | gethostbyaddr(ADDR,ADDRTYPE) | |
6348 | gethostbyname(NAME) | |
6349 | gethostent | |
6350 | getlogin | |
6351 | getnetbyaddr(ADDR,ADDRTYPE) | |
6352 | getnetbyname(NAME) | |
6353 | getnetent | |
6354 | getpeername(SOCKET) | |
6355 | getpgrp(PID) | |
6356 | getppid | |
6357 | getpriority(WHICH,WHO) | |
6358 | getprotobyname(NAME) | |
6359 | getprotobynumber(NUMBER) | |
6360 | getprotoent | |
6361 | getpwent | |
6362 | getpwnam(NAME) | |
6363 | getpwuid(UID) | |
6364 | getservbyname(NAME,PROTO) | |
6365 | getservbyport(PORT,PROTO) | |
6366 | getservent | |
6367 | getsockname(SOCKET) | |
6368 | getsockopt(SOCKET,LEVEL,OPTNAME) | |
6369 | gmtime(EXPR) | |
6370 | goto LABEL | |
f83d2997 KH |
6371 | ... gt ... String greater than. |
6372 | hex(EXPR) | |
6373 | if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR | |
6374 | index(STR,SUBSTR[,OFFSET]) | |
6375 | int(EXPR) | |
6376 | ioctl(FILEHANDLE,FUNCTION,SCALAR) | |
6377 | join(EXPR,LIST) | |
6378 | keys(%HASH) | |
6379 | kill(LIST) | |
6380 | last [LABEL] | |
6381 | ... le ... String less than or equal. | |
6382 | length(EXPR) | |
6383 | link(OLDFILE,NEWFILE) | |
6384 | listen(SOCKET,QUEUESIZE) | |
6385 | local(LIST) | |
6386 | localtime(EXPR) | |
6387 | log(EXPR) | |
6388 | lstat(EXPR|FILEHANDLE|VAR) | |
6389 | ... lt ... String less than. | |
6390 | m/PATTERN/iogsmx | |
6391 | mkdir(FILENAME,MODE) | |
6392 | msgctl(ID,CMD,ARG) | |
6393 | msgget(KEY,FLAGS) | |
6394 | msgrcv(ID,VAR,SIZE,TYPE.FLAGS) | |
6395 | msgsnd(ID,MSG,FLAGS) | |
6396 | my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH). | |
6c389151 | 6397 | our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H). |
f83d2997 KH |
6398 | ... ne ... String inequality. |
6399 | next [LABEL] | |
6400 | oct(EXPR) | |
6401 | open(FILEHANDLE[,EXPR]) | |
6402 | opendir(DIRHANDLE,EXPR) | |
6403 | ord(EXPR) ASCII value of the first char of the string. | |
6404 | pack(TEMPLATE,LIST) | |
6405 | package NAME Introduces package context. | |
6406 | pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe. | |
6407 | pop(ARRAY) | |
6408 | print [FILEHANDLE] [(LIST)] | |
6409 | printf [FILEHANDLE] (FORMAT,LIST) | |
6410 | push(ARRAY,LIST) | |
6411 | q/STRING/ Synonym for 'STRING' | |
6412 | qq/STRING/ Synonym for \"STRING\" | |
6413 | qx/STRING/ Synonym for `STRING` | |
6414 | rand[(EXPR)] | |
6415 | read(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6416 | readdir(DIRHANDLE) | |
6417 | readlink(EXPR) | |
6418 | recv(SOCKET,SCALAR,LEN,FLAGS) | |
6419 | redo [LABEL] | |
6420 | rename(OLDNAME,NEWNAME) | |
6421 | require [FILENAME | PERL_VERSION] | |
6422 | reset[(EXPR)] | |
6423 | return(LIST) | |
6424 | reverse(LIST) | |
6425 | rewinddir(DIRHANDLE) | |
6426 | rindex(STR,SUBSTR[,OFFSET]) | |
6427 | rmdir(FILENAME) | |
6428 | s/PATTERN/REPLACEMENT/gieoxsm | |
6429 | scalar(EXPR) | |
6430 | seek(FILEHANDLE,POSITION,WHENCE) | |
6431 | seekdir(DIRHANDLE,POS) | |
6432 | select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT) | |
6433 | semctl(ID,SEMNUM,CMD,ARG) | |
6434 | semget(KEY,NSEMS,SIZE,FLAGS) | |
6435 | semop(KEY,...) | |
6436 | send(SOCKET,MSG,FLAGS[,TO]) | |
6437 | setgrent | |
6438 | sethostent(STAYOPEN) | |
6439 | setnetent(STAYOPEN) | |
6440 | setpgrp(PID,PGRP) | |
6441 | setpriority(WHICH,WHO,PRIORITY) | |
6442 | setprotoent(STAYOPEN) | |
6443 | setpwent | |
6444 | setservent(STAYOPEN) | |
6445 | setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL) | |
6446 | shift[(ARRAY)] | |
6447 | shmctl(ID,CMD,ARG) | |
6448 | shmget(KEY,SIZE,FLAGS) | |
6449 | shmread(ID,VAR,POS,SIZE) | |
6450 | shmwrite(ID,STRING,POS,SIZE) | |
6451 | shutdown(SOCKET,HOW) | |
6452 | sin(EXPR) | |
6453 | sleep[(EXPR)] | |
6454 | socket(SOCKET,DOMAIN,TYPE,PROTOCOL) | |
6455 | socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL) | |
6456 | sort [SUBROUTINE] (LIST) | |
6457 | splice(ARRAY,OFFSET[,LENGTH[,LIST]]) | |
6458 | split[(/PATTERN/[,EXPR[,LIMIT]])] | |
6459 | sprintf(FORMAT,LIST) | |
6460 | sqrt(EXPR) | |
6461 | srand(EXPR) | |
6462 | stat(EXPR|FILEHANDLE|VAR) | |
6463 | study[(SCALAR)] | |
6464 | sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...} | |
6465 | substr(EXPR,OFFSET[,LEN]) | |
6466 | symlink(OLDFILE,NEWFILE) | |
6467 | syscall(LIST) | |
6468 | sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6469 | system(LIST) | |
6470 | syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET]) | |
6471 | tell[(FILEHANDLE)] | |
6472 | telldir(DIRHANDLE) | |
6473 | time | |
6474 | times | |
6475 | tr/SEARCHLIST/REPLACEMENTLIST/cds | |
6476 | truncate(FILE|EXPR,LENGTH) | |
6477 | umask[(EXPR)] | |
6478 | undef[(EXPR)] | |
6479 | unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR | |
6480 | unlink(LIST) | |
6481 | unpack(TEMPLATE,EXPR) | |
6482 | unshift(ARRAY,LIST) | |
6483 | until (EXPR) { ... } EXPR until EXPR | |
6484 | utime(LIST) | |
6485 | values(%HASH) | |
6486 | vec(EXPR,OFFSET,BITS) | |
6487 | wait | |
6488 | waitpid(PID,FLAGS) | |
6489 | wantarray Returns true if the sub/eval is called in list context. | |
6490 | warn(LIST) | |
6491 | while (EXPR) { ... } EXPR while EXPR | |
6492 | write[(EXPR|FILEHANDLE)] | |
6493 | ... x ... Repeat string or array. | |
6494 | x= ... Repetition assignment. | |
6495 | y/SEARCHLIST/REPLACEMENTLIST/ | |
6496 | ... | ... Bitwise or. | |
6497 | ... || ... Logical or. | |
6498 | ~ ... Unary bitwise complement. | |
db133cb6 | 6499 | #! OS interpreter indicator. If contains `perl', used for options, and -x. |
f83d2997 KH |
6500 | AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'. |
6501 | CORE:: Prefix to access builtin function if imported sub obscures it. | |
6502 | SUPER:: Prefix to lookup for a method in @ISA classes. | |
6503 | DESTROY Shorthand for `sub DESTROY {...}'. | |
6504 | ... EQ ... Obsolete synonym of `eq'. | |
6505 | ... GE ... Obsolete synonym of `ge'. | |
6506 | ... GT ... Obsolete synonym of `gt'. | |
6507 | ... LE ... Obsolete synonym of `le'. | |
6508 | ... LT ... Obsolete synonym of `lt'. | |
6509 | ... NE ... Obsolete synonym of `ne'. | |
6510 | abs [ EXPR ] absolute value | |
6511 | ... and ... Low-precedence synonym for &&. | |
6512 | bless REFERENCE [, PACKAGE] Makes reference into an object of a package. | |
6513 | chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''! | |
6514 | chr Converts a number to char with the same ordinal. | |
6515 | else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}. | |
6516 | elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}. | |
83261a2f | 6517 | exists $HASH{KEY} True if the key exists. |
f83d2997 KH |
6518 | format [NAME] = Start of output format. Ended by a single dot (.) on a line. |
6519 | formline PICTURE, LIST Backdoor into \"format\" processing. | |
6520 | glob EXPR Synonym of <EXPR>. | |
6521 | lc [ EXPR ] Returns lowercased EXPR. | |
6522 | lcfirst [ EXPR ] Returns EXPR with lower-cased first letter. | |
db133cb6 | 6523 | grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK. |
f83d2997 KH |
6524 | map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST. |
6525 | no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method. | |
6526 | not ... Low-precedence synonym for ! - negation. | |
6527 | ... or ... Low-precedence synonym for ||. | |
6528 | pos STRING Set/Get end-position of the last match over this string, see \\G. | |
6529 | quotemeta [ EXPR ] Quote regexp metacharacters. | |
6530 | qw/WORD1 .../ Synonym of split('', 'WORD1 ...') | |
6531 | readline FH Synonym of <FH>. | |
6532 | readpipe CMD Synonym of `CMD`. | |
6533 | ref [ EXPR ] Type of EXPR when dereferenced. | |
6534 | sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.) | |
6535 | tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable. | |
6536 | tied Returns internal object for a tied data. | |
6537 | uc [ EXPR ] Returns upcased EXPR. | |
6538 | ucfirst [ EXPR ] Returns EXPR with upcased first letter. | |
6539 | untie VAR Unlink an object from a simple Perl variable. | |
6540 | use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'. | |
6541 | ... xor ... Low-precedence synonym for exclusive or. | |
6542 | prototype \&SUB Returns prototype of the function given a reference. | |
6543 | =head1 Top-level heading. | |
6544 | =head2 Second-level heading. | |
6545 | =head3 Third-level heading (is there such?). | |
6546 | =over [ NUMBER ] Start list. | |
6547 | =item [ TITLE ] Start new item in the list. | |
6548 | =back End list. | |
6549 | =cut Switch from POD to Perl. | |
6550 | =pod Switch from Perl to POD. | |
6551 | ") | |
6552 | ||
6553 | (defun cperl-switch-to-doc-buffer () | |
6554 | "Go to the perl documentation buffer and insert the documentation." | |
6555 | (interactive) | |
6556 | (let ((buf (get-buffer-create cperl-doc-buffer))) | |
6557 | (if (interactive-p) | |
6558 | (switch-to-buffer-other-window buf) | |
6559 | (set-buffer buf)) | |
6560 | (if (= (buffer-size) 0) | |
6561 | (progn | |
6562 | (insert (documentation-property 'cperl-short-docs | |
6563 | 'variable-documentation)) | |
6564 | (setq buffer-read-only t))))) | |
6565 | ||
6c389151 | 6566 | (defun cperl-beautify-regexp-piece (b e embed level) |
f83d2997 KH |
6567 | ;; b is before the starting delimiter, e before the ending |
6568 | ;; e should be a marker, may be changed, but remains "correct". | |
6c389151 SM |
6569 | ;; EMBED is nil iff we process the whole REx. |
6570 | ;; The REx is guarantied to have //x | |
6571 | ;; LEVEL shows how many levels deep to go | |
6572 | ;; position at enter and at leave is not defined | |
6573 | (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos) | |
f83d2997 KH |
6574 | (if (not embed) |
6575 | (goto-char (1+ b)) | |
6576 | (goto-char b) | |
6c389151 | 6577 | (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing |
f83d2997 KH |
6578 | (forward-char 2) |
6579 | (delete-char 1) | |
6580 | (forward-char 1)) | |
6581 | ((looking-at "(\\?[^a-zA-Z]") | |
6582 | (forward-char 3)) | |
6583 | ((looking-at "(\\?") ; (?i) | |
6584 | (forward-char 2)) | |
6585 | (t | |
6586 | (forward-char 1)))) | |
6587 | (setq c (if embed (current-indentation) (1- (current-column))) | |
6588 | c1 (+ c (or cperl-regexp-indent-step cperl-indent-level))) | |
6589 | (or (looking-at "[ \t]*[\n#]") | |
6590 | (progn | |
6591 | (insert "\n"))) | |
6592 | (goto-char e) | |
6593 | (beginning-of-line) | |
6594 | (if (re-search-forward "[^ \t]" e t) | |
83261a2f | 6595 | (progn ; Something before the ending delimiter |
f83d2997 | 6596 | (goto-char e) |
6c389151 | 6597 | (delete-horizontal-space) |
f83d2997 KH |
6598 | (insert "\n") |
6599 | (indent-to-column c) | |
6600 | (set-marker e (point)))) | |
6601 | (goto-char b) | |
6602 | (end-of-line 2) | |
6603 | (while (< (point) (marker-position e)) | |
6604 | (beginning-of-line) | |
6605 | (setq s (point) | |
6606 | inline t) | |
6607 | (skip-chars-forward " \t") | |
6608 | (delete-region s (point)) | |
6609 | (indent-to-column c1) | |
6610 | (while (and | |
6611 | inline | |
5c8b7eaf | 6612 | (looking-at |
f83d2997 KH |
6613 | (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word |
6614 | "\\|" ; Embedded variable | |
6615 | "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3 | |
6616 | "\\|" ; $ ^ | |
6617 | "[$^]" | |
6618 | "\\|" ; simple-code simple-code*? | |
6619 | "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5 | |
6620 | "\\|" ; Class | |
6621 | "\\(\\[\\)" ; 6 | |
6622 | "\\|" ; Grouping | |
6623 | "\\((\\(\\?\\)?\\)" ; 7 8 | |
6624 | "\\|" ; | | |
83261a2f | 6625 | "\\(|\\)"))) ; 9 |
f83d2997 KH |
6626 | (goto-char (match-end 0)) |
6627 | (setq spaces t) | |
6628 | (cond ((match-beginning 1) ; Alphanum word + junk | |
6629 | (forward-char -1)) | |
6630 | ((or (match-beginning 3) ; $ab[12] | |
6631 | (and (match-beginning 5) ; X* X+ X{2,3} | |
6632 | (eq (preceding-char) ?\{))) | |
6633 | (forward-char -1) | |
6634 | (forward-sexp 1)) | |
6635 | ((match-beginning 6) ; [] | |
6636 | (setq tmp (point)) | |
6637 | (if (looking-at "\\^?\\]") | |
6638 | (goto-char (match-end 0))) | |
6c389151 SM |
6639 | ;; XXXX POSIX classes?! |
6640 | (while (and (not pos) | |
6641 | (re-search-forward "\\[:\\|\\]" e t)) | |
6642 | (if (eq (preceding-char) ?:) | |
6643 | (or (re-search-forward ":\\]" e t) | |
6644 | (error "[:POSIX:]-group in []-group not terminated")) | |
6645 | (setq pos t))) | |
6646 | (or (eq (preceding-char) ?\]) | |
6647 | (error "[]-group not terminated")) | |
6648 | (if (eq (following-char) ?\{) | |
f83d2997 | 6649 | (progn |
6c389151 SM |
6650 | (forward-sexp 1) |
6651 | (and (eq (following-char) ??) | |
6652 | (forward-char 1))) | |
6653 | (re-search-forward "\\=\\([*+?]\\??\\)" e t))) | |
f83d2997 KH |
6654 | ((match-beginning 7) ; () |
6655 | (goto-char (match-beginning 0)) | |
6c389151 SM |
6656 | (setq pos (current-column)) |
6657 | (or (eq pos c1) | |
f83d2997 | 6658 | (progn |
6c389151 | 6659 | (delete-horizontal-space) |
f83d2997 KH |
6660 | (insert "\n") |
6661 | (indent-to-column c1))) | |
6662 | (setq tmp (point)) | |
6663 | (forward-sexp 1) | |
6664 | ;; (or (forward-sexp 1) | |
6665 | ;; (progn | |
6666 | ;; (goto-char tmp) | |
6667 | ;; (error "()-group not terminated"))) | |
6668 | (set-marker m (1- (point))) | |
6669 | (set-marker m1 (point)) | |
6c389151 SM |
6670 | (if (= level 1) |
6671 | (if (progn ; indent rigidly if multiline | |
a1506d29 | 6672 | ;; In fact does not make a lot of sense, since |
6c389151 SM |
6673 | ;; the starting position can be already lost due |
6674 | ;; to insertion of "\n" and " " | |
6675 | (goto-char tmp) | |
6676 | (search-forward "\n" m1 t)) | |
6677 | (indent-rigidly (point) m1 (- c1 pos))) | |
6678 | (setq level (1- level)) | |
6679 | (cond | |
6680 | ((not (match-beginning 8)) | |
6681 | (cperl-beautify-regexp-piece tmp m t level)) | |
6682 | ((eq (char-after (+ 2 tmp)) ?\{) ; Code | |
6683 | t) | |
6684 | ((eq (char-after (+ 2 tmp)) ?\() ; Conditional | |
6685 | (goto-char (+ 2 tmp)) | |
6686 | (forward-sexp 1) | |
6687 | (cperl-beautify-regexp-piece (point) m t level)) | |
6688 | ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind | |
6689 | (goto-char (+ 3 tmp)) | |
6690 | (cperl-beautify-regexp-piece (point) m t level)) | |
6691 | (t | |
6692 | (cperl-beautify-regexp-piece tmp m t level)))) | |
f83d2997 KH |
6693 | (goto-char m1) |
6694 | (cond ((looking-at "[*+?]\\??") | |
6695 | (goto-char (match-end 0))) | |
6696 | ((eq (following-char) ?\{) | |
6697 | (forward-sexp 1) | |
6698 | (if (eq (following-char) ?\?) | |
6699 | (forward-char)))) | |
6700 | (skip-chars-forward " \t") | |
6701 | (setq spaces nil) | |
6702 | (if (looking-at "[#\n]") | |
6703 | (progn | |
6704 | (or (eolp) (indent-for-comment)) | |
6705 | (beginning-of-line 2)) | |
6c389151 | 6706 | (delete-horizontal-space) |
f83d2997 KH |
6707 | (insert "\n")) |
6708 | (end-of-line) | |
6709 | (setq inline nil)) | |
6710 | ((match-beginning 9) ; | | |
6711 | (forward-char -1) | |
6712 | (setq tmp (point)) | |
6713 | (beginning-of-line) | |
6714 | (if (re-search-forward "[^ \t]" tmp t) | |
6715 | (progn | |
6716 | (goto-char tmp) | |
6c389151 | 6717 | (delete-horizontal-space) |
f83d2997 KH |
6718 | (insert "\n")) |
6719 | ;; first at line | |
6720 | (delete-region (point) tmp)) | |
6721 | (indent-to-column c) | |
6722 | (forward-char 1) | |
6723 | (skip-chars-forward " \t") | |
6724 | (setq spaces nil) | |
6725 | (if (looking-at "[#\n]") | |
6726 | (beginning-of-line 2) | |
6c389151 | 6727 | (delete-horizontal-space) |
f83d2997 KH |
6728 | (insert "\n")) |
6729 | (end-of-line) | |
6730 | (setq inline nil))) | |
6731 | (or (looking-at "[ \t\n]") | |
6732 | (not spaces) | |
6733 | (insert " ")) | |
6734 | (skip-chars-forward " \t")) | |
83261a2f SM |
6735 | (or (looking-at "[#\n]") |
6736 | (error "Unknown code `%s' in a regexp" | |
6737 | (buffer-substring (point) (1+ (point))))) | |
6738 | (and inline (end-of-line 2))) | |
f83d2997 KH |
6739 | ;; Special-case the last line of group |
6740 | (if (and (>= (point) (marker-position e)) | |
6741 | (/= (current-indentation) c)) | |
6742 | (progn | |
83261a2f SM |
6743 | (beginning-of-line) |
6744 | (setq s (point)) | |
6745 | (skip-chars-forward " \t") | |
6746 | (delete-region s (point)) | |
6747 | (indent-to-column c))))) | |
f83d2997 KH |
6748 | |
6749 | (defun cperl-make-regexp-x () | |
db133cb6 | 6750 | ;; Returns position of the start |
6c389151 | 6751 | ;; XXX this is called too often! Need to cache the result! |
f83d2997 KH |
6752 | (save-excursion |
6753 | (or cperl-use-syntax-table-text-property | |
5bd52f0e | 6754 | (error "I need to have a regexp marked!")) |
f83d2997 | 6755 | ;; Find the start |
db133cb6 RS |
6756 | (if (looking-at "\\s|") |
6757 | nil ; good already | |
5bd52f0e | 6758 | (if (looking-at "\\([smy]\\|qr\\)\\s|") |
db133cb6 | 6759 | (forward-char 1) |
83261a2f | 6760 | (re-search-backward "\\s|"))) ; Assume it is scanned already. |
f83d2997 KH |
6761 | ;;(forward-char 1) |
6762 | (let ((b (point)) (e (make-marker)) have-x delim (c (current-column)) | |
6763 | (sub-p (eq (preceding-char) ?s)) s) | |
6764 | (forward-sexp 1) | |
6765 | (set-marker e (1- (point))) | |
6766 | (setq delim (preceding-char)) | |
6767 | (if (and sub-p (eq delim (char-after (- (point) 2)))) | |
6768 | (error "Possible s/blah// - do not know how to deal with")) | |
6769 | (if sub-p (forward-sexp 1)) | |
5c8b7eaf | 6770 | (if (looking-at "\\sw*x") |
f83d2997 KH |
6771 | (setq have-x t) |
6772 | (insert "x")) | |
6773 | ;; Protect fragile " ", "#" | |
6774 | (if have-x nil | |
6775 | (goto-char (1+ b)) | |
6776 | (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too? | |
6777 | (forward-char -1) | |
6778 | (insert "\\") | |
6779 | (forward-char 1))) | |
6780 | b))) | |
6781 | ||
6c389151 | 6782 | (defun cperl-beautify-regexp (&optional deep) |
f94a632a | 6783 | "Do it. (Experimental, may change semantics, recheck the result.) |
f83d2997 | 6784 | We suppose that the regexp is scanned already." |
6c389151 | 6785 | (interactive "P") |
0c602a0f | 6786 | (setq deep (if deep (prefix-numeric-value deep) -1)) |
6c389151 SM |
6787 | (save-excursion |
6788 | (goto-char (cperl-make-regexp-x)) | |
6789 | (let ((b (point)) (e (make-marker))) | |
6790 | (forward-sexp 1) | |
6791 | (set-marker e (1- (point))) | |
6792 | (cperl-beautify-regexp-piece b e nil deep)))) | |
f83d2997 | 6793 | |
db133cb6 RS |
6794 | (defun cperl-regext-to-level-start () |
6795 | "Goto start of an enclosing group in regexp. | |
f83d2997 KH |
6796 | We suppose that the regexp is scanned already." |
6797 | (interactive) | |
db133cb6 | 6798 | (let ((limit (cperl-make-regexp-x)) done) |
f83d2997 KH |
6799 | (while (not done) |
6800 | (or (eq (following-char) ?\() | |
db133cb6 | 6801 | (search-backward "(" (1+ limit) t) |
f83d2997 KH |
6802 | (error "Cannot find `(' which starts a group")) |
6803 | (setq done | |
6804 | (save-excursion | |
6805 | (skip-chars-backward "\\") | |
6806 | (looking-at "\\(\\\\\\\\\\)*("))) | |
db133cb6 RS |
6807 | (or done (forward-char -1))))) |
6808 | ||
6809 | (defun cperl-contract-level () | |
5bd52f0e | 6810 | "Find an enclosing group in regexp and contract it. |
db133cb6 RS |
6811 | \(Experimental, may change semantics, recheck the result.) |
6812 | We suppose that the regexp is scanned already." | |
6813 | (interactive) | |
6c389151 | 6814 | ;; (save-excursion ; Can't, breaks `cperl-contract-levels' |
83261a2f SM |
6815 | (cperl-regext-to-level-start) |
6816 | (let ((b (point)) (e (make-marker)) s c) | |
6817 | (forward-sexp 1) | |
6818 | (set-marker e (1- (point))) | |
6819 | (goto-char b) | |
6820 | (while (re-search-forward "\\(#\\)\\|\n" e 'to-end) | |
6821 | (cond | |
6822 | ((match-beginning 1) ; #-comment | |
6823 | (or c (setq c (current-indentation))) | |
6824 | (beginning-of-line 2) ; Skip | |
6825 | (setq s (point)) | |
6826 | (skip-chars-forward " \t") | |
6827 | (delete-region s (point)) | |
6828 | (indent-to-column c)) | |
6829 | (t | |
6830 | (delete-char -1) | |
6831 | (just-one-space)))))) | |
db133cb6 RS |
6832 | |
6833 | (defun cperl-contract-levels () | |
5bd52f0e | 6834 | "Find an enclosing group in regexp and contract all the kids. |
db133cb6 RS |
6835 | \(Experimental, may change semantics, recheck the result.) |
6836 | We suppose that the regexp is scanned already." | |
6837 | (interactive) | |
6c389151 SM |
6838 | (save-excursion |
6839 | (condition-case nil | |
6840 | (cperl-regext-to-level-start) | |
6841 | (error ; We are outside outermost group | |
5efe6a56 SM |
6842 | (goto-char (cperl-make-regexp-x)))) |
6843 | (let ((b (point)) (e (make-marker)) s c) | |
6844 | (forward-sexp 1) | |
6845 | (set-marker e (1- (point))) | |
6846 | (goto-char (1+ b)) | |
6847 | (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t) | |
a1506d29 | 6848 | (cond |
6c389151 SM |
6849 | ((match-beginning 1) ; Skip |
6850 | nil) | |
6851 | (t ; Group | |
6852 | (cperl-contract-level))))))) | |
f83d2997 | 6853 | |
6c389151 | 6854 | (defun cperl-beautify-level (&optional deep) |
f83d2997 KH |
6855 | "Find an enclosing group in regexp and beautify it. |
6856 | \(Experimental, may change semantics, recheck the result.) | |
6857 | We suppose that the regexp is scanned already." | |
6c389151 | 6858 | (interactive "P") |
0c602a0f | 6859 | (setq deep (if deep (prefix-numeric-value deep) -1)) |
6c389151 SM |
6860 | (save-excursion |
6861 | (cperl-regext-to-level-start) | |
6862 | (let ((b (point)) (e (make-marker))) | |
6863 | (forward-sexp 1) | |
6864 | (set-marker e (1- (point))) | |
6865 | (cperl-beautify-regexp-piece b e nil deep)))) | |
db133cb6 RS |
6866 | |
6867 | (defun cperl-invert-if-unless () | |
6c389151 | 6868 | "Change `if (A) {B}' into `B if A;' etc if possible." |
db133cb6 RS |
6869 | (interactive) |
6870 | (or (looking-at "\\<") | |
83261a2f | 6871 | (forward-sexp -1)) |
6c389151 | 6872 | (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>") |
db133cb6 RS |
6873 | (let ((pos1 (point)) |
6874 | pos2 pos3 pos4 pos5 s1 s2 state p pos45 | |
6875 | (s0 (buffer-substring (match-beginning 0) (match-end 0)))) | |
6876 | (forward-sexp 2) | |
6877 | (setq pos3 (point)) | |
6878 | (forward-sexp -1) | |
6879 | (setq pos2 (point)) | |
6880 | (if (eq (following-char) ?\( ) | |
6881 | (progn | |
6882 | (goto-char pos3) | |
6883 | (forward-sexp 1) | |
6884 | (setq pos5 (point)) | |
6885 | (forward-sexp -1) | |
6886 | (setq pos4 (point)) | |
6887 | ;; XXXX In fact may be `A if (B); {C}' ... | |
6888 | (if (and (eq (following-char) ?\{ ) | |
6889 | (progn | |
6890 | (cperl-backward-to-noncomment pos3) | |
6891 | (eq (preceding-char) ?\) ))) | |
6892 | (if (condition-case nil | |
6893 | (progn | |
6894 | (goto-char pos5) | |
6895 | (forward-sexp 1) | |
6896 | (forward-sexp -1) | |
6897 | (looking-at "\\<els\\(e\\|if\\)\\>")) | |
6898 | (error nil)) | |
6899 | (error | |
6900 | "`%s' (EXPR) {BLOCK} with `else'/`elsif'" s0) | |
6901 | (goto-char (1- pos5)) | |
6902 | (cperl-backward-to-noncomment pos4) | |
6903 | (if (eq (preceding-char) ?\;) | |
6904 | (forward-char -1)) | |
6905 | (setq pos45 (point)) | |
6906 | (goto-char pos4) | |
6907 | (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" pos45 t) | |
6908 | (setq p (match-beginning 0) | |
6909 | s1 (buffer-substring p (match-end 0)) | |
6910 | state (parse-partial-sexp pos4 p)) | |
5c8b7eaf | 6911 | (or (nth 3 state) |
db133cb6 RS |
6912 | (nth 4 state) |
6913 | (nth 5 state) | |
6914 | (error "`%s' inside `%s' BLOCK" s1 s0)) | |
6915 | (goto-char (match-end 0))) | |
6916 | ;; Finally got it | |
6917 | (goto-char (1+ pos4)) | |
6918 | (skip-chars-forward " \t\n") | |
6919 | (setq s2 (buffer-substring (point) pos45)) | |
6920 | (goto-char pos45) | |
6921 | (or (looking-at ";?[ \t\n]*}") | |
6922 | (progn | |
6923 | (skip-chars-forward "; \t\n") | |
6924 | (setq s2 (concat s2 "\n" (buffer-substring (point) (1- pos5)))))) | |
6925 | (and (equal s2 "") | |
6926 | (setq s2 "1")) | |
6927 | (goto-char (1- pos3)) | |
6928 | (cperl-backward-to-noncomment pos2) | |
6929 | (or (looking-at "[ \t\n]*)") | |
6930 | (goto-char (1- pos3))) | |
6931 | (setq p (point)) | |
6932 | (goto-char (1+ pos2)) | |
6933 | (skip-chars-forward " \t\n") | |
6934 | (setq s1 (buffer-substring (point) p)) | |
6935 | (delete-region pos4 pos5) | |
6936 | (delete-region pos2 pos3) | |
6937 | (goto-char pos1) | |
6938 | (insert s2 " ") | |
6939 | (just-one-space) | |
6940 | (forward-word 1) | |
6941 | (setq pos1 (point)) | |
6942 | (insert " " s1 ";") | |
6c389151 | 6943 | (delete-horizontal-space) |
db133cb6 RS |
6944 | (forward-char -1) |
6945 | (delete-horizontal-space) | |
6946 | (goto-char pos1) | |
6947 | (just-one-space) | |
6948 | (cperl-indent-line)) | |
6949 | (error "`%s' (EXPR) not with an {BLOCK}" s0))) | |
6950 | (error "`%s' not with an (EXPR)" s0))) | |
f94a632a | 6951 | (error "Not at `if', `unless', `while', `until', `for' or `foreach'"))) |
db133cb6 | 6952 | |
5bd52f0e | 6953 | ;;; By Anthony Foiani <afoiani@uswest.com> |
b7ec9e59 RS |
6954 | ;;; Getting help on modules in C-h f ? |
6955 | ;;; This is a modified version of `man'. | |
6956 | ;;; Need to teach it how to lookup functions | |
6957 | (defun cperl-perldoc (word) | |
6958 | "Run `perldoc' on WORD." | |
6959 | (interactive | |
6960 | (list (let* ((default-entry (cperl-word-at-point)) | |
6961 | (input (read-string | |
6962 | (format "perldoc entry%s: " | |
6963 | (if (string= default-entry "") | |
6964 | "" | |
6965 | (format " (default %s)" default-entry)))))) | |
6966 | (if (string= input "") | |
6967 | (if (string= default-entry "") | |
6968 | (error "No perldoc args given") | |
6969 | default-entry) | |
6970 | input)))) | |
a8e1e57f | 6971 | (require 'man) |
5c8b7eaf | 6972 | (let* ((is-func (and |
b7ec9e59 RS |
6973 | (string-match "^[a-z]+$" word) |
6974 | (string-match (concat "^" word "\\>") | |
6975 | (documentation-property | |
6976 | 'cperl-short-docs | |
6977 | 'variable-documentation)))) | |
6978 | (manual-program (if is-func "perldoc -f" "perldoc"))) | |
b7ec9e59 RS |
6979 | (Man-getpage-in-background word))) |
6980 | ||
6981 | (defun cperl-perldoc-at-point () | |
6982 | "Run a `perldoc' on the word around point." | |
6983 | (interactive) | |
6984 | (cperl-perldoc (cperl-word-at-point))) | |
6985 | ||
6986 | (defcustom pod2man-program "pod2man" | |
6987 | "*File name for `pod2man'." | |
6988 | :type 'file | |
6989 | :group 'cperl) | |
6990 | ||
5bd52f0e | 6991 | ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes) |
b7ec9e59 RS |
6992 | (defun cperl-pod-to-manpage () |
6993 | "Create a virtual manpage in Emacs from the Perl Online Documentation." | |
6994 | (interactive) | |
6995 | (require 'man) | |
6996 | (let* ((pod2man-args (concat buffer-file-name " | nroff -man ")) | |
6997 | (bufname (concat "Man " buffer-file-name)) | |
6998 | (buffer (generate-new-buffer bufname))) | |
6999 | (save-excursion | |
7000 | (set-buffer buffer) | |
7001 | (let ((process-environment (copy-sequence process-environment))) | |
7002 | ;; Prevent any attempt to use display terminal fanciness. | |
7003 | (setenv "TERM" "dumb") | |
7004 | (set-process-sentinel | |
7005 | (start-process pod2man-program buffer "sh" "-c" | |
7006 | (format (cperl-pod2man-build-command) pod2man-args)) | |
7007 | 'Man-bgproc-sentinel))))) | |
7008 | ||
7009 | (defun cperl-pod2man-build-command () | |
7010 | "Builds the entire background manpage and cleaning command." | |
7011 | (let ((command (concat pod2man-program " %s 2>/dev/null")) | |
7012 | (flist Man-filter-list)) | |
7013 | (while (and flist (car flist)) | |
7014 | (let ((pcom (car (car flist))) | |
7015 | (pargs (cdr (car flist)))) | |
7016 | (setq command | |
7017 | (concat command " | " pcom " " | |
7018 | (mapconcat '(lambda (phrase) | |
7019 | (if (not (stringp phrase)) | |
7020 | (error "Malformed Man-filter-list")) | |
7021 | phrase) | |
7022 | pargs " "))) | |
7023 | (setq flist (cdr flist)))) | |
7024 | command)) | |
db133cb6 RS |
7025 | |
7026 | (defun cperl-lazy-install ()) ; Avoid a warning | |
f83d2997 KH |
7027 | |
7028 | (if (fboundp 'run-with-idle-timer) | |
7029 | (progn | |
7030 | (defvar cperl-help-shown nil | |
7031 | "Non-nil means that the help was already shown now.") | |
7032 | ||
7033 | (defvar cperl-lazy-installed nil | |
7034 | "Non-nil means that the lazy-help handlers are installed now.") | |
7035 | ||
7036 | (defun cperl-lazy-install () | |
7037 | (interactive) | |
7038 | (make-variable-buffer-local 'cperl-help-shown) | |
7039 | (if (and (cperl-val 'cperl-lazy-help-time) | |
7040 | (not cperl-lazy-installed)) | |
7041 | (progn | |
7042 | (add-hook 'post-command-hook 'cperl-lazy-hook) | |
5c8b7eaf SS |
7043 | (run-with-idle-timer |
7044 | (cperl-val 'cperl-lazy-help-time 1000000 5) | |
7045 | t | |
f83d2997 KH |
7046 | 'cperl-get-help-defer) |
7047 | (setq cperl-lazy-installed t)))) | |
7048 | ||
7049 | (defun cperl-lazy-unstall () | |
7050 | (interactive) | |
7051 | (remove-hook 'post-command-hook 'cperl-lazy-hook) | |
7052 | (cancel-function-timers 'cperl-get-help-defer) | |
7053 | (setq cperl-lazy-installed nil)) | |
7054 | ||
7055 | (defun cperl-lazy-hook () | |
7056 | (setq cperl-help-shown nil)) | |
7057 | ||
7058 | (defun cperl-get-help-defer () | |
83261a2f | 7059 | (if (not (memq major-mode '(perl-mode cperl-mode))) nil |
f83d2997 KH |
7060 | (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t)) |
7061 | (cperl-get-help) | |
7062 | (setq cperl-help-shown t)))) | |
7063 | (cperl-lazy-install))) | |
7064 | ||
db133cb6 RS |
7065 | |
7066 | ;;; Plug for wrong font-lock: | |
7067 | ||
7068 | (defun cperl-font-lock-unfontify-region-function (beg end) | |
7069 | (let* ((modified (buffer-modified-p)) (buffer-undo-list t) | |
7070 | (inhibit-read-only t) (inhibit-point-motion-hooks t) | |
7071 | before-change-functions after-change-functions | |
7072 | deactivate-mark buffer-file-name buffer-file-truename) | |
7073 | (remove-text-properties beg end '(face nil)) | |
7074 | (when (and (not modified) (buffer-modified-p)) | |
7075 | (set-buffer-modified-p nil)))) | |
7076 | ||
7077 | (defvar cperl-d-l nil) | |
7078 | (defun cperl-fontify-syntaxically (end) | |
5bd52f0e | 7079 | ;; Some vars for debugging only |
6c389151 | 7080 | ;; (message "Syntaxifying...") |
83261a2f SM |
7081 | (let ((dbg (point)) (iend end) |
7082 | (istate (car cperl-syntax-state)) | |
7083 | start) | |
5bd52f0e RS |
7084 | (and cperl-syntaxify-unwind |
7085 | (setq end (cperl-unwind-to-safe t end))) | |
7086 | (setq start (point)) | |
db133cb6 RS |
7087 | (or cperl-syntax-done-to |
7088 | (setq cperl-syntax-done-to (point-min))) | |
7089 | (if (or (not (boundp 'font-lock-hot-pass)) | |
5bd52f0e RS |
7090 | (eval 'font-lock-hot-pass) |
7091 | t) ; Not debugged otherwise | |
db133cb6 RS |
7092 | ;; Need to forget what is after `start' |
7093 | (setq start (min cperl-syntax-done-to start)) | |
7094 | ;; Fontification without a change | |
7095 | (setq start (max cperl-syntax-done-to start))) | |
7096 | (and (> end start) | |
7097 | (setq cperl-syntax-done-to start) ; In case what follows fails | |
7098 | (cperl-find-pods-heres start end t nil t)) | |
5bd52f0e | 7099 | (if (eq cperl-syntaxify-by-font-lock 'message) |
5c8b7eaf SS |
7100 | (message "Syntaxified %s..%s from %s to %s(%s), state %s-->%s" |
7101 | dbg iend | |
7102 | start end cperl-syntax-done-to | |
7103 | istate (car cperl-syntax-state))) ; For debugging | |
83261a2f | 7104 | nil)) ; Do not iterate |
db133cb6 | 7105 | |
5bd52f0e RS |
7106 | (defun cperl-fontify-update (end) |
7107 | (let ((pos (point)) prop posend) | |
7108 | (while (< pos end) | |
7109 | (setq prop (get-text-property pos 'cperl-postpone)) | |
7110 | (setq posend (next-single-property-change pos 'cperl-postpone nil end)) | |
7111 | (and prop (put-text-property pos posend (car prop) (cdr prop))) | |
7112 | (setq pos posend))) | |
83261a2f | 7113 | nil) ; Do not iterate |
5bd52f0e RS |
7114 | |
7115 | (defun cperl-update-syntaxification (from to) | |
7116 | (if (and cperl-use-syntax-table-text-property | |
7117 | cperl-syntaxify-by-font-lock | |
7118 | (or (null cperl-syntax-done-to) | |
7119 | (< cperl-syntax-done-to to))) | |
7120 | (progn | |
7121 | (save-excursion | |
7122 | (goto-char from) | |
7123 | (cperl-fontify-syntaxically to))))) | |
7124 | ||
5c8b7eaf | 7125 | (defvar cperl-version |
83261a2f | 7126 | (let ((v "Revision: 4.35")) |
5bd52f0e RS |
7127 | (string-match ":\\s *\\([0-9.]+\\)" v) |
7128 | (substring v (match-beginning 1) (match-end 1))) | |
7129 | "Version of IZ-supported CPerl package this file is based on.") | |
7130 | ||
f83d2997 KH |
7131 | (provide 'cperl-mode) |
7132 | ||
7133 | ;;; cperl-mode.el ends here |