Index: glossary.module
===================================================================
--- glossary.module	(revision 928)
+++ glossary.module	(working copy)
@@ -254,7 +254,32 @@
     '#default_value' => variable_get("glossary_click_option", 0),
     '#description' => t('Changing this setting may require you to clear the cache_filter.'),
     );
+  $form['glossary'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Glossary Internal Links'),
+    '#collapsible' => true,
+    '#collapsed' => false,
+    '#description' => t('Settings for glossary items')
+  );
 
+  $form['glossary']['glossary_internal_links'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Link between glossary terms.'),
+    '#default_value' => variable_get('glossary_internal_links', true),
+    '#description' => t('If checked, glossary descriptions that contain other elements in the glossary will be linked (or annotated) to them. Settings below require this to be enabled.')
+  );
+  
+  $form['glossary']['glossary_link_term_to_self'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Link a term back to itself?'),
+    '#default_value' => variable_get('glossary_link_term_to_self', false),
+    '#description' => t('If checked, when a term contains its own term in its description it will be converted into a glossary item.')
+  );
+  
+  //Generate remaining form elements for each input filter.
+  $glossary_subform['glossary'] = glossary_filter_format_subform('0');
+  $form['glossary'] = array_merge($form['glossary'], $glossary_subform['glossary']);
+    
   return system_settings_form($form);
 }
 
@@ -263,12 +288,8 @@
  */ 
 function glossary_filter_form($format = 1) {
   $form = array();
-  
-  $options = array(t('<none>'));
-  foreach (taxonomy_get_vocabularies() as $vocabulary) {
-    $options[$vocabulary->vid] = $vocabulary->name;
-  }
 
+
   // Make sure we know if we need to clear the cache.
   variable_del('glossary_need_to_clear_cache', true);
 
@@ -325,8 +346,32 @@
   if (!$enabled) {
     return $form;
   }
+  
+  //Generate remaining form elements for each input filter.
+  $glossary_subform['filter'] = glossary_filter_format_subform($format);
+  $form['filter'] = array_merge($form['filter'], $glossary_subform['filter']);
 
-  $form['filter']["glossary_vids_$format"] = array(
+// Update Button
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration'),
+    '#weight' => 5,
+  );
+
+  return $form;
+}
+
+function glossary_filter_format_subform($format = null) {
+  if ($format == null) { return null; }
+  
+  $form = array();
+
+  $options = array(t('<none>'));
+  foreach (taxonomy_get_vocabularies() as $vocabulary) {
+    $options[$vocabulary->vid] = $vocabulary->name;
+  }
+  
+  $form["glossary_vids_$format"] = array(
     '#type' => 'select',
     '#title' => t('Select Vocabulary'),
     '#default_value' => variable_get("glossary_vids_$format", array()),
@@ -336,14 +381,14 @@
     '#required' => true,
     );
 
-  $form['filter']['match'] = array(
+  $form['match'] = array(
     '#type' => 'fieldset',
     '#title' => t('Term matching'),
     '#collapsible' => true,
     '#collapsed' => false,
     );
     
-  $form['filter']['match']["glossary_match_$format"] = array(
+  $form['match']["glossary_match_$format"] = array(
     '#type' => 'select',
     '#title' => t('Match type'),
     '#default_value' => variable_get("glossary_match_$format", 'match'),
@@ -357,7 +402,7 @@
     '#description' => t('Choose the match type of glossary links.'),
     );
 
-  $form['filter']['match']["glossary_case_$format"] = array(
+  $form['match']["glossary_case_$format"] = array(
     '#type' => 'select',
     '#title' => t('Case sensitivity'),
     '#default_value' => variable_get("glossary_case_$format", '1'),
@@ -368,7 +413,7 @@
     '#description' => t('Match either case sensitive or not. Case sensitive matches are not very resource intensive.'),
     );
 
-  $form['filter']['match']["glossary_replace_all_$format"] = array(
+  $form['match']["glossary_replace_all_$format"] = array(
     '#type' => 'select',
     '#title' => t('Replace matches'),
     '#default_value' => variable_get("glossary_replace_all_$format", 0),
@@ -379,7 +424,7 @@
     '#description' => t('Whether only the first match should be replaced or all matches.'),
     );
 
-  $form['filter']['match']["glossary_blocking_tags_$format"] = array(
+  $form['match']["glossary_blocking_tags_$format"] = array(
     '#type' => 'textarea',
     '#title' => t('Blocked elements'),
     '#default_value' => variable_get("glossary_blocking_tags_$format", 'acronym'),
@@ -389,7 +434,7 @@
     '#description' => t('Which html elements (tags) should not include Glossary links; that is, text within these elements will not be scanned for glossary terms. Enter the list separated by a space and do not include < and > characters (e.g. h1 h2).'),
     );
 
-  $form['filter']['indicator'] = array(
+  $form['indicator'] = array(
     '#type' => 'fieldset',
     '#title' => t('Link style'),
     '#collapsible' => true,
@@ -405,14 +450,14 @@
     $indicator_options['hovertip'] = t('hovertip');
   }
 
-  $form['filter']['indicator']["glossary_absolute_$format"] = array(
+  $form['indicator']["glossary_absolute_$format"] = array(
     '#type' => 'checkbox',
     '#title' => t('Make Glossary links absolute'),
     '#default_value' => variable_get("glossary_absolute_$format", false),
     '#description' => t('RSS feeds need absolute links to ensure they point back to this site. If you are not providing RSS feeds, it is better to leave this turned off.'),
     );
 
-  $form['filter']['indicator']["glossary_replace_$format"] = array(
+  $form['indicator']["glossary_replace_$format"] = array(
     '#type' => 'select',
     '#title' => t('Term Indicator'),
     '#default_value' => variable_get("glossary_replace_$format", 'superscript'),
@@ -421,7 +466,7 @@
     '#validate' => array('glossary_indicator_intercept' => array()),
     );
 
-  $form['filter']['indicator']["glossary_superscript_$format"] = array(
+  $form['indicator']["glossary_superscript_$format"] = array(
     '#type' => 'textfield',
     '#title' => t('Superscript'),
     '#default_value' =>  variable_get("glossary_superscript_$format", 'i'),
@@ -433,7 +478,7 @@
     );
 
   $mypath = '/'. drupal_get_path('module', 'glossary');
-  $form['filter']['indicator']["glossary_icon_$format"] = array(
+  $form['indicator']["glossary_icon_$format"] = array(
     '#type' => 'textfield',
     '#title' => t('Glossary Icon URL'),
     '#default_value' => variable_get("glossary_icon_$format", $mypath .'/glossary.gif'),
@@ -441,15 +486,7 @@
     '#maxlength' => 255,
     '#description' => t('If you choose !icon above, enter the URL of the glossary icon relative to the root of your Drupal site.',
       array('!icon' => '<strong>'. t('icon') .'</strong>')),
-    );
-
-// Update Button
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save configuration'),
-    '#weight' => 5,
-  );
-
+    );    
   return $form;
 }
 
@@ -532,7 +569,7 @@
   }
 }
 
-function _glossary_filter_process($format, $text) {
+function _glossary_filter_process($format, $text, $current_term = null) {
   global $user;
   if ($user->glossary_disable_indicator && variable_get('glossary_disable_indicator', false)) {
     return $text;
@@ -548,6 +585,12 @@
     $terms_replace = array();
 
     foreach ($terms as $term) {
+      //Skip the term if we are creating glossary links inside of another glossary item.
+      if (variable_get('glossary_link_term_to_self', false) == false) {
+        if ($term->name == $current_term) { 
+          continue;
+        }
+      }
       $term_title = $term->name .': '. strip_tags($term->description);
       $fragment = null;
       if ($term->nodes > 0) {
@@ -903,7 +946,11 @@
       $output .= l(t('Detailed definition of @term', array('@term' => $term->name)), "taxonomy/term/$term->tid");
     }
     else {
-      $output .= $term->description;
+      if (variable_get('glossary_internal_links', true)) {
+        $output .= _glossary_filter_process(0, $term->description, $term->name);
+      } else {
+        $output .= $term->description;
+      }
     }
   
     if ($relations = taxonomy_get_related($term->tid, "name")) {
