# encoding: ascii
# api: powershell
# title: Del. TrailingBlank (ISE)
# description: This function is intended to be uses as ISE add on.
# version: 0.1
# type: function
# license: CC0
# function: Delete-TrailingBlanks
# x-poshcode-id: 1626
# x-archived: 2010-02-26T12:40:11
#
# When working with ISE multiline texts take care of correct `r`n handling.
# Finding the regular expression was a bit tricky.
# Compare the different versions for yourself
#
function Delete-TrailingBlanks
{
$editor = $psISE.CurrentFile.Editor
$caretLine = $editor.CaretLine
# First trial. Works.
# $newText = @()
# foreach ( $line in $editor.Text.Split("`n") )
# {
# $newText += $line -replace ("\s+$", "")
# }
# $editor.Text = [String]::Join("`n", $newText)
# 2nd trial, but deletes empty lines too \s matches `r and `n
# $editor.Text = $editor.Text -replace '(?m)\s*$', ''
# 3rd working again, but doesn't it look like perl ?
# $editor.Text = $editor.Text -replace '(?m)[ \t]+(?:\r?)$', ''
# the solution is so simple
$editor.Text = $editor.Text -replace '(?m)\s*?$', ''
$editor.SetCaretPosition($caretLine, 1)
}