[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3. Getting Started (Tutorial)

3.1 Lesson I: Development Cycle  
3.2 Lesson II: Customization  
3.3 Lesson III: User and Library Catalogs  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 Lesson I: Development Cycle

The purpose of this tutorial is to guide you through a very basic development cycle using IDLWAVE. We will paste a simple program into a buffer and use the shell to compile, debug and run it. On the way we will use many of the important IDLWAVE commands. Note, however, that IDLWAVE has many more capabilities than covered here, which can be discovered by reading the entire manual, or hovering over the shoulder of your nearest IDLWAVE guru for a few days.

It is assumed that you have access to Emacs or XEmacs with the full IDLWAVE package including online help (see section 6. Installation). We also assume that you are familiar with Emacs and can read the nomenclature of key presses in Emacs (in particular, C stands for CONTROL and M for META (often the ALT key carries this functionality)).

Open a new source file by typing:

C-x C-f tutorial.pro RET

A buffer for this file will pop up, and it should be in IDLWAVE mode, indicated in the mode line just below the editing window. Also, the menu bar should contain entries `IDLWAVE' and `Debug'.

Now cut-and-paste the following code, also available as `tutorial.pro' in the IDLWAVE distribution.

function daynr,d,m,y
  ;; compute a sequence number for a date
  ;; works 1901-2099.
  if y lt 100 then y = y+1900
  if m le 2 then delta = 1 else delta = 0
  m1 = m + delta*12 + 1
  y1 = y * delta
  return, d + floor(m1*30.6)+floor(y1*365.25)+5
function weekday,day,month,year
  ;; compute weekday number for date
  nr = daynr(day,month,year)
  return, nr mod 7
pro plot_wday,day,month
  ;; Plot the weekday of a date in the first 10 years of this century.
  years = 2000,+indgen(10)
  wdays = intarr(10)
  for i=0,n_elements(wdays)-1 do begin
      wdays[i] =  weekday(day,month,years[i])
  plot,years,wdays,YS=2,YT="Wday (0=Sunday)"

The indentation probably looks funny, since it's different from the settings you use, so use the TAB key in each line to automatically line it up (or, more quickly, select the entire buffer with C-x h, and indent the whole region with C-M-\). Notice how different syntactical elements are highlighted in different colors, if you have set up support for font-lock.

Let's check out two particular editing features of IDLWAVE. Place the cursor after the end statement of the for loop and press SPC. IDLWAVE blinks back to the beginning of the block and changes the generic end to the specific endfor automatically. Now place the cursor in any line you would like to split and press M-RET. The line is split at the cursor position, with the continuation `$' and indentation all taken care of. Use C-/ to undo the last change.

The procedure plot_wday is supposed to plot the day of the week of a given date for the first 10 years of the 21st century. As in most code, there are a few bugs, which we are going to use IDLWAVE to help us fix.

First, let's launch the IDLWAVE shell. You do this with the command C-c C-s. The Emacs window will split or another window will popup to display IDL running in a shell interaction buffer. Type a few commands like print,!PI to convince yourself that you can work there just as well as in a terminal, or the IDLDE. Use the arrow keys to cycle through your command history. Are we having fun now?

Now go back to the source window and type C-c C-d C-c to compile the program. If you watch the shell buffer, you see that IDLWAVE types `.run tutorial.pro' for you. But the compilation fails because there is a comma in the line `years=...'. The line with the error is highlighted and the cursor positioned at the error, so remove the comma (you should only need to hit Delete!). Compile again, using the same keystrokes as before. Notice that the file is automatically saved for you. This time everything should work fine, and you should see the three routines compile.

Now we want to use the command to plot the day of the week on January 1st. We could type the full command ourselves, but why do that? Go back to the shell window, type `plot_' and hit TAB. After a bit of a delay (while IDLWAVE initializes its routine info database, if necessary), the window will split to show all procedures it knows starting with that string, and plot_wday should be one of them. Saving the buffer alerted IDLWAVE about this new routine. Click with the middle mouse button on plot_wday and it will be copied to the shell buffer, or if you prefer, add `w' to `plot_' to make it unambiguous (depending on what other routines starting with `plot_' you have installed on your system), hit TAB again, and the full routine name will be completed. Now provide the two arguments:


and press RET. This fails with an error message telling you the YT keyword to plot is ambiguous. What are the allowed keywords again? Go back to the source window and put the cursor into the `plot' line and press C-c ?. This shows the routine info window for the plot routine, which contains a list of keywords, along with the argument list. Oh, we wanted YTITLE. Fix that up. Recompile with C-c C-d C-c. Jump back into the shell with C-c C-s, press the UP arrow to recall the previous command and execute again.

This time we get a plot, but it is pretty ugly -- the points are all connected with a line. Hmm, isn't there a way for plot to use symbols instead? What was that keyword? Position the cursor on the plot line after a comma (where you'd normally type a keyword), and hit M-Tab. A long list of plot's keywords appears. Aha, there it is, PSYM. Middle click to insert it. An `=' sign is included for you too. Now what were the values of PSYM supposed to be? With the cursor on or after the keyword, press M-? for online help (alternatively, you could have right clicked on the colored keyword itself in the completion list). A browser will pop up showing the HTML documentation for the PYSM keyword. OK, let's use diamonds=4. Fix this, recompile (you know the command by now: C-c C-d C-c), go back to the shell (if it's vanished, you know what to do: C-c C-s) and execute again. Now things look pretty good.

Let's try a different day -- how about April fool's day?


Oops, this looks very wrong. All April fool's days cannot be Fridays! We've got a bug in the program, perhaps in the daynr function. Let's put a breakpoint on the last line there. Position the cursor on the `return, d+...' line and press C-c C-d C-b. IDL sets a breakpoint (as you see in the shell window), and the break line is indicated. Back to the shell buffer, re-execute the previous command. IDL stops at the line with the breakpoint. Now hold down the SHIFT key and click with the middle mouse button on a few variables there: `d', `y', `m', `y1', etc. Maybe d isn't the correct type. CONTROL-SHIFT middle-click on it for help. Well, it's an integer, so that's not the problem. Aha, `y1' is zero, but it should be the year, depending on delta. Shift click `delta' to see that it's 0. Below, we see the offending line: `y1=y*delta...' the multiplication should have been a minus sign! Hit q to exit the debugging mode, and fix the line to read:

y1 = y - delta

Now remove all breakpoints: C-c C-d C-a. Recompile and rerun the command. Everything should now work fine. How about those leap years? Change the code to plot 100 years and see that every 28 years, the sequence of weekdays repeats.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2 Lesson II: Customization

Emacs is probably the most customizable piece of software ever written, and it would be a shame if you did not make use of this and adapt IDLWAVE to your own preferences. Customizing Emacs or IDLWAVE is accomplished by setting Lisp variables in the `.emacs' file in your home directory -- but do not be dismayed; for the most part, you can just copy and work from the examples given here.

Let's first use a boolean variable. These are variables which you turn on or off, much like a checkbox. A value of `t' means on, a value of `nil' means off. Copy the following line into your `.emacs' file, exit and restart Emacs.

(setq idlwave-reserved-word-upcase t)

When this option is turned on, each reserved word you type into an IDL source buffer will be converted to upper case when you press SPC or RET right after the word. Try it out! `if' changes to `IF', `begin' to `BEGIN'. If you don't like this behavior, remove the option again from your `.emacs' file and restart Emacs.

You likely have your own indentation preferences for IDL code. For example, some like to indent the main block of an IDL program from the margin, different from the conventions used by RSI, and use only 3 spaces as indentation between BEGIN and END. Try the following lines in `.emacs':

(setq idlwave-main-block-indent 2)
(setq idlwave-block-indent 3)
(setq idlwave-end-offset -3)

Restart Emacs, and re-indent the program we developed in the first part of this tutorial with C-c h and C-M-\. You may want to keep these lines in `.emacs', with values adjusted to your likings. If you want to get more information about any of these variables, type, e.g., C-h v idlwave-main-block-indent RET. To find which variables can be customized, look for items marked `User Option:' throughout this manual.

If you cannot seem to master this Lisp customization in `.emacs', there is another, more user-friendly way to customize all the IDLWAVE variables. You can access it through the IDLWAVE menu in one of the `.pro' buffers, menu item Customize->Browse IDLWAVE Group. Here you'll be presented with all the various variables grouped into categories. You can navigate the hierarchy (e.g. IDLWAVE Code Formatting->Idlwave Main Block Indent), read about the variables, change them, and `Save for Future Sessions'. Few of these variables need customization, but you can exercise considerable control over IDLWAVE's functionality with them.

You may also find the key bindings used for the debugging commands too long and complicated. Often we have heard complaints along the lines of, "Do I really have to go through the finger gymnastics of C-c C-d C-c to run a simple command?" Due to Emacs rules and conventions, shorter bindings cannot be set by default, but you can easily enable them. First, there is a way to assign all debugging commands in a single sweep to another simpler combination. The only problem is that we have to use something which Emacs does not need for other important commands. One good option is to execute debugging commands by holding down CONTROL and SHIFT while pressing a single character: C-S-b for setting a breakpoint, C-S-c for compiling the current source file, C-S-a for deleting all breakpoints (try it, it's easier). You can enable this with:

(setq idlwave-shell-debug-modifiers '(shift control))

If you have a special keyboard with, for example, a SUPER key, you could even shorten that:

(setq idlwave-shell-debug-modifiers '(super))

to get compilation on S-c. Often, a modifier key like SUPER or HYPER is bound or can be bound to an otherwise unused key on your keyboard -- consult your system documentation.

You can also assign specific commands to keys. This you must do in the mode-hook, a special function which is run when a new IDLWAVE buffer gets set up. The possibilities for key customization are endless. Here we set function keys f4-f8 to common debugging commands.

;; First for the source buffer
(add-hook 'idlwave-mode-hook
   (lambda ()
    (local-set-key [f4] 'idlwave-shell-retall)
    (local-set-key [f5] 'idlwave-shell-break-here)
    (local-set-key [f6] 'idlwave-shell-clear-current-bp)
    (local-set-key [f7] 'idlwave-shell-cont)
    (local-set-key [f8] 'idlwave-shell-clear-all-bp)))
;; Then for the shell buffer
(add-hook 'idlwave-shell-mode-hook
   (lambda ()
    (local-set-key [f4] 'idlwave-shell-retall)
    (local-set-key [f5] 'idlwave-shell-break-here)
    (local-set-key [f6] 'idlwave-shell-clear-current-bp)
    (local-set-key [f7] 'idlwave-shell-cont)
    (local-set-key [f8] 'idlwave-shell-clear-all-bp)))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3 Lesson III: User and Library Catalogs

We have already used the routine info display in the first part of this tutorial. This was the invoked using C-c ?, and displays information about the IDL routine near the cursor position. Wouldn't it be nice to have the same kind of information available for your own routines and for the huge amount of code in major libraries like JHUPL or the IDL-Astro library? In many cases, you may already have this information. Files named `.idlwave_catalog' in library directories contain scanned information on the routines in that directory; many popular libraries ship with these "library catalogs" pre-scanned. Users can scan their own routines in one of two ways: either using the supplied tool to scan directories and build their own `.idlwave_catalog' files, or using the built-in method to create a single "user catalog", which we'll show here. See section A.3 Catalogs, for more information on choosing which method to use.

To build a user catalog, select Routine Info/Select Catalog Directories from the IDLWAVE entry in the menu bar. If necessary, start the shell first with C-c C-s (see section 5.1 Starting the Shell). IDLWAVE will find out about the IDL !PATH variable and offer a list of directories on the path. Simply select them all (or whichever you want -- directories with existing library catalogs will not be selected by default) and click on the `Scan&Save' button. Then go for a cup of coffee while IDLWAVE collects information for each and every IDL routine on your search path. All this information is written to the file `.idlwave/idlusercat.el' in your home directory and will from now on automatically load whenever you use IDLWAVE. You may find it necessary to rebuild the catalog on occasion as your local libraries change, or build a library catalog for those directories instead. Invoke routine info (C-c ?) or completion (M-TAB) on any routine or partial routine name you know to be located in the library. E.g., if you have scanned the IDL-Astro library:


expands to `readfits('. Then try

    a=readfits(C-c ?

and you get:

Usage:    Result = READFITS(filename, header, heap)

I hope you made it until here. Now you are set to work with IDLWAVE. On the way you will want to change other things, and to learn more about the possibilities not discussed in this short tutorial. Read the manual, look at the documentation strings of interesting variables (with C-h v idlwave<-variable-name> RET) and ask the remaining questions on the newsgroup comp.lang.idl-pvwave.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html