# -*- coding: utf-8 -*-
"""
tube.expand
~~~~~~~~~~~

This module defines various utilities for expanding special placeholders
inside commands.
"""

import re


def cr(rawstr):
    """To replace the character sequence `<CR>` into a newline.

    If `<CR>` is prepended with a backslash, the replacement is not performed.
    """
    return re.sub("(?<!\\\\)<CR>", "\n", rawstr, flags=re.IGNORECASE)


def character(rawstr, target, repl):
    """To replace the character `target` with `repl` in `rawstr`.

    If `target` is prepended with a backslash, the replacement is not performed.
    """
    out = re.sub("(?<!\\\\){0}".format(target), repl, rawstr)
    return re.sub("\\{0}".format(target), target, out)

@@ -325,30 +325,29 @@
 %22%22%22%0A    
-return
+out =
  re.sub(
 %22(?%3C!%5C%5C%5C
@@ -338,25 +338,24 @@
  re.sub(
+r
 %22(?%3C!%5C%5C
-%5C%5C
 )%3CCR%3E%22, 
@@ -389,16 +389,80 @@
 RECASE)%0A
+    return re.sub(r%22%5C%5C(%3Ccr%3E)%22, %22%5C%5C1%22, out, flags=re.IGNORECASE)%0A
 %0A%0Adef ch

Fix "<CR>" expansion