Loading ...
Sorry, an error occurred while loading the content.

Function question

Expand Messages
  • Sean C. Hubbell
    Hello, I was wondering if I could get some advice. I created a function and I have two issues. The first issue is when I print to the screen my LineComment
    Message 1 of 4 , Dec 1, 2004
    • 0 Attachment
      Hello,

      I was wondering if I could get some advice. I created a function and I
      have two issues. The first issue is when I print to the screen my
      LineComment variable, I always get a new line after echoing the LineComment
      using the following function:

      function TFunc(num1, num2, ...)
      let Tab = " "

      " let LineComment =
      "/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
      \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
      \*\*\*/"
      let LineComment =
      "\/*************************************************************************
      *****/"
      "let LineComment = "/\*\*/"
      echo LineComment
      echo a:num1 a:num2
      echo LineComment
      echo Tab "{"
      echo Tab "/* ."
      echo
      echo Tab Tab "History:"
      echo Tab Tab Tab expand (strftime("%d %b %y")) "- Created. SCHubbell"
      echo Tab "*/"
      echo Tab "} /* " a:num2 " */"
      endfunction


      If I assign LineComment to the following:

      let LineComment = "/\*\*/"

      I do not get a new line at the end. I checked to see if I had wrap set but I
      do not. Does anyone have an idea?

      Next, I get all of this printed to the screen, but what I really want is
      the best way to insert the text generated by my function into the current
      line that the cursor is on? Do I need to set the function to write to a
      buffer and then paste the buffer?

      Thanks in advance,

      Sean

      Sean C. Hubbell
      (256) 881-9933 Ext. 316
      (256) 426-8957 (Cell)
      Ternion Corporation
      3325 Triana Boulevard
      Huntsville, Alabama 35805
    • Jürgen Krämer
      Hi, ... that s because LineComment contains exactly 80 character and 80 is probably the width of your VIM window. After echoing characters
      Message 2 of 4 , Dec 1, 2004
      • 0 Attachment
        Hi,

        Sean C. Hubbell schrieb:
        >
        > I was wondering if I could get some advice. I created a function and I
        > have two issues. The first issue is when I print to the screen my
        > LineComment variable, I always get a new line after echoing the LineComment
        > using the following function:
        >
        > function TFunc(num1, num2, ...)
        > let Tab = " "
        >
        > " let LineComment =
        > "/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
        > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
        > \*\*\*/"
        > let LineComment =
        > "\/*************************************************************************
        > *****/"

        that's because LineComment contains exactly 80 character and 80 is
        probably the width of your VIM window. After echoing <width of window>
        characters VIM enters the next line so that an additional 81st character
        would have been put at the beginning of the nex line. (If you append
        "123" to LineComment you will see that "123" will start a new line and
        that there is no additional empty line.) The value of the wrap option
        is of no relevance here.

        > "let LineComment = "/\*\*/"
        > echo LineComment
        > echo a:num1 a:num2
        > echo LineComment
        > echo Tab "{"
        > echo Tab "/* ."
        > echo
        > echo Tab Tab "History:"
        > echo Tab Tab Tab expand (strftime("%d %b %y")) "- Created. SCHubbell"
        > echo Tab "*/"
        > echo Tab "} /* " a:num2 " */"
        > endfunction
        >
        >
        > If I assign LineComment to the following:
        >
        > let LineComment = "/\*\*/"
        >
        > I do not get a new line at the end. I checked to see if I had wrap set but I
        > do not. Does anyone have an idea?
        >
        > Next, I get all of this printed to the screen, but what I really want is
        > the best way to insert the text generated by my function into the current
        > line that the cursor is on? Do I need to set the function to write to a
        > buffer and then paste the buffer?

        You can return the text as the result of your function

        function TFunc(num1, num2, ...)
        let Tab = " "
        let LineComment = "/******************************************************************************/"

        let res = ''

        let res = res . LineComment
        let res = res . a:num1 a:num2
        let res = res . LineComment
        let res = res . Tab "{"
        let res = res . Tab "/* ."
        let res = res .
        let res = res . Tab Tab "History:"
        let res = res . Tab Tab Tab expand (strftime("%d %b %y")) "- Created. SCHubbell"
        let res = res . Tab "*/"
        let res = res . Tab "} /* " a:num2 " */"

        return res
        endfunction

        and insert it into your buffer by calling the function through :put

        :put =TFunc('void', 'print()')

        Note the equal sign which denotes the expression register. Everything
        on the right side of the equal sign is considered as an expression and
        its value is returned and then inserted by :put.

        Regards,
        Jürgen

        --
        Jürgen Krämer Softwareentwicklung
        Habel GmbH mailto:jkr@...
        Hinteres Öschle 2 Tel: (0 74 61) 93 53 - 15
        78604 Rietheim-Weilheim Fax: (0 74 61) 93 53 - 99
      • Sonic_Wind
        Hello Sean, Wednesday, December 1, 2004, 4:06:24 PM, you wrote: SCH Hello, SCH I was wondering if I could get some advice. I created a function and I SCH
        Message 3 of 4 , Dec 1, 2004
        • 0 Attachment
          Hello Sean,

          Wednesday, December 1, 2004, 4:06:24 PM, you wrote:

          SCH> Hello,

          SCH> I was wondering if I could get some advice. I created a function and I
          SCH> have two issues. The first issue is when I print to the screen my
          SCH> LineComment variable, I always get a new line after echoing the LineComment
          SCH> using the following function:

          SCH> function TFunc(num1, num2, ...)
          SCH> let Tab = " "

          SCH> " let LineComment =
          SCH> "/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
          SCH> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
          SCH> \*\*\*/"
          SCH> let LineComment =
          SCH> "\/*************************************************************************
          SCH> *****/"
          SCH> "let LineComment = "/\*\*/"
          SCH> echo LineComment
          SCH> echo a:num1 a:num2
          SCH> echo LineComment
          SCH> echo Tab "{"
          SCH> echo Tab "/* ."
          SCH> echo
          SCH> echo Tab Tab "History:"
          SCH> echo Tab Tab Tab expand (strftime("%d %b %y")) "- Created. SCHubbell"
          SCH> echo Tab "*/"
          SCH> echo Tab "} /* " a:num2 " */"
          SCH> endfunction


          SCH> If I assign LineComment to the following:

          SCH> let LineComment = "/\*\*/"

          SCH> I do not get a new line at the end. I checked to see if I had wrap set but I
          SCH> do not. Does anyone have an idea?
          I do not clearly understand, Do You have new line after final "/"?
          For me it works just like it have to.

          SCH> Next, I get all of this printed to the screen, but what I really want is
          SCH> the best way to insert the text generated by my function into the current
          SCH> line that the cursor is on? Do I need to set the function to write to a
          SCH> buffer and then paste the buffer?

          Try to use append() function for example:
          call append('.',LineComment) "this will add new line with value
          "of your LineComment variable next
          "to thr current line.

          see :h append()

          And yet, isn't it better to avoid indenting by the hand text that
          you have added automaticly. In this case try to use "=" normal mode
          command. for example
          let start=line('.')+1 " get the first line number that you
          " will add
          .....
          "add 12 lines (e.g. append(...))
          ....
          exec "normal ".start."G=12j"
          " this will move to the first line added by your script and
          " format all 12 lines added by you.

          --
          Best regards,
          Wind mailto:sonic_wind@...
        • Sean C. Hubbell
          Great! Thank you very much. Sean
          Message 4 of 4 , Dec 1, 2004
          • 0 Attachment
            Great! Thank you very much.

            Sean

            > -----Original Message-----
            > From: Jürgen Krämer [mailto:jkr@...]
            > Sent: Wednesday, December 01, 2004 10:19 AM
            > To: vim mailing list
            > Subject: Re: Function question
            >
            >
            >
            > Hi,
            >
            > Sean C. Hubbell schrieb:
            > >
            > > I was wondering if I could get some advice. I created a function and I
            > > have two issues. The first issue is when I print to the screen my
            > > LineComment variable, I always get a new line after echoing the
            > LineComment
            > > using the following function:
            > >
            > > function TFunc(num1, num2, ...)
            > > let Tab = " "
            > >
            > > " let LineComment =
            > >
            > "/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
            > \*\*\*\*\*
            > >
            > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
            > \*\*\*\*\*
            > > \*\*\*/"
            > > let LineComment =
            > >
            > "\/***************************************************************
            > **********
            > > *****/"
            >
            > that's because LineComment contains exactly 80 character and 80 is
            > probably the width of your VIM window. After echoing <width of window>
            > characters VIM enters the next line so that an additional 81st character
            > would have been put at the beginning of the nex line. (If you append
            > "123" to LineComment you will see that "123" will start a new line and
            > that there is no additional empty line.) The value of the wrap option
            > is of no relevance here.
            >
            > > "let LineComment = "/\*\*/"
            > > echo LineComment
            > > echo a:num1 a:num2
            > > echo LineComment
            > > echo Tab "{"
            > > echo Tab "/* ."
            > > echo
            > > echo Tab Tab "History:"
            > > echo Tab Tab Tab expand (strftime("%d %b %y")) "- Created. SCHubbell"
            > > echo Tab "*/"
            > > echo Tab "} /* " a:num2 " */"
            > > endfunction
            > >
            > >
            > > If I assign LineComment to the following:
            > >
            > > let LineComment = "/\*\*/"
            > >
            > > I do not get a new line at the end. I checked to see if I had
            > wrap set but I
            > > do not. Does anyone have an idea?
            > >
            > > Next, I get all of this printed to the screen, but what I
            > really want is
            > > the best way to insert the text generated by my function into
            > the current
            > > line that the cursor is on? Do I need to set the function to write to a
            > > buffer and then paste the buffer?
            >
            > You can return the text as the result of your function
            >
            > function TFunc(num1, num2, ...)
            > let Tab = " "
            > let LineComment =
            > "/****************************************************************
            > **************/"
            >
            > let res = ''
            >
            > let res = res . LineComment
            > let res = res . a:num1 a:num2
            > let res = res . LineComment
            > let res = res . Tab "{"
            > let res = res . Tab "/* ."
            > let res = res .
            > let res = res . Tab Tab "History:"
            > let res = res . Tab Tab Tab expand (strftime("%d %b %y")) "-
            > Created. SCHubbell"
            > let res = res . Tab "*/"
            > let res = res . Tab "} /* " a:num2 " */"
            >
            > return res
            > endfunction
            >
            > and insert it into your buffer by calling the function through :put
            >
            > :put =TFunc('void', 'print()')
            >
            > Note the equal sign which denotes the expression register. Everything
            > on the right side of the equal sign is considered as an expression and
            > its value is returned and then inserted by :put.
            >
            > Regards,
            > Jürgen
            >
            > --
            > Jürgen Krämer Softwareentwicklung
            > Habel GmbH mailto:jkr@...
            > Hinteres Öschle 2 Tel: (0 74 61) 93 53 - 15
            > 78604 Rietheim-Weilheim Fax: (0 74 61) 93 53 - 99
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.