Function: cperl-invert-if-unless-modifiers
cperl-invert-if-unless-modifiers is an interactive and byte-compiled
function defined in cperl-mode.el.gz.
Signature
(cperl-invert-if-unless-modifiers)
Documentation
Change B if A; into if (A) {B} etc if possible.
(Unfinished.)
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/cperl-mode.el.gz
(defun cperl-invert-if-unless-modifiers ()
"Change `B if A;' into `if (A) {B}' etc if possible.
\(Unfinished.)"
(interactive)
(let (A B pre-B post-B pre-if post-if pre-A post-A if-string
(w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
(and (= (char-syntax (preceding-char)) ?w)
(forward-sexp -1))
(setq pre-if (point))
(cperl-backward-to-start-of-expr)
(setq pre-B (point))
(forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
(cperl-forward-to-end-of-expr)
(setq post-A (point))
(goto-char pre-if)
(or (looking-at w-rex)
;; Find the position
(progn (goto-char post-A)
(while (and
(not (looking-at w-rex))
(> (point) pre-B))
(forward-sexp -1))
(setq pre-if (point))))
(or (looking-at w-rex)
(error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
(setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
;; First, simple part: find code boundaries
(forward-sexp 1)
(setq post-if (point))
(forward-sexp -2)
(forward-sexp 1)
(setq post-B (point))
(cperl-backward-to-start-of-expr)
(setq pre-B (point))
(setq B (buffer-substring pre-B post-B))
(goto-char pre-if)
(forward-sexp 2)
(forward-sexp -1)
;; May be after $, @, $# etc of a variable
(skip-chars-backward "$@%#")
(setq pre-A (point))
(cperl-forward-to-end-of-expr)
(setq post-A (point))
(setq A (buffer-substring pre-A post-A))
;; Now modify (from end, to not break the stuff)
(skip-chars-forward " \t;")
(delete-region pre-A (point)) ; we move to pre-A
(insert "\n" B ";\n}")
(and (looking-at "[ \t]*#") (cperl-indent-for-comment))
(delete-region pre-if post-if)
(delete-region pre-B post-B)
(goto-char pre-B)
(insert if-string " (" A ") {")
(setq post-B (point))
(if (looking-at "[ \t]+$")
(delete-horizontal-space)
(if (looking-at "[ \t]*#")
(cperl-indent-for-comment)
(just-one-space)))
(forward-line 1)
(if (looking-at "[ \t]*$")
(progn ; delete line
(delete-horizontal-space)
(delete-region (point) (1+ (point)))))
(cperl-indent-line)
(goto-char (1- post-B))
(forward-sexp 1)
(cperl-indent-line)
(goto-char pre-B)))