comment macro; long lines chopped off

2013-02-28
2013-05-15
  • Damian Green
    Damian Green
    2013-02-28

    I recently noticed that when I try to use a default comment macro in nedit (a perl comment), the end of very long lines get chopped off…

    The default macro for it is currently this in the ~/.nedit file is:
            Comments># Comment@Perl@Sh Ksh Bash@NEdit Macro@Makefile@Awk@Csh@Python@Tcl:::R: {\n\
                    replace_in_selection("^.*$", "#&", "regex")\n\
            }\n\

    It looks like the buffer for what gets retained in "&" is size limited…

    To change this behavior, I have found some code at http://www.mailnet.co.uk/~marc/msl.html#25  that I have modified slightly such that it works much better. I wanted to document it here such that the problem and solution would be easier to google.

    The solution is to add something like the following to your .nedit file or replace the contents of the section above with:
    Comment Perl2:::: {\n\
      *slideInString="# "\n\
      selstr=get_selection()\n\
      p=0\n\
      while(1) {\n\
        p=search_string(selstr,"^",p,"regex","forward")\n\
        if (p==-1 || p==length(selstr))\n\
          break\n\
        selstr=replace_substring(selstr,p,p,slideInString)\n\
        p++\n*

      }\n\
      replace_selection(selstr)\n\
    }\n\

    Good Luck!

     
  • Damian Green
    Damian Green
    2013-02-28

    After trying the above out for a while, I realized that if a selection did not begin at the beginning of a line, the comment would be floating where ever the selection began from, so I've updated the code to make it select add the comment from the beginning of the line, regardless of where the selection started in the line… So the following should work better for removing comments as well:

      slideInString="# "\n\
      final_cursor_pos=$cursor\n\
      end_of_selection()\n\
      b=$cursor\n\
      beginning_of_selection()\n\
      beginning_of_line()\n\
      a=$cursor\n\
      select(a,b)\n\
      selstr=get_selection()\n\
      p=0\n\
      while(1) {\n\
        p=search_string(selstr,"^",p,"regex","forward")\n\
        if (p==-1 || p==length(selstr))\n\
          break\n\
        selstr=replace_substring(selstr,p,p,slideInString)\n\
        p++\n\
      }\n\
      replace_selection(selstr)\n\

    Note: in the end, the selection disappears and the cursor goes to the beginning of the comment. But does that behavior really matter to anybody? If so, you have some updates to make… See http://web.njit.edu/all_topics/Prog_Lang_Docs/html/nedit