texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]


From: Patrice Dumas
Date: Sun, 29 Sep 2024 08:31:36 -0400 (EDT)

branch: master
commit c1b3de4c3fe2366b8822373be84ac6224499f998
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Mon Jun 24 14:49:25 2024 +0200

    * tp/Texinfo/ParserNonXS.pm (_isolate_trailing_space)
    (_register_extra_menu_entry_information, _isolate_last_space)
    (_process_remaining_on_line), tp/Texinfo/XS/parsetexi/menus.c
    (register_extra_menu_entry_information),
    tp/Texinfo/XS/parsetexi/parser.c (isolate_trailing_space)
    (isolate_last_space, process_remaining_on_line): check if there is
    content and the last content is text in isolate_trailing_space.  Call
    directly isolate_trailing_space in
    register_extra_menu_entry_information.  Remove call of
    isolate_trailing_space from isolate_last_space.
    
    * tp/Texinfo/ParserNonXS.pm (_isolate_trailing_spaces_element)
    (_isolate_trailing_space, _isolate_last_space),
    tp/Texinfo/XS/parsetexi/parser.c (isolate_trailing_spaces_element)
    (isolate_trailing_space, isolate_last_space): split
    isolate_trailing_spaces_element out of isolate_last_space, and use it
    in isolate_trailing_space to correctly transfer source marks.
    Reorganize isolate_last_space code to be more like
    isolate_trailing_space code, in particular do not check separately
    that the text ends with spaces.
---
 ChangeLog                        |  23 ++++++
 tp/Texinfo/ParserNonXS.pm        | 113 +++++++++++++++------------
 tp/Texinfo/XS/parsetexi/menus.c  |   2 +-
 tp/Texinfo/XS/parsetexi/parser.c | 162 ++++++++++++++++-----------------------
 tp/Texinfo/XS/parsetexi/parser.h |   1 +
 5 files changed, 155 insertions(+), 146 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5c64cf83e7..9946840704 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2024-06-24  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/ParserNonXS.pm (_isolate_trailing_space)
+       (_register_extra_menu_entry_information, _isolate_last_space)
+       (_process_remaining_on_line), tp/Texinfo/XS/parsetexi/menus.c
+       (register_extra_menu_entry_information),
+       tp/Texinfo/XS/parsetexi/parser.c (isolate_trailing_space)
+       (isolate_last_space, process_remaining_on_line): check if there is
+       content and the last content is text in isolate_trailing_space.  Call
+       directly isolate_trailing_space in
+       register_extra_menu_entry_information.  Remove call of
+       isolate_trailing_space from isolate_last_space.
+
+       * tp/Texinfo/ParserNonXS.pm (_isolate_trailing_spaces_element)
+       (_isolate_trailing_space, _isolate_last_space),
+       tp/Texinfo/XS/parsetexi/parser.c (isolate_trailing_spaces_element)
+       (isolate_trailing_space, isolate_last_space): split
+       isolate_trailing_spaces_element out of isolate_last_space, and use it
+       in isolate_trailing_space to correctly transfer source marks.
+       Reorganize isolate_last_space code to be more like
+       isolate_trailing_space code, in particular do not check separately
+       that the text ends with spaces.
+
 2024-06-23  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/ParserNonXS.pm (_process_remaining_on_line),
diff --git a/tp/Texinfo/ParserNonXS.pm b/tp/Texinfo/ParserNonXS.pm
index 0708856823..69535a7747 100644
--- a/tp/Texinfo/ParserNonXS.pm
+++ b/tp/Texinfo/ParserNonXS.pm
@@ -3086,18 +3086,44 @@ sub _abort_empty_line($$) {
   }
 }
 
+sub _isolate_trailing_spaces_element($)
+{
+  my $element = shift;
+  my $new_space_element;
+
+  if ($element->{'text'} =~ s/(\s+)$//) {
+    $new_space_element = {'text' => $1};
+    if ($element->{'source_marks'}) {
+      my $current_position = length($element->{'text'});
+      Texinfo::Common::relocate_source_marks(
+                          $element->{'source_marks'}, $new_space_element,
+                          $current_position, length($1));
+      delete $element->{'source_marks'}
+        if (!scalar(@{$element->{'source_marks'}}));
+    }
+  }
+  return $new_space_element;
+}
+
 sub _isolate_trailing_space($$)
 {
   my $current = shift;
   my $spaces_type = shift;
 
-  if ($current->{'contents'}->[-1]->{'text'} !~ /\S/) {
-    $current->{'contents'}->[-1]->{'type'} = $spaces_type;
-  } else {
-    if ($current->{'contents'}->[-1]->{'text'} =~ s/(\s+)$//) {
-      my $new_spaces = { 'text' => $1, 'parent' => $current,
-        'type' => $spaces_type };
-      push @{$current->{'contents'}}, $new_spaces;
+  if ($current->{'contents'}) {
+    my $last_element = $current->{'contents'}->[-1];
+    if (defined($last_element->{'text'})
+        and $last_element->{'text'} ne '') {
+      if ($last_element->{'text'} !~ /\S/) {
+        $last_element->{'type'} = $spaces_type;
+      } else {
+        my $new_space_element = 
_isolate_trailing_spaces_element($last_element);
+        if ($new_space_element) {
+          $new_space_element->{'type'} = $spaces_type;
+          $new_space_element->{'parent'} = $current;
+          push @{$current->{'contents'}}, $new_space_element;
+        }
+      }
     }
   }
 }
@@ -3131,53 +3157,43 @@ sub _isolate_last_space
   my $debug_str;
   if ($self->{'conf'}->{'DEBUG'}) {
     $debug_str = 'p '.Texinfo::Common::debug_print_element($current).'; c ';
-    if ($current->{'contents'} and scalar(@{$current->{'contents'}})) {
+    if ($current->{'contents'}) {
       $debug_str .=
          Texinfo::Common::debug_print_element($current->{'contents'}->[-1]);
     }
   }
 
-  if (!$current->{'contents'}
-      or !scalar(@{$current->{'contents'}})
-      or !defined($current->{'contents'}->[-1]->{'text'})
-      or $current->{'contents'}->[-1]->{'text'} !~ /\s+$/) {
-    print STDERR "NOT ISOLATING $debug_str\n"
-       if ($self->{'conf'}->{'DEBUG'});
-    return;
-  }
-
-  my $last_element = $current->{'contents'}->[-1];
-
-  print STDERR "ISOLATE SPACE $debug_str\n"
-    if ($self->{'conf'}->{'DEBUG'});
-
-  if ($current->{'type'} and $current->{'type'} eq 'menu_entry_node') {
-    _isolate_trailing_space($current, 'space_at_end_menu_node');
-  } else {
-    # Store final spaces in 'spaces_after_argument'.
-    #$current->{'info'} = {} if (!$current->{'info'});
-    if ($last_element->{'text'} !~ /\S/) {
-      my $spaces_after_argument = _pop_element_from_contents($self, $current);
-      delete $spaces_after_argument->{'parent'};
-      delete $spaces_after_argument->{'type'};
-      $current->{'info'} = {} if (!exists($current->{'info'}));
-      $current->{'info'}->{'spaces_after_argument'}
-                 = $spaces_after_argument;
-    } else {
-      $last_element->{'text'} =~ s/(\s+)$//;
-      my $new_space_element = {'text' => $1,};
-      if ($last_element->{'source_marks'}) {
-        my $current_position = length($last_element->{'text'});
-        Texinfo::Common::relocate_source_marks(
-                            $last_element->{'source_marks'}, 
$new_space_element,
-                            $current_position, length($1));
-        delete $last_element->{'source_marks'}
-          if (!scalar(@{$last_element->{'source_marks'}}));
+  if ($current->{'contents'}) {
+    my $last_element = $current->{'contents'}->[-1];
+    if (defined($last_element->{'text'})
+        and $last_element->{'text'} ne '') {
+      # Store final spaces in 'spaces_after_argument'.
+      if ($last_element->{'text'} !~ /\S/) {
+        my $spaces_after_argument = _pop_element_from_contents($self, 
$current);
+        delete $spaces_after_argument->{'parent'};
+        delete $spaces_after_argument->{'type'};
+        $current->{'info'} = {} if (!exists($current->{'info'}));
+        $current->{'info'}->{'spaces_after_argument'}
+               = $spaces_after_argument;
+      } else {
+        my $new_space_element = 
_isolate_trailing_spaces_element($last_element);
+        if ($new_space_element) {
+          $current->{'info'} = {} if (!exists($current->{'info'}));
+          $current->{'info'}->{'spaces_after_argument'} = $new_space_element;
+        } else {
+          print STDERR "NOT ISOLATING $debug_str\n"
+            if ($self->{'conf'}->{'DEBUG'});
+          return;
+        }
       }
-      $current->{'info'} = {} if (!exists($current->{'info'}));
-      $current->{'info'}->{'spaces_after_argument'} = $new_space_element;
+      print STDERR "ISOLATE SPACE $debug_str\n"
+        if ($self->{'conf'}->{'DEBUG'});
+      return;
     }
   }
+
+  print STDERR "NOT ISOLATING $debug_str\n"
+     if ($self->{'conf'}->{'DEBUG'});
 }
 
 # split non-space text elements into strings without [ ] ( ) , and single
@@ -4832,7 +4848,7 @@ sub _register_extra_menu_entry_information($$;$)
                           $source_info);
       }
     } elsif ($arg->{'type'} eq 'menu_entry_node') {
-      _isolate_last_space($self, $arg);
+      _isolate_trailing_space($arg, 'space_at_end_menu_node');
       if (! $arg->{'contents'}) {
         if ($self->{'conf'}->{'FORMAT_MENU'} eq 'menu') {
           $self->_line_error(__("empty node name in menu entry"), 
$source_info);
@@ -7461,9 +7477,6 @@ sub _process_remaining_on_line($$$$)
     _check_valid_nesting_context ($self, $command, $source_info);
 
     if ($in_index_commands{$command}
-        and $current->{'contents'}
-        and $current->{'contents'}->[-1]
-        and $current->{'contents'}->[-1]->{'text'}
         # it is important to check if in an index command, as otherwise
         # the internal space type is not processed and remains as is in
         # the final tree.
diff --git a/tp/Texinfo/XS/parsetexi/menus.c b/tp/Texinfo/XS/parsetexi/menus.c
index 8fc271fb7f..cf61ef1536 100644
--- a/tp/Texinfo/XS/parsetexi/menus.c
+++ b/tp/Texinfo/XS/parsetexi/menus.c
@@ -61,7 +61,7 @@ register_extra_menu_entry_information (ELEMENT *current)
         {
           NODE_SPEC_EXTRA *parsed_entry_node;
 
-          isolate_last_space (arg);
+          isolate_trailing_space (arg, ET_space_at_end_menu_node);
 
           parsed_entry_node = parse_node_manual (arg, 1);
           if (!parsed_entry_node->manual_content
diff --git a/tp/Texinfo/XS/parsetexi/parser.c b/tp/Texinfo/XS/parsetexi/parser.c
index ed001cc1cc..56c2c58ebc 100644
--- a/tp/Texinfo/XS/parsetexi/parser.c
+++ b/tp/Texinfo/XS/parsetexi/parser.c
@@ -968,61 +968,54 @@ abort_empty_line (ELEMENT *current)
     }
 }
 
-/* The caller verifies that last_elt is a text element */
-static void
-isolate_last_space_internal (ELEMENT *current, ELEMENT *last_elt)
+static ELEMENT *
+isolate_trailing_spaces_element (char *text, enum element_type spaces_type,
+                                 ELEMENT *element)
 {
-  char *text;
-  int text_len;
+  ELEMENT *spaces_element = 0;
+  int i, trailing_spaces;
+  int text_len = element->e.text->end;
 
-  text = last_elt->e.text->text;
-  text_len = last_elt->e.text->end;
+  trailing_spaces = 0;
+  for (i = text_len - 1;
+       i > 0 && strchr (whitespace_chars, text[i]);
+       i--)
+    trailing_spaces++;
 
-  /* If text all whitespace */
-  if (text[strspn (text, whitespace_chars)] == '\0')
+  if (trailing_spaces)
     {
-      /* e is last_elt */
-      ELEMENT *e = pop_element_from_contents (current);
-      e->parent = 0;
-      e->type = ET_other_text;
-      current->elt_info[eit_spaces_after_argument] = e;
-    }
-  else
-    {
-      int i, trailing_spaces;
-      ELEMENT *spaces_element = new_text_element (ET_other_text);
-
-      trailing_spaces = 0;
-      for (i = text_len - 1;
-           i > 0 && strchr (whitespace_chars, text[i]);
-           i--)
-        trailing_spaces++;
-
-      text_append_n (spaces_element->e.text, text + text_len - trailing_spaces,
+      spaces_element = new_text_element (spaces_type);
+      text_append_n (spaces_element->e.text,
+                     text + text_len - trailing_spaces,
                      trailing_spaces);
 
       text[text_len - trailing_spaces] = '\0';
-      last_elt->e.text->end -= trailing_spaces;
+      element->e.text->end -= trailing_spaces;
 
-      if (last_elt->source_mark_list)
+      if (element->source_mark_list)
         {
           size_t begin_position = count_multibyte (text);
-          relocate_source_marks (last_elt->source_mark_list, spaces_element,
-                                 begin_position,
-                                 count_multibyte 
(spaces_element->e.text->text));
-          destroy_element_empty_source_mark_list (last_elt);
+          relocate_source_marks (element->source_mark_list,
+                                 spaces_element, begin_position,
+                       count_multibyte (spaces_element->e.text->text));
+          destroy_element_empty_source_mark_list (element);
         }
-      current->elt_info[eit_spaces_after_argument] = spaces_element;
     }
+  return spaces_element;
 }
 
-/* The caller verifies that last_elt is a text element */
-static void
-isolate_trailing_space (ELEMENT *current, ELEMENT *last_elt,
-                        enum element_type spaces_type)
+
+void
+isolate_trailing_space (ELEMENT *current, enum element_type spaces_type)
 {
-  char *text = last_elt->e.text->text;
+  ELEMENT *last_elt = last_contents_child (current);
+  char *text;
 
+  if (!last_elt || !(type_data[last_elt->type].flags & TF_text)
+      || last_elt->e.text->end <= 0)
+    return;
+
+  text = last_elt->e.text->text;
 
   /* If text all whitespace */
   if (text[strspn (text, whitespace_chars)] == '\0')
@@ -1031,27 +1024,10 @@ isolate_trailing_space (ELEMENT *current, ELEMENT 
*last_elt,
     }
   else
     {
-      ELEMENT *new_spaces;
-      int i, trailing_spaces;
-      int text_len = last_elt->e.text->end;
-
-      trailing_spaces = 0;
-      for (i = text_len - 1;
-           i > 0 && strchr (whitespace_chars, text[i]);
-           i--)
-        trailing_spaces++;
-
-      if (trailing_spaces)
-        {
-          new_spaces = new_text_element (spaces_type);
-          text_append_n (new_spaces->e.text,
-                         text + text_len - trailing_spaces,
-                         trailing_spaces);
-          text[text_len - trailing_spaces] = '\0';
-          last_elt->e.text->end -= trailing_spaces;
-
-          add_to_element_contents (current, new_spaces);
-        }
+      ELEMENT *new_spaces = isolate_trailing_spaces_element (text,
+                                             spaces_type, last_elt);
+      if (new_spaces)
+        add_to_element_contents (current, new_spaces);
     }
 }
 
@@ -1059,7 +1035,6 @@ void
 isolate_last_space (ELEMENT *current)
 {
   ELEMENT *last_elt = 0;
-  int text_len;
 
   if (current->e.c->contents.number == 0)
     return;
@@ -1079,32 +1054,39 @@ isolate_last_space (ELEMENT *current)
         }
     }
 
-  if (current->e.c->contents.number == 0)
-    goto no_isolate_space;
-
   last_elt = last_contents_child (current);
 
-  if (!(type_data[last_elt->type].flags & TF_text))
-    goto no_isolate_space;
-
-  text_len = last_elt->e.text->end;
-  if (text_len <= 0)
-    goto no_isolate_space;
-
-  /* Does the text end in whitespace? */
-  if (!strchr (whitespace_chars, last_elt->e.text->text[text_len - 1]))
-    goto no_isolate_space;
+  if (last_elt && type_data[last_elt->type].flags & TF_text
+      && last_elt->e.text->end > 0)
+    {
+      char *text = last_elt->e.text->text;
+      /* If text all whitespace */
+      if (text[strspn (text, whitespace_chars)] == '\0')
+        {
+          /* e is last_elt */
+          ELEMENT *e = pop_element_from_contents (current);
+          e->parent = 0;
+          e->type = ET_other_text;
+          current->elt_info[eit_spaces_after_argument] = e;
+        }
+      else
+        {
+          ELEMENT *spaces_element
+            = isolate_trailing_spaces_element (text,
+                                               ET_other_text, last_elt);
+          if (spaces_element)
+            current->elt_info[eit_spaces_after_argument] = spaces_element;
+          else
+            goto no_isolate_space;
+        }
+    }
+  else
+   goto no_isolate_space;
 
   debug_nonl ("ISOLATE SPACE p ");
   debug_parser_print_element (current, 0);
   debug_nonl ("; c ");
   debug_parser_print_element (last_elt, 0); debug ("");
-
-  if (current->type == ET_menu_entry_node)
-    isolate_trailing_space (current, last_elt, ET_space_at_end_menu_node);
-  else
-    isolate_last_space_internal (current, last_elt);
-
   return;
 
  no_isolate_space:
@@ -2447,25 +2429,15 @@ process_remaining_on_line (ELEMENT **current_inout, 
const char **line_inout)
           && (command_flags(current->parent) & CF_index_entry_command
                || current->parent->e.c->cmd == CM_subentry))
         {
-          ELEMENT *last_elt = last_contents_child (current);
-
-          if (last_elt && type_data[last_elt->type].flags & TF_text
-              && last_elt->e.text->end > 0)
-            {
-              if (cmd == CM_subentry)
-                {
-                  isolate_trailing_space (current, last_elt, ET_spaces_at_end);
-                }
-              else
+          if (cmd == CM_subentry)
+            isolate_trailing_space (current, ET_spaces_at_end);
+          else
                /* an internal and temporary space type that is converted to
                   a normal space if followed by text or a
                   "spaces_at_end" if followed by spaces only when the
                   index or subentry command is done. */
-                {
-                  isolate_trailing_space (current, last_elt,
-                               ET_internal_spaces_before_brace_in_index);
-                }
-            }
+            isolate_trailing_space (current,
+                                ET_internal_spaces_before_brace_in_index);
         }
 
       /* check command doesn't start a paragraph */
diff --git a/tp/Texinfo/XS/parsetexi/parser.h b/tp/Texinfo/XS/parsetexi/parser.h
index 7cc6300366..ccb490f3aa 100644
--- a/tp/Texinfo/XS/parsetexi/parser.h
+++ b/tp/Texinfo/XS/parsetexi/parser.h
@@ -82,6 +82,7 @@ ELEMENT *end_paragraph_preformatted (ELEMENT *current,
                                  enum command_id closed_block_cmd,
                                  enum command_id interrupting_cmd);
 void isolate_last_space (ELEMENT *current);
+void isolate_trailing_space (ELEMENT *current, enum element_type spaces_type);
 int kbd_formatted_as_code (ELEMENT *current);
 int parent_of_command_as_argument (ELEMENT *current);
 void register_command_as_argument (ELEMENT *cmd_as_arg);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]