Contents:

1. Introduction

This program implements a little utility for Windows users. It installs an icon in the system tray, and keeps track of text snippets from your clipboard (showing the last 5 items that you copied). It also allows you to add small notes.

2. Overview

The code is divided into three main sections: 3. Globals, defining a few global values, 4. Functions, defining the functions (actually, just one), and 5. System port setup, that sets up the system port (used to get Windows messages from the tray icon).

After that, the program consists of calling the set-tray function, that installs the systray icon and attaches a menu to it, and looping forever (see the 2.1 Forever loop section for the reason we do this).

〈Main〉 ≡

3. Globals
4. Functions
5. System port setup

set-tray
forever [
 2.1 Forever loop
]

2.1 Forever loop

Every 5 seconds, we check che clipboard. If a new clip is present, we add it to the clipboard history.

We conclude that there is a new clip if all the following conditions are met:

  1. read clipboard:// does not fail and returns a string;
  2. the string is not empty;
  3. the string is not the same as the last clip in the history.

If it is so, we add it to the list (the title for the snippet is generated from the clip text); then if the list is longer than 5 items, we remove the older items (so only 5 are retained). (Note that we are actually only moving the block position; on saving only values from this position onward are saved (since we are not using save/all) and when generating the menu only these are considered.) Then we save the clips and recreate the menu by calling the set-tray function.

〈2.1 Forever loop〉 ≡

wait 5
if all [clip: attempt [read clipboard://] not empty? clip any [empty? clips/2 clip second last clips/2]] [
 ; add clip to the list (title generated from clip text)
 insert/only tail clips/2 reduce [trim/lines copy/part clip 20 clip]
 ; show only the last 5 items in the history
 clips/2: skip tail clips/2 −5
 ; save and recreate menu
 attempt [save %clips.txt clips]
 set-tray
]