Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica editor: Removing the right matching ']' automatically when the left '[' is removed?

In Mathematica editor (i.e. notebook), one of the hardest things I always found is when I want to edit some long expression, and go and remove the left end "[" of some expression then before I get the chance to remove the right "]", the code in the cell will get all messed up, since it is no longer balanced, making it really hard to go chase the, now hanging, right end "]" since it is no longer in its original place!

Here is a simple example, suppose we have this

Text[Row[{PaddedForm[currentTime, {6, 3}, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}]}]]

now say I wanted to remove the outside Text[] call. So I delete "Text[" and before I get a chance to delete the other side "]", the notebook will now juggle things all over the place, making it hard to find the right "]". For long cell (which is the case for demonestrations work), the code can shift by large amount, and the right "]" can go out of view as well, has to scroll down and up looking for it.

Is there a way to set it, via an option or such, so that when I delete the left "[", Mathematica will automatically delete the matching right "]"? This applies for "(" and ")" as well.

This is really a big problem with the use of the notebook editor for me. Can't tell you how much time I spend just looking the hanging "]".

thanks --Nasser

like image 307
Nasser Avatar asked May 26 '11 11:05

Nasser


2 Answers

I shall think about an automatic method, but I currently handle this with:

  1. place the cursor on the first token inside the function you want to delete (in this case Row)

  2. press Ctrl+. until everything inside is selected (twice, in this case)

  3. Copy

  4. press Ctrl+. once to include the function to delete in the selection

  5. Paste

It is really quite fast once you do it a few times.


Andrew Moylan suggests this mouse variation:

Here is the variant I use for this common operation:

  • Triple-click "Row", Ctrl+C, Triple-click "Text", Ctrl+V, done
like image 84
Mr.Wizard Avatar answered Nov 14 '22 13:11

Mr.Wizard


I just registered here and I do not have MMA with me now, so this is just an outline of the proposed solution. This is my first answer here, so bear with me please. By the way...nice to see many mathgroupers here too. :-)

Nasser's question/problem is very common. I personally use the same technique described by Andrew. It is not difficult however solve this problem more elegantly with a button.

Create a small button palette with an action button. It would work like this:

  1. you double-click the outermost function (the one you want to delete). Since you double-click it you will select everything up to the rightmost ].
  2. you click on the action button. The code in the button will simply get the selection and extract the argument within the [] pair, then it will paste the argument onto the selection in the notebook. Without evaluating the argument of course.

That's it. Couple of lines of code.

Since you are at it, you might add a few more buttons to the palette. For example:

  • a button to comment/uncommnent a selection
  • a button to wrap {} or () to a selection
  • a button to wrap fun[] to a selection (fun can be left selected so it can later be overwritten with an appropriate function)

So you end up creating your own programming palette.

like image 4
magma Avatar answered Nov 14 '22 14:11

magma