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

Re: Smart keys

Expand Messages
  • Benji Fisher
    ... [body of if/endif snipped] ... This gets to be an unmanageably large function. IMHO, it is better to split it up into a function/keymap that provides a
    Message 1 of 18 , Jul 31, 2002
    • 0 Attachment
      David Fishburn wrote:
      >
      > The reason I took the function approach was to extend this a bit more.
      > Without knowing the syntax of how to write the script (yet) I was going
      > to do something like this:
      >
      [body of if/endif snipped]
      > function SmartKeys()
      > exe 'silent norm! "zyiw'
      > echo @z
      > if 'if' ==? @z
      > if filetype = c
      > elseif filetype = sql
      > endif
      > elseif 'while' ==?@z || ...
      > if filetype = c
      > elseif filetype = sql
      > endif
      > endif
      > endfunction
      > imap <A-a> <Esc>:call SmartKeys()<CR>

      This gets to be an unmanageably large function. IMHO, it is better to
      split it up into a function/keymap that provides a framework, and give the
      details in separate ftplugin files. See
      http://vim.sourceforge.net/scripts/script.php?script_id=243
      for one approach. (If you like this script, please rate it so that it is easier
      for others to find.)

      HTH --Benji Fisher
    • Benji Fisher
      ... if exists( expansion_ . @z) ... I think you want to add a test as above, and do something else if the variable has not been defined. (I have not tested
      Message 2 of 18 , Jul 31, 2002
      • 0 Attachment
        "HALIM,SALMAN (HP-NewJersey,ex2)" wrote:
        >
        > Your approach is the one I would use, with a slight modification; I'm
        > opposed to large if/else blocks if they are avoidable. Vim lets you
        > create variables of the form varName{expr} where expr is a variable or
        > an expression. In your particular case, you could create a variable
        > called expansion_if:
        >
        > let expansion_if = "silent norm! ea() {\<CR>}\<Esc>?(\<CR>a"
        >
        > Then, change your function to something like:
        >
        > execute 'silent normal! "zyiw'
        > echo @z
        if exists("expansion_" . @z)
        > execute expansion_{@z}
        >
        > This will let you create a variable called expansion_while,
        > expansion_for etc.

        I think you want to add a test as above, and do something else if the
        variable has not been defined. (I have not tested this...)

        HTH --Benji Fisher
      • Aldrich, Timothy
        I wrote a script to do this very thing : basically I made small snippet files for each keyword for each language. Then when you type a keyword and hit a key
        Message 3 of 18 , Jul 31, 2002
        • 0 Attachment
          I wrote a script to do this very thing :
          basically I made small "snippet files" for
          each keyword for each language. Then when you
          type a keyword and hit a key ( I use CTRL-S )
          the script :read 's the snippet file. I've added
          some basic replacements including :
          - where the cursor should be
          - insert date
          - insert filename
          ...

          if anyone's interested I could post it here or on
          vim.sf.net

          This message is for the named person's use only. It may contain sensitive and private proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you are not the intended recipient, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. CREDIT SUISSE GROUP and each legal entity in the CREDIT SUISSE FIRST BOSTON or CREDIT SUISSE ASSET MANAGEMENT business units of CREDIT SUISSE FIRST BOSTON reserve the right to monitor all e-mail communications through its networks. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
          Unless otherwise stated, any pricing information given in this message is indicative only, is subject to change and does not constitute an offer to deal at any price quoted. Any reference to the terms of executed transactions should be treated as preliminary only and subject to our formal written confirmation.
        • Charles E. Campbell
          Hello! I ve been using a keyword+backtick imap which fires off a function. Check out http://www.erols.com/astronaut/vim/vimscript/drcstubs60.vim which is
          Message 4 of 18 , Jul 31, 2002
          • 0 Attachment
            Hello!

            I've been using a keyword+backtick imap which fires off
            a function. Check out

            http://www.erols.com/astronaut/vim/vimscript/drcstubs60.vim

            which is discussed under

            http://www.erols.com/astronaut/vim/index.html#CAbbrv

            Regards,
            Chip Campbell

            --
            Charles E Campbell, Jr, PhD _ __ __
            Goddard Space Flight Center / /_/\_\_/ /
            cec@... /_/ \/_//_/
            PGP public key: http://www.erols.com/astronaut/pgp.html
          • David Fishburn
            Thanks to all the suggestions, I now have this working. See function and mapping below. Since these variables are essentially static: let expansion_if_c =
            Message 5 of 18 , Jul 31, 2002
            • 0 Attachment
              Thanks to all the suggestions, I now have this working. See function
              and mapping below.

              Since these variables are essentially static:
              let expansion_if_c = "silent norm! ea() {\<CR>}\<Esc>?)\<CR>"

              How would you suggest I set this up, it seems a waste to define them
              each time I enter the function.

              Should I specify them outside of the funtion in my _vimrc file, and then
              have the function reference them?
              If I do that, how do I define them outside of the function, so that the
              function can still reference them?
              Should I use global variables?

              Thanks,
              Dave


              function SmartKeys()
              let l:reg_z = @z
              exe 'silent norm! "zyiw'
              "exe 'silent norm!
              ?<\(select\|update\|create\|grant\|delete\|alter\|call\|exec\|insert\)\>
              '"\n".'v/;'."\n".'"zy'
              echo @z
              let expansion_if_c = "silent norm! ea() {\<CR>}\<Esc>?)\<CR>"
              let expansion_if_java = expansion_if_c
              let expansion_if_sql = "silent norm! ceIF() THEN\<CR>END
              IF;\<Esc>?)\<CR>"
              let expansion_while_c = "silent norm! ea() {\<CR>}\<Esc>?)\<CR>"
              let expansion_while_java = expansion_while_c
              let expansion_while_sql = "silent norm! ceWHILE() LOOP\<CR>END
              LOOP;\<Esc>?)\<CR>"
              let expansion_try_c = "silent norm! ea() {\<CR>}\<Esc>?)\<CR>"
              let expansion_try_java = "silent norm! ea() {\<CR>}\<CR>catch(
              Exception e ) {\<CR>}\<Esc>?)\<CR>n"
              let expansion_out_java = "silent norm!
              ceSystem.out.println();\<Left>\<Left>\<Left>"
              let expansion_err_java = "silent norm!
              ceSystem.err.println();\<Left>\<Left>\<Left>i"
              let expansion_for_c = "silent norm! ea( int i; i < ?; i++ )
              {\<CR>}\<Up>"
              "let expansion_for_c = "silent norm! ea( int i; i < ?; i++ )
              {\<CR>}\<Esc>?)\<CR>"
              let expansion_for_java = expansion_for_c
              let expansion_for_sql = "silent norm! ceFOR loop_name AS
              cursor_name\<CR>CURSOR FOR\<CR>SELECT ...\<CR>FROM ...\<CR>WHERE
              ...\<CR>FOR READ ONLY\<CR>DO\<CR>END FOR;"
              "let expansion_{ = "silent norm! ea\<CR>\<CR>}\<Up>"

              if exists( "expansion_" . @z . "_" . &filetype )
              execute expansion_{@z}_{&filetype}
              endif
              endfunction
              imap <A-a> <C-o>:call SmartKeys()<CR>


              -----Original Message-----
              From: Benji Fisher [mailto:benji@...]
              Sent: Wednesday, July 31, 2002 9:46 AM
              To: 'vim@...'
              Subject: Re: Smart keys
            • Luc Hermitte
              ... Once in ftplugins. For instance, the file ftplugin/c/c-SmartKeywords.vim if exist( b:c_SKwds_loaded ) | finish | endif let b:c_SKwds_loaded = 1 let
              Message 6 of 18 , Jul 31, 2002
              • 0 Attachment
                On Wed, Jul 31, 2002 at 10:36:59AM -0400, David Fishburn <fishburn@...> wrote:
                >
                > Thanks to all the suggestions, I now have this working. See function
                > and mapping below.
                >
                > Since these variables are essentially static:
                > let expansion_if_c = "silent norm! ea() {\<CR>}\<Esc>?)\<CR>"
                >
                > How would you suggest I set this up, it seems a waste to define them
                > each time I enter the function.
                Once in ftplugins. For instance, the file ftplugin/c/c-SmartKeywords.vim
                if exist('b:c_SKwds_loaded') | finish | endif
                let b:c_SKwds_loaded = 1

                let b:expansion_if = '...'
                let b:expansion_while = '...'

                and so on.

                You will not need to redefine them for java, C++ and other C like
                languages if other java, cpp, ... fplugin don't forget to execute
                runtime! ftplugin/c/*.vim ftplugin/c-*.vim ftplugin/c.vim


                > Should I specify them outside of the funtion in my _vimrc file, and
                > then have the function reference them?
                No.

                > If I do that, how do I define them outside of the function, so that
                > the function can still reference them?

                > Should I use global variables?
                Don't like the idea.

                > function SmartKeys()
                > let l:reg_z = @z
                > exe 'silent norm! "zyiw'
                > "exe 'silent norm!
                > ?<\(select\|update\|create\|grant\|delete\|alter\|call\|exec\|insert\)\>
                > '"\n".'v/;'."\n".'"zy'
                > echo @z
                >
                CHANGES HERE :
                if exists( "b:expansion_" . @z )
                execute b:expansion_{@z}
                > endif
                > endfunction
                > imap <A-a> <C-o>:call SmartKeys()<CR>

                --
                Luc Hermitte
                PS: did you take a look at other approaches on this issue ?
              • Benji Fisher
                ... Instead of using ftplugin/c/c-SmartKeywords.vim you could also use ftplugin/c/SmartKeywords.vim or ftplugin/c_SmartKeywords.vim . HTH --Benji Fisher
                Message 7 of 18 , Jul 31, 2002
                • 0 Attachment
                  Luc Hermitte wrote:
                  >
                  > Once in ftplugins. For instance, the file ftplugin/c/c-SmartKeywords.vim
                  > if exist('b:c_SKwds_loaded') | finish | endif
                  > let b:c_SKwds_loaded = 1
                  >
                  > let b:expansion_if = '...'
                  > let b:expansion_while = '...'
                  >
                  > and so on.

                  Instead of using ftplugin/c/c-SmartKeywords.vim you could also use
                  ftplugin/c/SmartKeywords.vim or ftplugin/c_SmartKeywords.vim .

                  HTH --Benji Fisher
                • Luc Hermitte
                  ... On this one, I think it is a bad idea and a very bad habbit with VIM. This way, he will end up with (too) many different files having the same name -- the
                  Message 8 of 18 , Jul 31, 2002
                  • 0 Attachment
                    On Wed, Jul 31, 2002 at 01:09:49PM -0400, Benji Fisher <benji@...> wrote:
                    > Instead of using ftplugin/c/c-SmartKeywords.vim you could also use
                    > ftplugin/c/SmartKeywords.vim or

                    On this one, I think it is a bad idea and a very bad habbit with VIM.
                    This way, he will end up with (too) many different files having the same
                    name -- the only difference beeing their location.

                    I may be the only one here to think that having
                    ftplugin/c.vim
                    syntax/c.vim
                    indent/c.vim
                    fold/c.vim -- not official, but likely and interresting
                    evolution of vim
                    is wrong. Fortunately, as aware VIM users we can tell what to do with
                    all these files...


                    > ftplugin/c_SmartKeywords.vim .

                    --
                    Luc Hermitte
                  Your message has been successfully submitted and would be delivered to recipients shortly.