perl-mode.el 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. ;;; perl-mode.el --- Perl code editing commands for GNU Emacs
  2. ;; Copyright (C) 1990, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: William F. Mann
  4. ;; Maintainer: FSF
  5. ;; Adapted-By: ESR
  6. ;; Keywords: languages
  7. ;; Adapted from C code editing commands 'c-mode.el', Copyright 1987 by the
  8. ;; Free Software Foundation, under terms of its General Public License.
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; To enter perl-mode automatically, add (autoload 'perl-mode "perl-mode")
  22. ;; to your .emacs file and change the first line of your perl script to:
  23. ;; #!/usr/bin/perl -- # -*-Perl-*-
  24. ;; With arguments to perl:
  25. ;; #!/usr/bin/perl -P- # -*-Perl-*-
  26. ;; To handle files included with do 'filename.pl';, add something like
  27. ;; (setq auto-mode-alist (append (list (cons "\\.pl\\'" 'perl-mode))
  28. ;; auto-mode-alist))
  29. ;; to your .emacs file; otherwise the .pl suffix defaults to prolog-mode.
  30. ;; This code is based on the 18.53 version c-mode.el, with extensive
  31. ;; rewriting. Most of the features of c-mode survived intact.
  32. ;; I added a new feature which adds functionality to TAB; it is controlled
  33. ;; by the variable perl-tab-to-comment. With it enabled, TAB does the
  34. ;; first thing it can from the following list: change the indentation;
  35. ;; move past leading white space; delete an empty comment; reindent a
  36. ;; comment; move to end of line; create an empty comment; tell you that
  37. ;; the line ends in a quoted string, or has a # which should be a \#.
  38. ;; If your machine is slow, you may want to remove some of the bindings
  39. ;; to perl-electric-terminator. I changed the indenting defaults to be
  40. ;; what Larry Wall uses in perl/lib, but left in all the options.
  41. ;; I also tuned a few things: comments and labels starting in column
  42. ;; zero are left there by perl-indent-exp; perl-beginning-of-function
  43. ;; goes back to the first open brace/paren in column zero, the open brace
  44. ;; in 'sub ... {', or the equal sign in 'format ... ='; perl-indent-exp
  45. ;; (meta-^q) indents from the current line through the close of the next
  46. ;; brace/paren, so you don't need to start exactly at a brace or paren.
  47. ;; It may be good style to put a set of redundant braces around your
  48. ;; main program. This will let you reindent it with meta-^q.
  49. ;; Known problems (these are all caused by limitations in the Emacs Lisp
  50. ;; parsing routine (parse-partial-sexp), which was not designed for such
  51. ;; a rich language; writing a more suitable parser would be a big job):
  52. ;; 2) The globbing syntax <pattern> is not recognized, so special
  53. ;; characters in the pattern string must be backslashed.
  54. ;; 3) The << quoting operators are not recognized; see below.
  55. ;; 5) To make '$' work correctly, $' is not recognized as a variable.
  56. ;; Use "$'" or $POSTMATCH instead.
  57. ;;
  58. ;; If you don't use font-lock, additional problems will appear:
  59. ;; 1) Regular expression delimiters do not act as quotes, so special
  60. ;; characters such as `'"#:;[](){} may need to be backslashed
  61. ;; in regular expressions and in both parts of s/// and tr///.
  62. ;; 4) The q and qq quoting operators are not recognized; see below.
  63. ;; 5) To make variables such a $' and $#array work, perl-mode treats
  64. ;; $ just like backslash, so '$' is not treated correctly.
  65. ;; 6) Unfortunately, treating $ like \ makes ${var} be treated as an
  66. ;; unmatched }. See below.
  67. ;; 7) When ' (quote) is used as a package name separator, perl-mode
  68. ;; doesn't understand, and thinks it is seeing a quoted string.
  69. ;; Here are some ugly tricks to bypass some of these problems: the perl
  70. ;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
  71. ;; but will trick perl-mode into starting a quoted string, which
  72. ;; can be ended with another /`/. Assuming you have no embedded
  73. ;; back-ticks, this can used to help solve problem 3:
  74. ;;
  75. ;; /`/; $ugly = q?"'$?; /`/;
  76. ;;
  77. ;; The same trick can be used for problem 6 as in:
  78. ;; /{/; while (<${glob_me}>)
  79. ;; but a simpler solution is to add a space between the $ and the {:
  80. ;; while (<$ {glob_me}>)
  81. ;;
  82. ;; Problem 7 is even worse, but this 'fix' does work :-(
  83. ;; $DB'stop#'
  84. ;; [$DB'line#'
  85. ;; ] =~ s/;9$//;
  86. ;;; Code:
  87. (eval-when-compile (require 'cl))
  88. (defvar font-lock-comment-face)
  89. (defvar font-lock-doc-face)
  90. (defvar font-lock-string-face)
  91. (defgroup perl nil
  92. "Major mode for editing Perl code."
  93. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  94. :prefix "perl-"
  95. :group 'languages)
  96. (defvar perl-mode-abbrev-table nil
  97. "Abbrev table in use in perl-mode buffers.")
  98. (define-abbrev-table 'perl-mode-abbrev-table ())
  99. (defvar perl-mode-map
  100. (let ((map (make-sparse-keymap)))
  101. (define-key map "{" 'perl-electric-terminator)
  102. (define-key map "}" 'perl-electric-terminator)
  103. (define-key map ";" 'perl-electric-terminator)
  104. (define-key map ":" 'perl-electric-terminator)
  105. (define-key map "\e\C-a" 'perl-beginning-of-function)
  106. (define-key map "\e\C-e" 'perl-end-of-function)
  107. (define-key map "\e\C-h" 'perl-mark-function)
  108. (define-key map "\e\C-q" 'perl-indent-exp)
  109. (define-key map "\177" 'backward-delete-char-untabify)
  110. (define-key map "\t" 'perl-indent-command)
  111. map)
  112. "Keymap used in Perl mode.")
  113. (autoload 'c-macro-expand "cmacexp"
  114. "Display the result of expanding all C macros occurring in the region.
  115. The expansion is entirely correct because it uses the C preprocessor."
  116. t)
  117. (defvar perl-mode-syntax-table
  118. (let ((st (make-syntax-table (standard-syntax-table))))
  119. (modify-syntax-entry ?\n ">" st)
  120. (modify-syntax-entry ?# "<" st)
  121. ;; `$' is also a prefix char so I was tempted to say "/ p",
  122. ;; but the `p' thingy basically overrides the `/' :-( --stef
  123. (modify-syntax-entry ?$ "/" st)
  124. (modify-syntax-entry ?% ". p" st)
  125. (modify-syntax-entry ?@ ". p" st)
  126. (modify-syntax-entry ?& "." st)
  127. (modify-syntax-entry ?\' "\"" st)
  128. (modify-syntax-entry ?* "." st)
  129. (modify-syntax-entry ?+ "." st)
  130. (modify-syntax-entry ?- "." st)
  131. (modify-syntax-entry ?/ "." st)
  132. (modify-syntax-entry ?< "." st)
  133. (modify-syntax-entry ?= "." st)
  134. (modify-syntax-entry ?> "." st)
  135. (modify-syntax-entry ?\\ "\\" st)
  136. (modify-syntax-entry ?` "\"" st)
  137. (modify-syntax-entry ?| "." st)
  138. st)
  139. "Syntax table in use in `perl-mode' buffers.")
  140. (defvar perl-imenu-generic-expression
  141. '(;; Functions
  142. (nil "^sub\\s-+\\([-A-Za-z0-9+_:]+\\)" 1)
  143. ;;Variables
  144. ("Variables" "^\\(?:my\\|our\\)\\s-+\\([$@%][-A-Za-z0-9+_:]+\\)\\s-*=" 1)
  145. ("Packages" "^package\\s-+\\([-A-Za-z0-9+_:]+\\);" 1)
  146. ("Doc sections" "^=head[0-9][ \t]+\\(.*\\)" 1))
  147. "Imenu generic expression for Perl mode. See `imenu-generic-expression'.")
  148. ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and
  149. ;; Jim Campbell <jec@murzim.ca.boeing.com>.
  150. (defconst perl-font-lock-keywords-1
  151. '(;; What is this for?
  152. ;;("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
  153. ;;
  154. ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'.
  155. ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea.
  156. ;; ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
  157. ;; ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
  158. ;; ("^#[ \t]*if\\>"
  159. ;; ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
  160. ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t)))
  161. ;; ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
  162. ;; (1 font-lock-constant-face) (2 font-lock-variable-name-face nil t))
  163. ;;
  164. ;; Fontify function and package names in declarations.
  165. ("\\<\\(package\\|sub\\)\\>[ \t]*\\(\\sw+\\)?"
  166. (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  167. ("\\<\\(import\\|no\\|require\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
  168. (1 font-lock-keyword-face) (2 font-lock-constant-face nil t)))
  169. "Subdued level highlighting for Perl mode.")
  170. (defconst perl-font-lock-keywords-2
  171. (append perl-font-lock-keywords-1
  172. (list
  173. ;;
  174. ;; Fontify keywords, except those fontified otherwise.
  175. (concat "\\<"
  176. (regexp-opt '("if" "until" "while" "elsif" "else" "unless"
  177. "do" "dump" "for" "foreach" "exit" "die"
  178. "BEGIN" "END" "return" "exec" "eval") t)
  179. "\\>")
  180. ;;
  181. ;; Fontify local and my keywords as types.
  182. '("\\<\\(local\\|my\\)\\>" . font-lock-type-face)
  183. ;;
  184. ;; Fontify function, variable and file name references.
  185. '("&\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-function-name-face)
  186. ;; Additionally underline non-scalar variables. Maybe this is a bad idea.
  187. ;;'("[$@%*][#{]?\\(\\sw+\\)" 1 font-lock-variable-name-face)
  188. '("[$*]{?\\(\\sw+\\(::\\sw+\\)*\\)" 1 font-lock-variable-name-face)
  189. '("\\([@%]\\|\\$#\\)\\(\\sw+\\(::\\sw+\\)*\\)"
  190. (2 (cons font-lock-variable-name-face '(underline))))
  191. '("<\\(\\sw+\\)>" 1 font-lock-constant-face)
  192. ;;
  193. ;; Fontify keywords with/and labels as we do in `c++-font-lock-keywords'.
  194. '("\\<\\(continue\\|goto\\|last\\|next\\|redo\\)\\>[ \t]*\\(\\sw+\\)?"
  195. (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
  196. '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)))
  197. "Gaudy level highlighting for Perl mode.")
  198. (defvar perl-font-lock-keywords perl-font-lock-keywords-1
  199. "Default expressions to highlight in Perl mode.")
  200. (defvar perl-quote-like-pairs
  201. '((?\( . ?\)) (?\[ . ?\]) (?\{ . ?\}) (?\< . ?\>)))
  202. ;; FIXME: handle here-docs and regexps.
  203. ;; <<EOF <<"EOF" <<'EOF' (no space)
  204. ;; see `man perlop'
  205. ;; ?...?
  206. ;; /.../
  207. ;; m [...]
  208. ;; m /.../
  209. ;; q /.../ = '...'
  210. ;; qq /.../ = "..."
  211. ;; qx /.../ = `...`
  212. ;; qr /.../ = precompiled regexp =~=~ m/.../
  213. ;; qw /.../
  214. ;; s /.../.../
  215. ;; s <...> /.../
  216. ;; s '...'...'
  217. ;; tr /.../.../
  218. ;; y /.../.../
  219. ;;
  220. ;; <file*glob>
  221. (defun perl-syntax-propertize-function (start end)
  222. (let ((case-fold-search nil))
  223. (goto-char start)
  224. (perl-syntax-propertize-special-constructs end)
  225. ;; TODO: here-documents ("<<\\(\\sw\\|['\"]\\)")
  226. (funcall
  227. (syntax-propertize-rules
  228. ;; Turn POD into b-style comments. Place the cut rule first since it's
  229. ;; more specific.
  230. ("^=cut\\>.*\\(\n\\)" (1 "> b"))
  231. ("^\\(=\\)\\sw" (1 "< b"))
  232. ;; Catch ${ so that ${var} doesn't screw up indentation.
  233. ;; This also catches $' to handle 'foo$', although it should really
  234. ;; check that it occurs inside a '..' string.
  235. ("\\(\\$\\)[{']" (1 ". p"))
  236. ;; Handle funny names like $DB'stop.
  237. ("\\$ ?{?^?[_a-zA-Z][_a-zA-Z0-9]*\\('\\)[_a-zA-Z]" (1 "_"))
  238. ;; format statements
  239. ("^[ \t]*format.*=[ \t]*\\(\n\\)"
  240. (1 (prog1 "\"" (perl-syntax-propertize-special-constructs end))))
  241. ;; Funny things in `sub' arg-specs like `sub myfun ($)' or `sub ($)'.
  242. ;; Be careful not to match "sub { (...) ... }".
  243. ("\\<sub\\(?:[[:space:]]+[^{}[:punct:][:space:]]+\\)?[[:space:]]*(\\([^)]+\\))"
  244. (1 "."))
  245. ;; Turn __DATA__ trailer into a comment.
  246. ("^\\(_\\)_\\(?:DATA\\|END\\)__[ \t]*\\(?:\\(\n\\)#.-\\*-.*perl.*-\\*-\\|\n.*\\)"
  247. (1 "< c") (2 "> c")
  248. (0 (ignore (put-text-property (match-beginning 0) (match-end 0)
  249. 'syntax-multiline t))))
  250. ;; Regexp and funny quotes. Distinguishing a / that starts a regexp
  251. ;; match from the division operator is ...interesting.
  252. ;; Basically, / is a regexp match if it's preceded by an infix operator
  253. ;; (or some similar separator), or by one of the special keywords
  254. ;; corresponding to builtin functions that can take their first arg
  255. ;; without parentheses. Of course, that presume we're looking at the
  256. ;; *opening* slash. We can afford to mis-match the closing ones
  257. ;; here, because they will be re-treated separately later in
  258. ;; perl-font-lock-special-syntactic-constructs.
  259. ((concat "\\(?:\\(?:^\\|[^$@&%[:word:]]\\)"
  260. (regexp-opt '("split" "if" "unless" "until" "while" "split"
  261. "grep" "map" "not" "or" "and"))
  262. "\\|[?:.,;=!~({[]\\|\\(^\\)\\)[ \t\n]*\\(/\\)")
  263. (2 (ignore
  264. (if (and (match-end 1) ; / at BOL.
  265. (save-excursion
  266. (goto-char (match-end 1))
  267. (forward-comment (- (point-max)))
  268. (put-text-property (point) (match-end 2)
  269. 'syntax-multiline t)
  270. (not (memq (char-before)
  271. '(?? ?: ?. ?, ?\; ?= ?! ?~ ?\( ?\[)))))
  272. nil ;; A division sign instead of a regexp-match.
  273. (put-text-property (match-beginning 2) (match-end 2)
  274. 'syntax-table (string-to-syntax "\""))
  275. (perl-syntax-propertize-special-constructs end)))))
  276. ("\\(^\\|[?:.,;=!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\(?:\\([^])}>= \n\t]\\)\\|\\(?3:=\\)[^>]\\)"
  277. ;; Nasty cases:
  278. ;; /foo/m $a->m $#m $m @m %m
  279. ;; \s (appears often in regexps).
  280. ;; -s file
  281. ;; y => 3
  282. ;; sub tr {...}
  283. (3 (ignore
  284. (if (save-excursion (goto-char (match-beginning 0))
  285. (forward-word -1)
  286. (looking-at-p "sub[ \t\n]"))
  287. ;; This is defining a function.
  288. nil
  289. (put-text-property (match-beginning 3) (match-end 3)
  290. 'syntax-table
  291. (if (assoc (char-after (match-beginning 3))
  292. perl-quote-like-pairs)
  293. (string-to-syntax "|")
  294. (string-to-syntax "\"")))
  295. (perl-syntax-propertize-special-constructs end))))))
  296. (point) end)))
  297. (defvar perl-empty-syntax-table
  298. (let ((st (copy-syntax-table)))
  299. ;; Make all chars be of punctuation syntax.
  300. (dotimes (i 256) (aset st i '(1)))
  301. (modify-syntax-entry ?\\ "\\" st)
  302. st)
  303. "Syntax table used internally for processing quote-like operators.")
  304. (defun perl-quote-syntax-table (char)
  305. (let ((close (cdr (assq char perl-quote-like-pairs)))
  306. (st (copy-syntax-table perl-empty-syntax-table)))
  307. (if (not close)
  308. (modify-syntax-entry char "\"" st)
  309. (modify-syntax-entry char "(" st)
  310. (modify-syntax-entry close ")" st))
  311. st))
  312. (defun perl-syntax-propertize-special-constructs (limit)
  313. "Propertize special constructs like regexps and formats."
  314. (let ((state (syntax-ppss))
  315. char)
  316. (cond
  317. ((or (null (setq char (nth 3 state)))
  318. (and (characterp char) (eq (char-syntax (nth 3 state)) ?\")))
  319. ;; Normal text, or comment, or docstring, or normal string.
  320. nil)
  321. ((eq (nth 3 state) ?\n)
  322. ;; A `format' command.
  323. (when (re-search-forward "^\\s *\\.\\s *\n" limit 'move)
  324. (put-text-property (1- (point)) (point)
  325. 'syntax-table (string-to-syntax "\""))))
  326. (t
  327. ;; This is regexp like quote thingy.
  328. (setq char (char-after (nth 8 state)))
  329. (let ((startpos (point))
  330. (twoargs (save-excursion
  331. (goto-char (nth 8 state))
  332. (skip-syntax-backward " ")
  333. (skip-syntax-backward "w")
  334. (member (buffer-substring
  335. (point) (progn (forward-word 1) (point)))
  336. '("tr" "s" "y"))))
  337. (close (cdr (assq char perl-quote-like-pairs)))
  338. (st (perl-quote-syntax-table char)))
  339. (when (with-syntax-table st
  340. (if close
  341. ;; For paired delimiters, Perl allows nesting them, but
  342. ;; since we treat them as strings, Emacs does not count
  343. ;; those delimiters in `state', so we don't know how deep
  344. ;; we are: we have to go back to the beginning of this
  345. ;; "string" and count from there.
  346. (condition-case nil
  347. (progn
  348. ;; Start after the first char since it doesn't have
  349. ;; paren-syntax (an alternative would be to let-bind
  350. ;; parse-sexp-lookup-properties).
  351. (goto-char (1+ (nth 8 state)))
  352. (up-list 1)
  353. t)
  354. ;; In case of error, make sure we don't move backward.
  355. (scan-error (goto-char startpos) nil))
  356. (not (or (nth 8 (parse-partial-sexp
  357. ;; Since we don't know if point is within
  358. ;; the first or the scond arg, we have to
  359. ;; start from the beginning.
  360. (if twoargs (1+ (nth 8 state)) (point))
  361. limit nil nil state 'syntax-table))
  362. ;; If we have a self-paired opener and a twoargs
  363. ;; command, the form is s/../../ so we have to skip
  364. ;; a second time.
  365. ;; In the case of s{...}{...}, we only handle the
  366. ;; first part here and the next below.
  367. (when (and twoargs (not close))
  368. (nth 8 (parse-partial-sexp
  369. (point) limit
  370. nil nil state 'syntax-table)))))))
  371. ;; Point is now right after the arg(s).
  372. (when (eq (char-before (1- (point))) ?$)
  373. (put-text-property (- (point) 2) (1- (point))
  374. 'syntax-table '(1)))
  375. (put-text-property (1- (point)) (point)
  376. 'syntax-table
  377. (if close
  378. (string-to-syntax "|")
  379. (string-to-syntax "\"")))
  380. ;; If we have two args with a non-self-paired starter (e.g.
  381. ;; s{...}{...}) we're right after the first arg, so we still have to
  382. ;; handle the second part.
  383. (when (and twoargs close)
  384. ;; Skip whitespace and make sure that font-lock will
  385. ;; refontify the second part in the proper context.
  386. (put-text-property
  387. (point) (progn (forward-comment (point-max)) (point))
  388. 'syntax-multiline t)
  389. ;;
  390. (when (< (point) limit)
  391. (put-text-property (point) (1+ (point))
  392. 'syntax-table
  393. (if (assoc (char-after)
  394. perl-quote-like-pairs)
  395. ;; Put an `e' in the cdr to mark this
  396. ;; char as "second arg starter".
  397. (string-to-syntax "|e")
  398. (string-to-syntax "\"e")))
  399. (forward-char 1)
  400. ;; Re-use perl-syntax-propertize-special-constructs to handle the
  401. ;; second part (the first delimiter of second part can't be
  402. ;; preceded by "s" or "tr" or "y", so it will not be considered
  403. ;; as twoarg).
  404. (perl-syntax-propertize-special-constructs limit)))))))))
  405. (defun perl-font-lock-syntactic-face-function (state)
  406. (cond
  407. ((and (nth 3 state)
  408. (eq ?e (cdr-safe (get-text-property (nth 8 state) 'syntax-table)))
  409. ;; This is a second-arg of s{..}{...} form; let's check if this second
  410. ;; arg is executable code rather than a string. For that, we need to
  411. ;; look for an "e" after this second arg, so we have to hunt for the
  412. ;; end of the arg. Depending on whether the whole arg has already
  413. ;; been syntax-propertized or not, the end-char will have different
  414. ;; syntaxes, so let's ignore syntax-properties temporarily so we can
  415. ;; pretend it has not been syntax-propertized yet.
  416. (let* ((parse-sexp-lookup-properties nil)
  417. (char (char-after (nth 8 state)))
  418. (paired (assq char perl-quote-like-pairs)))
  419. (with-syntax-table (perl-quote-syntax-table char)
  420. (save-excursion
  421. (if (not paired)
  422. (parse-partial-sexp (point) (point-max)
  423. nil nil state 'syntax-table)
  424. (condition-case nil
  425. (progn
  426. (goto-char (1+ (nth 8 state)))
  427. (up-list 1))
  428. (scan-error (goto-char (point-max)))))
  429. (put-text-property (nth 8 state) (point)
  430. 'jit-lock-defer-multiline t)
  431. (looking-at "[ \t]*\\sw*e")))))
  432. nil)
  433. (t (funcall (default-value 'font-lock-syntactic-face-function) state))))
  434. (defcustom perl-indent-level 4
  435. "*Indentation of Perl statements with respect to containing block."
  436. :type 'integer
  437. :group 'perl)
  438. ;; Is is not unusual to put both things like perl-indent-level and
  439. ;; cperl-indent-level in the local variable section of a file. If only
  440. ;; one of perl-mode and cperl-mode is in use, a warning will be issued
  441. ;; about the variable. Autoload these here, so that no warning is
  442. ;; issued when using either perl-mode or cperl-mode.
  443. ;;;###autoload(put 'perl-indent-level 'safe-local-variable 'integerp)
  444. ;;;###autoload(put 'perl-continued-statement-offset 'safe-local-variable 'integerp)
  445. ;;;###autoload(put 'perl-continued-brace-offset 'safe-local-variable 'integerp)
  446. ;;;###autoload(put 'perl-brace-offset 'safe-local-variable 'integerp)
  447. ;;;###autoload(put 'perl-brace-imaginary-offset 'safe-local-variable 'integerp)
  448. ;;;###autoload(put 'perl-label-offset 'safe-local-variable 'integerp)
  449. (defcustom perl-continued-statement-offset 4
  450. "*Extra indent for lines not starting new statements."
  451. :type 'integer
  452. :group 'perl)
  453. (defcustom perl-continued-brace-offset -4
  454. "*Extra indent for substatements that start with open-braces.
  455. This is in addition to `perl-continued-statement-offset'."
  456. :type 'integer
  457. :group 'perl)
  458. (defcustom perl-brace-offset 0
  459. "*Extra indentation for braces, compared with other text in same context."
  460. :type 'integer
  461. :group 'perl)
  462. (defcustom perl-brace-imaginary-offset 0
  463. "*Imagined indentation of an open brace that actually follows a statement."
  464. :type 'integer
  465. :group 'perl)
  466. (defcustom perl-label-offset -2
  467. "*Offset of Perl label lines relative to usual indentation."
  468. :type 'integer
  469. :group 'perl)
  470. (defcustom perl-indent-continued-arguments nil
  471. "*If non-nil offset of argument lines relative to usual indentation.
  472. If nil, continued arguments are aligned with the first argument."
  473. :type '(choice integer (const nil))
  474. :group 'perl)
  475. (defcustom perl-tab-always-indent tab-always-indent
  476. "Non-nil means TAB in Perl mode always indents the current line.
  477. Otherwise it inserts a tab character if you type it past the first
  478. nonwhite character on the line."
  479. :type 'boolean
  480. :group 'perl)
  481. ;; I changed the default to nil for consistency with general Emacs
  482. ;; conventions -- rms.
  483. (defcustom perl-tab-to-comment nil
  484. "*Non-nil means TAB moves to eol or makes a comment in some cases.
  485. For lines which don't need indenting, TAB either indents an
  486. existing comment, moves to end-of-line, or if at end-of-line already,
  487. create a new comment."
  488. :type 'boolean
  489. :group 'perl)
  490. (defcustom perl-nochange ";?#\\|\f\\|\\s(\\|\\(\\w\\|\\s_\\)+:[^:]"
  491. "*Lines starting with this regular expression are not auto-indented."
  492. :type 'regexp
  493. :group 'perl)
  494. ;; Outline support
  495. (defvar perl-outline-regexp
  496. (concat (mapconcat 'cadr perl-imenu-generic-expression "\\|")
  497. "\\|^=cut\\>"))
  498. (defun perl-outline-level ()
  499. (cond
  500. ((looking-at "package\\s-") 0)
  501. ((looking-at "sub\\s-") 1)
  502. ((looking-at "=head[0-9]") (- (char-before (match-end 0)) ?0))
  503. ((looking-at "=cut") 1)
  504. (t 3)))
  505. (defvar perl-mode-hook nil
  506. "Normal hook to run when entering Perl mode.")
  507. ;;;###autoload
  508. (define-derived-mode perl-mode prog-mode "Perl"
  509. "Major mode for editing Perl code.
  510. Expression and list commands understand all Perl brackets.
  511. Tab indents for Perl code.
  512. Comments are delimited with # ... \\n.
  513. Paragraphs are separated by blank lines only.
  514. Delete converts tabs to spaces as it moves back.
  515. \\{perl-mode-map}
  516. Variables controlling indentation style:
  517. `perl-tab-always-indent'
  518. Non-nil means TAB in Perl mode should always indent the current line,
  519. regardless of where in the line point is when the TAB command is used.
  520. `perl-tab-to-comment'
  521. Non-nil means that for lines which don't need indenting, TAB will
  522. either delete an empty comment, indent an existing comment, move
  523. to end-of-line, or if at end-of-line already, create a new comment.
  524. `perl-nochange'
  525. Lines starting with this regular expression are not auto-indented.
  526. `perl-indent-level'
  527. Indentation of Perl statements within surrounding block.
  528. The surrounding block's indentation is the indentation
  529. of the line on which the open-brace appears.
  530. `perl-continued-statement-offset'
  531. Extra indentation given to a substatement, such as the
  532. then-clause of an if or body of a while.
  533. `perl-continued-brace-offset'
  534. Extra indentation given to a brace that starts a substatement.
  535. This is in addition to `perl-continued-statement-offset'.
  536. `perl-brace-offset'
  537. Extra indentation for line if it starts with an open brace.
  538. `perl-brace-imaginary-offset'
  539. An open brace following other text is treated as if it were
  540. this far to the right of the start of its line.
  541. `perl-label-offset'
  542. Extra indentation for line that is a label.
  543. `perl-indent-continued-arguments'
  544. Offset of argument lines relative to usual indentation.
  545. Various indentation styles: K&R BSD BLK GNU LW
  546. perl-indent-level 5 8 0 2 4
  547. perl-continued-statement-offset 5 8 4 2 4
  548. perl-continued-brace-offset 0 0 0 0 -4
  549. perl-brace-offset -5 -8 0 0 0
  550. perl-brace-imaginary-offset 0 0 4 0 0
  551. perl-label-offset -5 -8 -2 -2 -2
  552. Turning on Perl mode runs the normal hook `perl-mode-hook'."
  553. :abbrev-table perl-mode-abbrev-table
  554. (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
  555. (set (make-local-variable 'paragraph-separate) paragraph-start)
  556. (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  557. (set (make-local-variable 'indent-line-function) #'perl-indent-line)
  558. (set (make-local-variable 'comment-start) "# ")
  559. (set (make-local-variable 'comment-end) "")
  560. (set (make-local-variable 'comment-start-skip) "\\(^\\|\\s-\\);?#+ *")
  561. (set (make-local-variable 'comment-indent-function) #'perl-comment-indent)
  562. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  563. ;; Tell font-lock.el how to handle Perl.
  564. (setq font-lock-defaults '((perl-font-lock-keywords
  565. perl-font-lock-keywords-1
  566. perl-font-lock-keywords-2)
  567. nil nil ((?\_ . "w")) nil
  568. (font-lock-syntactic-face-function
  569. . perl-font-lock-syntactic-face-function)))
  570. (set (make-local-variable 'syntax-propertize-function)
  571. #'perl-syntax-propertize-function)
  572. (add-hook 'syntax-propertize-extend-region-functions
  573. #'syntax-propertize-multiline 'append 'local)
  574. ;; Tell imenu how to handle Perl.
  575. (set (make-local-variable 'imenu-generic-expression)
  576. perl-imenu-generic-expression)
  577. (setq imenu-case-fold-search nil)
  578. ;; Setup outline-minor-mode.
  579. (set (make-local-variable 'outline-regexp) perl-outline-regexp)
  580. (set (make-local-variable 'outline-level) 'perl-outline-level))
  581. ;; This is used by indent-for-comment
  582. ;; to decide how much to indent a comment in Perl code
  583. ;; based on its context.
  584. (defun perl-comment-indent ()
  585. (if (and (bolp) (not (eolp)))
  586. 0 ;Existing comment at bol stays there.
  587. comment-column))
  588. (defalias 'electric-perl-terminator 'perl-electric-terminator)
  589. (defun perl-electric-terminator (arg)
  590. "Insert character and maybe adjust indentation.
  591. If at end-of-line, and not in a comment or a quote, correct the indentation."
  592. (interactive "P")
  593. (let ((insertpos (point)))
  594. (and (not arg) ; decide whether to indent
  595. (eolp)
  596. (save-excursion
  597. (beginning-of-line)
  598. (and (not ; eliminate comments quickly
  599. (and comment-start-skip
  600. (re-search-forward comment-start-skip insertpos t)) )
  601. (or (/= last-command-event ?:)
  602. ;; Colon is special only after a label ....
  603. (looking-at "\\s-*\\(\\w\\|\\s_\\)+$"))
  604. (let ((pps (parse-partial-sexp
  605. (perl-beginning-of-function) insertpos)))
  606. (not (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))
  607. (progn ; must insert, indent, delete
  608. (insert-char last-command-event 1)
  609. (perl-indent-line)
  610. (delete-char -1))))
  611. (self-insert-command (prefix-numeric-value arg)))
  612. ;; not used anymore, but may be useful someday:
  613. ;;(defun perl-inside-parens-p ()
  614. ;; (condition-case ()
  615. ;; (save-excursion
  616. ;; (save-restriction
  617. ;; (narrow-to-region (point)
  618. ;; (perl-beginning-of-function))
  619. ;; (goto-char (point-max))
  620. ;; (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
  621. ;; (error nil)))
  622. (defun perl-indent-command (&optional arg)
  623. "Indent Perl code in the active region or current line.
  624. In Transient Mark mode, when the region is active, reindent the region.
  625. Otherwise, with a prefix argument, reindent the current line
  626. unconditionally.
  627. Otherwise, if `perl-tab-always-indent' is nil and point is not in
  628. the indentation area at the beginning of the line, insert a tab.
  629. Otherwise, indent the current line. If point was within the
  630. indentation area, it is moved to the end of the indentation area.
  631. If the line was already indented properly and point was not
  632. within the indentation area, and if `perl-tab-to-comment' is
  633. non-nil (the default), then do the first possible action from the
  634. following list:
  635. 1) delete an empty comment
  636. 2) move forward to start of comment, indenting if necessary
  637. 3) move forward to end of line
  638. 4) create an empty comment
  639. 5) move backward to start of comment, indenting if necessary."
  640. (interactive "P")
  641. (cond ((use-region-p) ; indent the active region
  642. (indent-region (region-beginning) (region-end)))
  643. (arg
  644. (perl-indent-line "\f")) ; just indent this line
  645. ((and (not perl-tab-always-indent)
  646. (> (current-column) (current-indentation)))
  647. (insert-tab))
  648. (t
  649. (let* ((oldpnt (point))
  650. (lsexp (progn (beginning-of-line) (point)))
  651. (bof (perl-beginning-of-function))
  652. (delta (progn
  653. (goto-char oldpnt)
  654. (perl-indent-line "\f\\|;?#" bof))))
  655. (and perl-tab-to-comment
  656. (= oldpnt (point)) ; done if point moved
  657. (if (listp delta) ; if line starts in a quoted string
  658. (setq lsexp (or (nth 2 delta) bof))
  659. (= delta 0)) ; done if indenting occurred
  660. (let ((eol (progn (end-of-line) (point)))
  661. state)
  662. (cond ((= (char-after bof) ?=)
  663. (if (= oldpnt eol)
  664. (message "In a format statement")))
  665. ((progn (setq state (parse-partial-sexp lsexp eol))
  666. (nth 3 state))
  667. (if (= oldpnt eol) ; already at eol in a string
  668. (message "In a string which starts with a %c."
  669. (nth 3 state))))
  670. ((not (nth 4 state))
  671. (if (= oldpnt eol) ; no comment, create one?
  672. (indent-for-comment)))
  673. ((progn (beginning-of-line)
  674. (and comment-start-skip
  675. (re-search-forward
  676. comment-start-skip eol 'move)))
  677. (if (eolp)
  678. (progn ; delete existing comment
  679. (goto-char (match-beginning 0))
  680. (skip-chars-backward " \t")
  681. (delete-region (point) eol))
  682. (if (or (< oldpnt (point)) (= oldpnt eol))
  683. (indent-for-comment) ; indent existing comment
  684. (end-of-line))))
  685. ((/= oldpnt eol)
  686. (end-of-line))
  687. (t
  688. (message "Use backslash to quote # characters.")
  689. (ding t)))))))))
  690. (defun perl-indent-line (&optional nochange parse-start)
  691. "Indent current line as Perl code.
  692. Return the amount the indentation
  693. changed by, or (parse-state) if line starts in a quoted string."
  694. (let ((case-fold-search nil)
  695. (pos (- (point-max) (point)))
  696. (bof (or parse-start (save-excursion (perl-beginning-of-function))))
  697. beg indent shift-amt)
  698. (beginning-of-line)
  699. (setq beg (point))
  700. (setq shift-amt
  701. (cond ((eq (char-after bof) ?=) 0)
  702. ((listp (setq indent (perl-calculate-indent bof))) indent)
  703. ((eq 'noindent indent) indent)
  704. ((looking-at (or nochange perl-nochange)) 0)
  705. (t
  706. (skip-chars-forward " \t\f")
  707. (setq indent (perl-indent-new-calculate nil indent bof))
  708. (- indent (current-column)))))
  709. (skip-chars-forward " \t\f")
  710. (if (and (numberp shift-amt) (/= 0 shift-amt))
  711. (progn (delete-region beg (point))
  712. (indent-to indent)))
  713. ;; If initial point was within line's indentation,
  714. ;; position after the indentation. Else stay at same point in text.
  715. (if (> (- (point-max) pos) (point))
  716. (goto-char (- (point-max) pos)))
  717. shift-amt))
  718. (defun perl-continuation-line-p (limit)
  719. "Move to end of previous line and return non-nil if continued."
  720. ;; Statement level. Is it a continuation or a new statement?
  721. ;; Find previous non-comment character.
  722. (perl-backward-to-noncomment)
  723. ;; Back up over label lines, since they don't
  724. ;; affect whether our line is a continuation.
  725. (while (or (eq (preceding-char) ?\,)
  726. (and (eq (preceding-char) ?:)
  727. (memq (char-syntax (char-after (- (point) 2)))
  728. '(?w ?_))))
  729. (if (eq (preceding-char) ?\,)
  730. (perl-backward-to-start-of-continued-exp limit)
  731. (beginning-of-line))
  732. (perl-backward-to-noncomment))
  733. ;; Now we get the answer.
  734. (not (memq (preceding-char) '(?\; ?\} ?\{))))
  735. (defun perl-hanging-paren-p ()
  736. "Non-nil if we are right after a hanging parenthesis-like char."
  737. (and (looking-at "[ \t]*$")
  738. (save-excursion
  739. (skip-syntax-backward " (") (not (bolp)))))
  740. (defun perl-indent-new-calculate (&optional virtual default parse-start)
  741. (or
  742. (and virtual (save-excursion (skip-chars-backward " \t") (bolp))
  743. (current-column))
  744. (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]")
  745. (max 1 (+ (or default (perl-calculate-indent parse-start))
  746. perl-label-offset)))
  747. (and (= (char-syntax (following-char)) ?\))
  748. (save-excursion
  749. (forward-char 1)
  750. (forward-sexp -1)
  751. (perl-indent-new-calculate
  752. ;; Recalculate the parsing-start, since we may have jumped
  753. ;; dangerously close (typically in the case of nested functions).
  754. 'virtual nil (save-excursion (perl-beginning-of-function)))))
  755. (and (and (= (following-char) ?{)
  756. (save-excursion (forward-char) (perl-hanging-paren-p)))
  757. (+ (or default (perl-calculate-indent parse-start))
  758. perl-brace-offset))
  759. (or default (perl-calculate-indent parse-start))))
  760. (defun perl-calculate-indent (&optional parse-start)
  761. "Return appropriate indentation for current line as Perl code.
  762. In usual case returns an integer: the column to indent to.
  763. Returns (parse-state) if line starts inside a string.
  764. Optional argument PARSE-START should be the position of `beginning-of-defun'."
  765. (save-excursion
  766. (let ((indent-point (point))
  767. (case-fold-search nil)
  768. (colon-line-end 0)
  769. state containing-sexp)
  770. (if parse-start ;used to avoid searching
  771. (goto-char parse-start)
  772. (perl-beginning-of-function))
  773. ;; We might be now looking at a local function that has nothing to
  774. ;; do with us because `indent-point' is past it. In this case
  775. ;; look further back up for another `perl-beginning-of-function'.
  776. (while (and (looking-at "{")
  777. (save-excursion
  778. (beginning-of-line)
  779. (looking-at "\\s-+sub\\>"))
  780. (> indent-point (save-excursion
  781. (condition-case nil
  782. (forward-sexp 1)
  783. (scan-error nil))
  784. (point))))
  785. (perl-beginning-of-function))
  786. (while (< (point) indent-point) ;repeat until right sexp
  787. (setq state (parse-partial-sexp (point) indent-point 0))
  788. ;; state = (depth_in_parens innermost_containing_list
  789. ;; last_complete_sexp string_terminator_or_nil inside_commentp
  790. ;; following_quotep minimum_paren-depth_this_scan)
  791. ;; Parsing stops if depth in parentheses becomes equal to third arg.
  792. (setq containing-sexp (nth 1 state)))
  793. (cond ((nth 3 state) 'noindent) ; In a quoted string?
  794. ((null containing-sexp) ; Line is at top level.
  795. (skip-chars-forward " \t\f")
  796. (if (= (following-char) ?{)
  797. 0 ; move to beginning of line if it starts a function body
  798. ;; indent a little if this is a continuation line
  799. (perl-backward-to-noncomment)
  800. (if (or (bobp)
  801. (memq (preceding-char) '(?\; ?\})))
  802. 0 perl-continued-statement-offset)))
  803. ((/= (char-after containing-sexp) ?{)
  804. ;; line is expression, not statement:
  805. ;; indent to just after the surrounding open.
  806. (goto-char (1+ containing-sexp))
  807. (if (perl-hanging-paren-p)
  808. ;; We're indenting an arg of a call like:
  809. ;; $a = foobarlongnamefun (
  810. ;; arg1
  811. ;; arg2
  812. ;; );
  813. (progn
  814. (skip-syntax-backward "(")
  815. (condition-case nil
  816. (while (save-excursion
  817. (skip-syntax-backward " ") (not (bolp)))
  818. (forward-sexp -1))
  819. (scan-error nil))
  820. (+ (current-column) perl-indent-level))
  821. (if perl-indent-continued-arguments
  822. (+ perl-indent-continued-arguments (current-indentation))
  823. (skip-chars-forward " \t")
  824. (current-column))))
  825. (t
  826. ;; Statement level. Is it a continuation or a new statement?
  827. (if (perl-continuation-line-p containing-sexp)
  828. ;; This line is continuation of preceding line's statement;
  829. ;; indent perl-continued-statement-offset more than the
  830. ;; previous line of the statement.
  831. (progn
  832. (perl-backward-to-start-of-continued-exp containing-sexp)
  833. (+ (if (save-excursion
  834. (perl-continuation-line-p containing-sexp))
  835. ;; If the continued line is itself a continuation
  836. ;; line, then align, otherwise add an offset.
  837. 0 perl-continued-statement-offset)
  838. (current-column)
  839. (if (save-excursion (goto-char indent-point)
  840. (looking-at "[ \t]*{"))
  841. perl-continued-brace-offset 0)))
  842. ;; This line starts a new statement.
  843. ;; Position at last unclosed open.
  844. (goto-char containing-sexp)
  845. (or
  846. ;; Is line first statement after an open-brace?
  847. ;; If no, find that first statement and indent like it.
  848. (save-excursion
  849. (forward-char 1)
  850. ;; Skip over comments and labels following openbrace.
  851. (while (progn
  852. (skip-chars-forward " \t\f\n")
  853. (cond ((looking-at ";?#")
  854. (forward-line 1) t)
  855. ((looking-at "\\(\\w\\|\\s_\\)+:[^:]")
  856. (setq colon-line-end (line-end-position))
  857. (search-forward ":")))))
  858. ;; The first following code counts
  859. ;; if it is before the line we want to indent.
  860. (and (< (point) indent-point)
  861. (if (> colon-line-end (point))
  862. (- (current-indentation) perl-label-offset)
  863. (current-column))))
  864. ;; If no previous statement,
  865. ;; indent it relative to line brace is on.
  866. ;; For open paren in column zero, don't let statement
  867. ;; start there too. If perl-indent-level is zero,
  868. ;; use perl-brace-offset + perl-continued-statement-offset
  869. ;; For open-braces not the first thing in a line,
  870. ;; add in perl-brace-imaginary-offset.
  871. (+ (if (and (bolp) (zerop perl-indent-level))
  872. (+ perl-brace-offset perl-continued-statement-offset)
  873. perl-indent-level)
  874. ;; Move back over whitespace before the openbrace.
  875. ;; If openbrace is not first nonwhite thing on the line,
  876. ;; add the perl-brace-imaginary-offset.
  877. (progn (skip-chars-backward " \t")
  878. (if (bolp) 0 perl-brace-imaginary-offset))
  879. ;; If the openbrace is preceded by a parenthesized exp,
  880. ;; move to the beginning of that;
  881. ;; possibly a different line
  882. (progn
  883. (if (eq (preceding-char) ?\))
  884. (forward-sexp -1))
  885. ;; Get initial indentation of the line we are on.
  886. (current-indentation))))))))))
  887. (defun perl-backward-to-noncomment ()
  888. "Move point backward to after the first non-white-space, skipping comments."
  889. (interactive)
  890. (forward-comment (- (point-max))))
  891. (defun perl-backward-to-start-of-continued-exp (lim)
  892. (if (= (preceding-char) ?\))
  893. (forward-sexp -1))
  894. (beginning-of-line)
  895. (if (<= (point) lim)
  896. (goto-char (1+ lim)))
  897. (skip-chars-forward " \t\f"))
  898. ;; note: this may be slower than the c-mode version, but I can understand it.
  899. (defalias 'indent-perl-exp 'perl-indent-exp)
  900. (defun perl-indent-exp ()
  901. "Indent each line of the Perl grouping following point."
  902. (interactive)
  903. (let* ((case-fold-search nil)
  904. (oldpnt (point-marker))
  905. (bof-mark (save-excursion
  906. (end-of-line 2)
  907. (perl-beginning-of-function)
  908. (point-marker)))
  909. eol last-mark lsexp-mark delta)
  910. (if (= (char-after (marker-position bof-mark)) ?=)
  911. (message "Can't indent a format statement")
  912. (message "Indenting Perl expression...")
  913. (setq eol (line-end-position))
  914. (save-excursion ; locate matching close paren
  915. (while (and (not (eobp)) (<= (point) eol))
  916. (parse-partial-sexp (point) (point-max) 0))
  917. (setq last-mark (point-marker)))
  918. (setq lsexp-mark bof-mark)
  919. (beginning-of-line)
  920. (while (< (point) (marker-position last-mark))
  921. (setq delta (perl-indent-line nil (marker-position bof-mark)))
  922. (if (numberp delta) ; unquoted start-of-line?
  923. (progn
  924. (if (eolp)
  925. (delete-horizontal-space))
  926. (setq lsexp-mark (point-marker))))
  927. (end-of-line)
  928. (setq eol (point))
  929. (if (nth 4 (parse-partial-sexp (marker-position lsexp-mark) eol))
  930. (progn ; line ends in a comment
  931. (beginning-of-line)
  932. (if (or (not (looking-at "\\s-*;?#"))
  933. (listp delta)
  934. (and (/= 0 delta)
  935. (= (- (current-indentation) delta) comment-column)))
  936. (if (and comment-start-skip
  937. (re-search-forward comment-start-skip eol t))
  938. (indent-for-comment))))) ; indent existing comment
  939. (forward-line 1))
  940. (goto-char (marker-position oldpnt))
  941. (message "Indenting Perl expression...done"))))
  942. (defun perl-beginning-of-function (&optional arg)
  943. "Move backward to next beginning-of-function, or as far as possible.
  944. With argument, repeat that many times; negative args move forward.
  945. Returns new value of point in all cases."
  946. (interactive "p")
  947. (or arg (setq arg 1))
  948. (if (< arg 0) (forward-char 1))
  949. (and (/= arg 0)
  950. (re-search-backward
  951. "^\\s(\\|^\\s-*sub\\b[ \t\n]*\\_<[^{]+{\\|^\\s-*format\\b[^=]*=\\|^\\."
  952. nil 'move arg)
  953. (goto-char (1- (match-end 0))))
  954. (point))
  955. ;; note: this routine is adapted directly from emacs lisp.el, end-of-defun;
  956. ;; no bugs have been removed :-)
  957. (defun perl-end-of-function (&optional arg)
  958. "Move forward to next end-of-function.
  959. The end of a function is found by moving forward from the beginning of one.
  960. With argument, repeat that many times; negative args move backward."
  961. (interactive "p")
  962. (or arg (setq arg 1))
  963. (let ((first t))
  964. (while (and (> arg 0) (< (point) (point-max)))
  965. (let ((pos (point)))
  966. (while (progn
  967. (if (and first
  968. (progn
  969. (forward-char 1)
  970. (perl-beginning-of-function 1)
  971. (not (bobp))))
  972. nil
  973. (or (bobp) (forward-char -1))
  974. (perl-beginning-of-function -1))
  975. (setq first nil)
  976. (forward-list 1)
  977. (skip-chars-forward " \t")
  978. (if (looking-at "[#\n]")
  979. (forward-line 1))
  980. (<= (point) pos))))
  981. (setq arg (1- arg)))
  982. (while (< arg 0)
  983. (let ((pos (point)))
  984. (perl-beginning-of-function 1)
  985. (forward-sexp 1)
  986. (forward-line 1)
  987. (if (>= (point) pos)
  988. (if (progn (perl-beginning-of-function 2) (not (bobp)))
  989. (progn
  990. (forward-list 1)
  991. (skip-chars-forward " \t")
  992. (if (looking-at "[#\n]")
  993. (forward-line 1)))
  994. (goto-char (point-min)))))
  995. (setq arg (1+ arg)))))
  996. (defalias 'mark-perl-function 'perl-mark-function)
  997. (defun perl-mark-function ()
  998. "Put mark at end of Perl function, point at beginning."
  999. (interactive)
  1000. (push-mark (point))
  1001. (perl-end-of-function)
  1002. (push-mark (point))
  1003. (perl-beginning-of-function)
  1004. (backward-paragraph))
  1005. (provide 'perl-mode)
  1006. ;;; perl-mode.el ends here