ISE Functions – in a Module

The module linked below adds three functions to the ISE editor:

  • Indent
  • Outdent
  • A primitive interpretation of the Xedit “All” macro

To try it out just copy the folder from here: CJW-ISE-Functions.zip  and place it under your Modules folder.  If you don’t have a Modules folder yet, see This Post on the PowerShell Team Blog, or This Post on Thomas’s Blog.

Here’s a screenshot of the “All” function:

 

As you’d expect, the Indent and Outdent functions do exactly what they say; default is to in/out-dent by 4 spaces.  Indent is mapped to Ctrl-F12 and Outdent to Ctrl-F11.

I’m never quite sure if SkyDrive is going to work, so here’s the CJW-ISE-Functions.psm1 file.  This was created with the Module Module!:

Cheers,

Chris

 

  1 ################################################################################
  2 
  3 Function Get-ISEMatchingLines {
  4 <#
  5 .Synopsis
  6     Displays lines matching the given pattern from the currently active ISE Editor window
  7 .Description
  8     This is a poor-man's-version of the Xedit "ALL" command.  All lines matching the given parameter
  9     are displayed as output
 10 
 11 .Parameter Pattern
 12 .Example
 13     Get-ISEMatchingLines "# Todo"
 14 .ReturnValue
 15     Output lines matching pattern
 16 .Link
 17 
 18 .Notes
 19  NAME:      Get-ISEMatchingLines
 20  AUTHOR:    Chris Warwick
 21  LASTEDIT:  01/05/2009 22:30:12
 22 #Requires -Version 2.0
 23 #>
 24 
 25 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
 26 
 27 Param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][String]$Pattern)
 28 
 29     Process{
 30         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
 31         $psISE.CurrentOpenedFile.Editor.Text -split "`n"|Select-String -pattern $Pattern
 32     }
 33 } 
 34 
 35 ################################################################################
 36 
 37 Function Indent-ISESelection {
 38 <#
 39 .Synopsis
 40     Indents the current editor selection by the specified number of characters
 41 .Description
 42     The editor current selection is indented by having a given number of spaces (default=4) 
 43     inserted before each line in the selection.  Note that if the selection does not start
 44     on a line-break boundary this cannot be detected and indenting will occur from the start
 45     of the selection irrespective of its actual place in the line.
 46 
 47 .Parameter Spaces
 48 .Example
 49     Indent-ISESelection 8
 50 .ReturnValue
 51     Selected lines indented
 52 .Link
 53 
 54 .Notes
 55  NAME:      Indent-ISESelection
 56  AUTHOR:    Chris Warwick
 57  LASTEDIT:  01/05/2009 22:30:12
 58 #Requires -Version 2.0
 59 #>
 60 
 61 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
 62 
 63 Param([Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false)][Int]$indent=4)
 64 
 65     Process{
 66         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
 67         $psISE.CurrentOpenedFile.Editor.InsertText(
 68             ($psISE.CurrentOpenedFile.Editor.SelectedText -split "`n"|%{$_ -Replace '^', (' '*$Indent)}) -join "`n"
 69         )
 70     }
 71 } 
 72 
 73 ################################################################################
 74 
 75 Function Outdent-ISESelection {
 76 <#
 77 .Synopsis
 78     Outdents the current editor selection by the specified number of characters
 79 .Description
 80     The editor current selection is outdented by having a given number of spaces (default=4) 
 81     removed before each line in the selection.  Note that if the selection does not start
 82     on a line-break boundary this cannot be detected and outdenting will occur from the start
 83     of the selection irrespective of its actual place in the line.
 84     
 85     The outdenting process will only remove whitespace characters from the start of each line.
 86     If whitespace characters run out then no shifting will occur
 87 
 88 .Parameter Spaces
 89 .Example
 90     Outdent-ISESelection 8
 91 .ReturnValue
 92     Selected lines outdented
 93 .Link
 94 
 95 .Notes
 96  NAME:      Outdent-ISESelection
 97  AUTHOR:    Chris Warwick
 98  LASTEDIT:  01/05/2009 22:30:12
 99 #Requires -Version 2.0
100 #>
101 
102 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
103 
104 Param([Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false)][Int]$Outdent=4)
105 
106     Process{
107         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
108         $psISE.CurrentOpenedFile.Editor.InsertText(
109             ($psISE.CurrentOpenedFile.Editor.SelectedText -split "`n"|%{$_ -Replace "^\s{0,$Outdent}"}) -join "`n"
110         )
111     }
112 } 
113 
114 ################################################################################
115             
116 $Null=$psISE.CustomMenu.Submenus.Add('Indent', {Indent-ISESelection}, 'Ctrl+F12')
117 $Null=$psISE.CustomMenu.Submenus.Add('Outdent', {Outdent-ISESelection}, 'Ctrl+F11')
118 
119 Export-ModuleMember Get-ISEMatchingLines, Indent-ISESelection, Outdent-ISESelection
120 
121 Set-Alias All Get-ISEMatchingLines -Scope Global
122 Set-Alias Indent Indent-ISESelection -Scope Global
123 Set-Alias Outdent Outdent-ISESelection -Scope Global
124 
125 ################################################################################

PowerShell ISE – Yippee!

No blog entries here for ever, I was thinking this blog would see no life until next year’s Scripting Games:-)  But then CTP3 was released and in particular, the Integrated Scripting Environment (ISE) has got my attention – because it’s scriptable (See PowerShell ISE Can Do a Lot More Than You Think).

The CTP3 Story So Far

I’ve installed CTP3 on 6 machines here at home (3xVista Desktop; 1xVista Laptop; 2xServer 2008 Domain Controllers) – all working as expected; remoting running between all the machines.

I’ve been trying to keep up with the blogs (there have been 33 posts to date on the PowerShell Team Blog alone since CTP3 was released!) and have been toying with Advanced Functions and Modules.  All very, very cool.

My PowerShell Environment (or, Why I Didn’t Use ISE)

I use (and highly recommend) PowerShell Plus (now sold by Idera http://www.idera.com/Products/PowerShell/).  Hopefully the rumoured update due soon will improve the CTP3 interoperability…  Anyway, being a PS+ fan meant I practically ignored ISE all the way through CTP2.

I fired up ISE with CTP3 and thought “same-old, same-old” until I saw Jeffery’s post – at which point I got slightly more interested.  Now this is getting good!  I’ve just written a module containing some ISE functions (to be posted shortly) and thought I’d provide some thoughts…

Where To Next, ISE?

This is a fantastic opportunity!  OK, currently the ISE object model is a little, err, on the light side.  But the potential is there.  I come from a IBM/VM background and was a huge fan of Xedit – the VM programmable editor; there was nothing that couldn’t be done with that editor and I’ve never been 100% happy about my current-fave replacement – whatever that happens to have been (currently EditPad Pro and the PS+ Editor (yeah, I’ve tried the PowerGUI one too…))

ISE could be right up there if the object model is expanded. 

One of the good things in Xedit was the “All” command; this took a pattern and displayed all the lines in the current file that matched the pattern.  All other lines were either hidden entirely or bunches of hidden lines were replaced with a “shadow line” (Set Shadow On/Off).  Global edits to the file would then operate only on the displayed lines (Set Scope Display).  This was incredibly useful (You perhaps don’t miss it until you don’t have it anymore!) – it’s been implemented in other editors (for example GVIM – and Emacs I’m sure) – if you’d like a go get the free Xedit clone by Mark Hessling (here: http://sourceforge.net/project/showfiles.php?group_id=29648) called “The Hessling Editor” or “THE” for short.

I’ve written a very simple, poor-man’s “All” command for ISE (included in the module), but it’s somewhat less than it could be … hopefully that object model will improve!

What about ISE?  Well, check the (extremely comprehensive) Xedit command set and object model here: http://hessling-editor.sourceforge.net/doc/index.html

As a first step – let us display or hide lines in the editor window:-)

Preferably, find out where Mark Hessling is and get him a job on the PowerShell Team:-))