7. Custom menu

7.1. Adding a custom menu dialog
7.2. Adding a custom replace dialog

To customize items in the Custom menu tool bar, you will use the Custom menu element:

A screen shot of the default custom menu's access

Figure VII.22. Accessing the Custom Menu

The Custom menuEdit custom menu... leads to the Custom Menu Editor. The Load new item allows you to load a new menu in case you have directly changed the custom_menu file located in the .bluefish directory within your HOME directory, while Reset item allows you to return to the default custom menu under the same circumstances.

The custom_menu file is created upon install Bluefish and corresponds to some default entries, the ones you can see in the Custom menu tool bar. These will give you an idea what can be done with the custom menu.

The custom menu operates only on elements of the Custom menu tool bar, and allows you to:

The Custom Menu Editor is the place where you make all changes to the custom menu. The location for entries in the custom menu is defined by their menu path in the Custom Menu Editor:

A screen shot of the custom menu editor

Figure VII.23. The Custom Menu Editor

It has four parts:

7.1. Adding a custom menu dialog

The most simple custom dialog item has a menupath, for example /MySite/author, and a Formatstring before, for example written by Olivier. If you add this item, you can add this string by selecting the menu item.

Procedure VII.2. Adding a custom menu based on custom dialog

  1. Choose Custom menuEdit custom menu... in the custom menu tool bar.

  2. Enter /MySite/author in the Menu Path field of the Custom Menu editor.

  3. Enter written by Olivier in the Formatstring Before field located on the right.

  4. Click on the Add button at the top.

    Notice that upon adding the new entry, it is listed at the bottom of the Menu path list:

    A screen shot of a new custom entry in the Menu path list

    Figure VII.26. A new Custom Entry in the Menu Path List

  5. Click on the Save button. This will add the menu to the Custom menu tool bar:

    A screen shot of a new menu in the custom menu tool bar

    Figure VII.27. A new Menu in the Custom Menu Tool Bar

Note that the new menu is placed at the right end of the custom menu tool bar. When closing Bluefish and relaunching it, it will be placed in alphabetical order, except that the Replace menu will always be at the far right side.

In another example, you have a string you often need to set before and after some block of text, for example <div class="MyClass">YourBlockOfText</div>. To do it:

  1. Open the Custom Menu Editor

  2. Enter /MySite/div with class in the Menu Path field

  3. Enter <div class="MyClass"> in the Formatstring Before field

  4. Enter </div> in the Formatstring After field

  5. Click on Add, then on Save. The item will appear in the menu.

If you select some text:

A screen shot of a block of selected text before activating the menu

Figure VII.28. A Block of selected Text before Activating the Menu

And activate this menu item, the first bit of text is now added before the selection, and the second bit after the selection:

A screen shot of a block of text after activating the menu

Figure VII.29. A Block of Text after Activating the Menu

Suppose you want to improve this last example. You have both MyClass1 and MyClass2 and want to be able to choose the desired class when activating the menu. Here's how to do it:

  1. Open the Custom Menu Editor

  2. Browse the Menu path list to retrieve the /MySite/class with div entry and click on it to make appear its components in the Menu Path and Custom Dialog fields

  3. Click on the top arrow of the Number of Variables pop up menu to get 1 in the field. As you see a Variables entry appears where you can enter the name for variable %0. As name we enter MyClass number

  4. Now change the FormatString Before field to take this new variable into account, as following: <div class="MyClass%0">

  5. Click on Apply so that your changes will be taken in account, and click on Save to update the menu.

If you now activate this menu after having selected a block of text, you will be presented with a new dialog asking you for the value of MyClass number:

A screen shot of the new div with class dialog

Figure VII.30. The new DIV with Class Dialog

After entering the desired value, the same process as before will occur, using the value you provided. Here we have entered 1 as value:

A screen shot of the block of text after entering the value

Figure VII.31. The Block of Text after Entering the Value

[Tip]

You can use the Return and Tab keys to format the output.

Any variable can be used any times you want in the dialog.

7.2. Adding a custom replace dialog

Find and replace items are no different. The dialog has some more options, each of these options corresponds to the regular Replace dialog. Again you can use variables like %0, %1 etc. to make a certain menu item more flexible.

Say you want to add title tags to a selection in a HTML page so that the user agent could render it either as a tool tip or as spoken words. To ease the discussion we will work on the following snippet of code:

<ul>
<li><a href="progsys01.html">Process scheduling</a> - 26/10/2002</li>
<li><a href="progsys02.html">Fork and Wait</a> - 02/11/2002</li>
</ul>

We will transform it into the following one:

<ul>
<li><a href="progsys01.html" title="blah Process scheduling">Process scheduling</a> \
- 26/10/2002</li>
<li><a href="progsys02.html" title="blah Fork and Wait">Fork and Wait</a> \
- 02/11/2002</li>
</ul>

where blah is any text you want.

The initial rendering:

A screen shot of the HTML page before the transformation

Figure VII.32. The HTML Page before the Transformation

will be transformed as is:

A screen shot of the HTML page after the transformation

Figure VII.33. The HTML Page after the Transformation

To do this, we need to express the <a href="yoururl">yourstring</a> part of the initial snippet of code as a Perl regular expression (see Section 5.3, “Find and Replace Using Regular Expressions” for full details):

  • a href=" will be expressed as is and embedded into parentheses to retrieve it as \0 variable.

  • yoururl will be expressed as ([^"]+) to match one or more non double quote characters and retrieve it as \1 variable.

  • The second double quote will be expressed as is and embedded into parentheses to retrieve it as \2 variable.

  • The second > sign will be expressed as is and embedded into parentheses to retrieve it as \3 variable.

  • yourstring will be expressed as ([^>]+) to match one or more non > characters and retrieve it as \4 variable.

  • </a> will be expressed as is and embedded into parentheses to retrieve it as \5 variable.

Thus, the search string will be: (<a href=")([^"]+)(")(>)([^>]+)(</a>)

The replace string should be of the form: <a href="yoururl" title="yourvariablestring yourstring">yourstring</a>

Expressed as a regular Perl replacement expression, it will be as simple as: \0\1\2 title=\2%0 \4\2\3\4\5 where %0 will match yourvariablestring, that is the value entered in the Title field of the Replace dialog at activating time.

Procedure VII.3. Adding a custom menu based on replace dialog

  1. Choose Custom menuEdit custom menu... in the custom menu tool bar.

  2. Browse the Menu path list to retrieve the /Replace/Convert in Selection/<td> to <th> and click on it to make appear its components in the Menu Path and Custom Replace fields.

  3. Change the Menu Path to /Replace/Anchor/Add Title.

  4. Click on the top arrow of the Number of Variables pop up menu to get 1 in the field. Enter Title in the %0 Variables field.

  5. Change the Matching pop up menu to perl regular expressions.

  6. Change the Search Pattern field like this:

    (<a href=")([^"]+)(")(>)([^>]+)(</a>)
  7. Change the Replace String field like this:

    \0\1\2 title=\2%0 \4\2\3\4\5
  8. Click on the Add button.

    The Custom replace dialog should have the following appearance:

    A screen shot of the custom menu replace dialog filled in

    Figure VII.34. The Custom Menu Replace Dialog filled in

  9. Click on the Save button.

To use the new menu item, select the lines to be changed in the HTML file and activate Replace/Anchor/Add Title in the custom menu bar. Fill in the dialog as follows:

A screen shot of the Add Title dialog

Figure VII.35. The Add Title Dialog

Click OK to proceed.