• About
    • About

Steve Cooper's Personal Blog

  • How to choose functions in Visual Studio with the keyboard

    September 12th, 2007

    In Visual Studio, there’s pretty good support for keys, but for me it lacks one significant feature. That is the ability to jump to a function from inside a class. That is, if you’ve got a class open the text editor, you want to see a list of functions in the class, choose one, and navigate to it, via the keyboard. You’ll be familiar with it from the two drop-down lists at the top of the code window; however, this drop-down can’t be activated by keyboard.

    Here’s a cheap and cheerful implementation of the same features. Put it into your macro module, and map the macro to a keyboard shortcut.

    Sub ChooseFunctionAndNavigateInIde()
    Try
    Dim functionIndex As New Dictionary(Of String, Integer)

    Dim doc As Document = DTE.ActiveWindow.Document
    For Each elem As CodeElement In doc.ProjectItem.FileCodeModel.CodeElements
    BuildCodeElements(elem, functionIndex, 0)
    Next

    Dim result As Integer = FChooser.SelectItem(functionIndex)
    If (result = -1) Then
    ‘ user cancelled
    Return
    Else
    Dim sel As TextSelection = CType(doc.Selection, TextSelection)
    sel.GotoLine(result, True)
    End If
    Catch ex As Exception
    End Try
    End Sub

    Private Sub BuildCodeElements(ByVal elem As CodeElement, ByVal functionIndex As Dictionary(Of String, Integer), ByVal depth As Integer)

    Try
    Select Case elem.Kind
    Case vsCMElement.vsCMElementClass, vsCMElement.vsCMElementDelegate, vsCMElement.vsCMElementEnum, vsCMElement.vsCMElementEvent, vsCMElement.vsCMElementEventsDeclaration, vsCMElement.vsCMElementFunction, vsCMElement.vsCMElementInterface, vsCMElement.vsCMElementModule, vsCMElement.vsCMElementProperty
    Dim name As String = Space(depth * 4) & elem.Name
    Dim line As Integer = elem.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).Line
    functionIndex.Add(name, line)
    Case Else

    End Select

    Catch ex As Exception

    End Try

    For Each child As CodeElement In elem.Children
    BuildCodeElements(child, functionIndex, depth + 1)
    Next
    End Sub

    Private Class FChooser
    Inherits Form

    Dim lv As ListView

    Public Shared Function SelectItem(ByVal list As Dictionary(Of String, Integer)) As Integer
    Dim chooser As New FChooser(List)
    chooser.BringToFront()
    chooser.ShowDialog()
    Return chooser.Result
    End Function

    Public Sub New(ByVal list As Dictionary(Of String, Integer))
    lv = New ListView()
    lv.Dock = DockStyle.Fill
    lv.View = View.List

    For Each kvp As KeyValuePair(Of String, Integer) In list
    Dim lvi As ListViewItem = lv.Items.Add(kvp.Key)
    lvi.Tag = kvp.Value
    Next
    Me.Controls.Add(lv)

    AddHandler lv.KeyUp, AddressOf Key
    AddHandler lv.DoubleClick, AddressOf DoubleClick

    End Sub

    Public Result As Integer = -1

    Public Sub DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
    If (Me.lv.SelectedItems.Count = 1) Then
    Me.Result = CType(lv.SelectedItems(0).Tag, Integer)
    Me.Close()
    End If
    End Sub

    Public Sub Key(ByVal sender As Object, ByVal e As KeyEventArgs)
    If (e.KeyCode = Keys.Enter) Then
    Me.Result = CType(lv.SelectedItems(0).Tag, Integer)
    Me.Close()
    ElseIf (e.KeyCode = Keys.Escape) Then
    Result = -1
    Me.Close()
    Else
    ‘ could be a letter, and therefore a seek
    Dim letter As String = e.KeyCode.ToString().ToLower()
    If letter.Length = 1 And Regex.IsMatch(letter, “[a-z0-9]”) Then
    ‘ find the first entry starting with this letter

    Dim found As ListViewItem = Nothing
    For Each lvi As ListViewItem In Me.lv.Items
    Dim lviText As String = lvi.Text.Trim().ToLower()
    If (found Is Nothing And lviText.StartsWith(letter)) Then
    found = lvi
    End If
    Next

    If (found Is Nothing = False) Then
    For Each lvi As ListViewItem In Me.lv.Items
    lvi.Selected = False
    Next
    found.Selected = True
    End If
    End If
    End If
    End Sub

    End Class

  • Automatic Word Counts in MS Word

    August 24th, 2007

    I’m writing fiction at the moment. Which of course means I’m wasting time fiddling with formatting in Word.

    A neat manuscript [requires a word count](http://www.cs.cmu.edu/~mslee/format.html) on the first page. MS Word will give you a neat word count if you try the `Insert` menu, then `Field…`, choose `NumWords`, and hit OK.

    It’s fine, as far as it goes, but it’s very precise; it gives you a count that looks a little too autistic; ‘7672 words’. What you’d rather see is an approximation to, say, 100 words. Something more like ‘7700 words’. It’s fiddly, but you can do it. Save this off in a template somewhere and you’ll have automatic, neat wordcounting for ever after.

    1. use `Insert` | `Field…`
    2. Click the ‘Formula’ button
    3. Type what follows and hit OK.

    `= round ( 666 / 100 , 0) * 100`

    You should see ‘700’ displayed as the approximate word count. This is 666 rounded to the nearest hundred. Change `100` to `1000` in the line above to round to the nearest thousand. To get your story’s count…

    4. Right-click the number 700 and choose ‘Toggle Field Codes’
    5. Select the number `666`
    6. Hit ctrl-F9. The number now reads **{** 666 **}**
    7. Delete `666` and type `NumWords` instead.

    **{** = round ( **{** NumWords **}** / 100 , 0) * 100 **}**`

    8. Right-click the grey field and choose ‘Update Field’
    9. Voila! A neat wordcount, rounded to the nearest 100 words, and always up-to-date. The awesome power of a computer, neatly rounded.

  • A programming language only a mother could love

    April 2nd, 2007

    For the last few days, I’ve been trying to get my head around Common Lisp. Here are my first impressions.

    It’s a language that is both incredibly beautiful and awfully ugly. I don’t know quite how it manages it. First, some of the ugly;

    (apply
    #'(lambda (x y) (+ x y))
    ‘(1 2))

    See? Ugly. (The code is from Paul Graham’s free book, [On Lisp][].) It calculates the same as this C code;

    return 1 + 2;

    But, of course, all that `#'(lambda` stuff is doing more than the C; in fact, it’s doing more than C possibly can, which is where the beauty comes in.

    It seems to me that Lisp has some fantastic fundamental ideas and some really awful choices in naming and syntax. For example, here’s the way you set a variable in the lisp console;

    CL-USER> (setq x 4)
    4

    `setq`! not `:=`, not `assign`, not `set`; `setq`. That’s why I mean by awful naming.

    Another; here’s how to get the first item in a list;

    (car list)

    again, `car`? Admittedly, you _could_ use `first`, which is much more sensible, but the Lisp world seems to have settled on `car`, which stands for the ‘contents of the address register’, referring to a memory register in a long-dead machine from the 1970s. Awful naming.

    However, the punctuation is great. It’s just that all the words suck.

    If I were an author, and an editor gave me that advice, I’d quit. Wouldn’t you? So tell me something. Why is it that Lisp’s punctuation seems to hold the most profound and important programming concepts?

    For example; the quote character — ‘ — allows you to freeze-dry code into something you can pass around like any other variable. If I want to multiply a global variable, x, by two, I’d say this;

    CL-USER> (setq x 2)
    2
    CL-USER> (* x 2)
    4

    Thats sort-of-equivalent to this c-like code;

    int x = 2;
    int doublex = x * 2;

    But now I’m going to use the quote symbol…

    CL-USER> (setq x 2)
    2
    CL-USER> (setq doublex ‘(* x 2))
    (* X 2)
    CL-USER> (eval doublex)
    4

    I set the global variable `x` to 2. Then I set the variable `doublex` to be a piece of code which doubles the current value of `x`. Then, I evaluate the code in `doublex`, which gives me four.

    The fun part is, you can’t do this in C — I’ve assigned arbitrary code to a variable. So now I can pass functions around like variables. Isn’t that just like function pointers in C? A little. But Lisp goes way beyond.

    The awful glory is that the code is just a list; it’s the three symbols `*`, `x` and `2`. And since lisp can edit lists, and programs are lists, lisp can edit programs. And it’s no trickier to edit programs than it is to change arrays.

    I’ll say that again. _Lisp programs can edit programs as easily as C can edit arrays._

    Watch this. I’m going to examine code to see if it’s a multiplication.

    ;; `x` is a multiplier if it starts with the `*` symbol
    (defun is-multiplier? (x)
    (eq (first x) ‘*))

    ;; is doublex a multiplier? returns true.
    (is-multiplier? doublex)

    Just by checking for the `*` symbol, I can tell you things about the code.

    And if we decided that we actually wanted to calculate x+2, rather than x*2? Well, we just change a piece of the program

    ;; change the first item in ‘doublex’ to a plus.
    (setf (first doublex) ‘+)

    This is stuff you just can’t do in other languages. I’m going to keep looking into Lisp. It seems worthwhile.

    [On Lisp]: http://www.paulgraham.com/onlisp.html

  • Secret Santa 2006

    October 10th, 2006

    Once again I’m running Secret Commie Santa. It’s open to the York crowd, and thanks to Internet Shopping, anyone around the country that knows them.

    Why would you want to be involved?

    It makes your christmas shopping very, very easy.

    The idea is simple. We enter into a pact to buy each other pressies. Everyone gets a present of about the same value, even though people can afford to contribute different amounts. That’s the magic of Secret Commie Santa.

    1. Join the Pact by sending an email to steve

    /a t/

    @stevecooper

    /d o t/

    .org. Livejournal comments ain’t good enough!

    2. Tot up the amount you would normally spend on presents for everyone in the Pact. Tell me how much that is. We put all that money into a pot, and then share it out equally.

    3. I choose you a person I think you’d enjoy buying a present for, and act as a double-blind for questions, suggestions, and wishlists.

    4. You deliver your presents to me. If you contributed over the average into the pot, I’ll take the difference. If you contributed under the average, I’ll give you some cash to make up the difference. Equal Presents for All!

    5. With the help of my Magic Elves, I’ll deliver the presents out in time for Christmas.

    This year, since Santa has no sleigh, he’ll need to find some Magic Elves to help with the deliveries. Apply now for extra Christmas Kudos.

  • Clare and I are engaged!

    October 9th, 2006

    After a fantastic meal at Middlethorpe Hall, I asked Clare to marry me, and she said yes.* And then nearly sqeezed my head off with a great big cuddle.

    I’m over the moon.

    Here’s the ring;

    chuffing big diamond ;)

    We’ve not decided on a date for the wedding yet, but it’ll probably be in York, and Greg is the best man.

    —–
    * Well, of course she would. Who wouldn’t?

  • Leeds festival – thanks to cfw

    August 29th, 2006

    was very, very good to me and managed to get me tickets for Leeds festival over the weekend. I knew I love that woman for a reason 😉

    I saw Kaiser Chiefs, Arctic Monkeys, and Muse. Got right into the mosh with the first two, and that was a lot of fun. If you’re willing to get thrown about and crushed a bit, you can get right up the front with only a little effort; both times I was two or three rows from the front, right in the middle. You just need a bit of persistance and to not mind bruised ribs 😉

    Muse were fantastic, too. I stood a bit further off that time; still, the crowd were enthusiastic and it gave me more space to pogo rather than spend all my energy just trying to stand up.

  • Fantastic ’50’s science songs

    February 1st, 2006

    Wow

    These are, well, they’re delightful. Atomic-age kids songs about the joys of science. They feature the original soure of They Might Be Giants’ “Why does the sun shine”, and loads of other pieces of perhaps the finest empiricist kitsch ever…

    There is no disputin’,
    There is no refutin’,
    We’re all indebted to Sir Isaac Newton.

    Just when the creationists are getting you down, something like this comes along. Genius. GO LISTEN NOW!

    from the x-ray and electron and the quantum theory,
    down to Einstein and his formula for mass and energy.
    Hip Hooray! We’ve got atomic energy.
    It could mean a better world for all.

  • Secret Commie Santa III – your last chance

    October 19th, 2005

    Secret Commie Santa is about to close up! Send me a message ASAP if you want in.

    The current list of members;

    1. Ed Allison
    2. Steve Cooper
    3. John Denholm
    4. Olivia Denholm
    5. Brett Gibson
    6. Sarah Malaise
    7. Derek Muir
    8. Caro Partridge
    9. Ellen Phillips
    10. Greg Reynolds
    11. Wendy Reynolds
    12. Fleur Targett
    13. James Targett
    14. Clare Wall
  • [york] Secret Commie Santa III

    October 12th, 2005

    Hi, all. Well, it’s early, but before any of you start buying presents…

    Once again, I’ll be arranging a Secret Commie Santa. It’s a great way to get everyone in the York gang (and those crazies who have temporarily moved away) a really funky present for not much money.

    The way it works is like this;

    • THE LIST! I gather a list of everyone who’s interested, and distribute the list of names out. If you’re in, you decide how much you’d like to contribute to the pot.
    • THE NAMES! I pick a name for each person to buy a present for.
    • THE SUGGESTIONS! Everyone gives me suggestions for presents.
    • THE MONEY! The pot is split equally, and everyone buys a present costing about that amount.
    • THE SLEIGHRIDE! Everyone brings their present to me, and I get them distributed before christmas to the right person.

    This is our third time round; both previous attempts worked fantastically well. The pot ended up around £40-45 per person, and so everyone got a really funky present from the group. Verily, all was joyous and happy.

    So; just send me an email (santa3@stevecooper.org) telling me you’re in.

    Do it now.

    NOW!!!!

  • George RR Martin, Leeds Borders, 19th Oct, 1pm

    October 10th, 2005

    For all you fantasy fiends out there, George RR Martin will be talking
    about his fourth book in the Game of Thrones… thingy in Leeds
    Borders on 19th october. It’s a 1pm start, so maybe if you work in
    Leeds or something, you can get yourself over there.

←Previous Page
1 … 8 9 10 11 12 … 18
Next Page→

Create a free website or blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Subscribe Subscribed
    • Steve Cooper's Personal Blog
    • Already have a WordPress.com account? Log in now.
    • Steve Cooper's Personal Blog
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar