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

46527RE: Quickest way to lowercase all characters in a file EXCEPT those in single quotes?

Expand Messages
  • David Fishburn
    Jan 7, 2004
    • 0 Attachment
      <plug>
      I have 2 plugins that can be of use here:

      " SQLUtilities:
      " http://www.vim.org/script.php?script_id=492
      This will reformat SQL statement for you, when you visually select them.
      Note, this will not convert keywords to upper case.
      But if you are doing a lot of SQL work, I always reformat SQL statements.


      " SrchRplcHiGrp - Search and replace based on a syntax highlight group :
      " http://www.vim.org/script.php?script_id=848

      This is closer to what Dr. Chip pointed out.
      Assuming you are using syntax highlighting, then you can use this plugin.

      Your *exact* request is the reason I wrote this plugin in the first place.
      I wanted to convert (SELECT, INTO, FROM, WHERE, ...) to UPPER case.
      You put your cursor on a keyword and run:

      :SRChooseHiGrp
      Visually select the area you want
      :SRHiGrp ( or :SRHiGrp '\(\w\+\>\)', '\U\1' )

      The defaults will uppercase all keywords.
      You can do the same sequence to lower case all quoted areas.
      :SRHiGrp '\(\w\+\>\)', '\L\1'

      </plug>

      Dave


      > -----Original Message-----
      > From: Dennis W. Disney [mailto:ddisney@...]
      > Sent: Friday, January 02, 2004 10:28 AM
      > To: vim-ML
      > Cc: Benji Fisher; Roberto Bonvallet
      > Subject: Re: Quickest way to lowercase all characters in a
      > file EXCEPT those in single quotes?
      >
      >
      > I asked earlier what is the quickest way to lowercase all
      > characters in a file EXPECT those in single quotes. Let me
      > provide a bit more background - I am trying to write
      > something to reformat SQL generated by a reporting tool. It
      > generates SQL that looks like: SELECT
      > PRODUCT.PRODUCT_NAME,
      > STORE.REGION,
      > SUM(SALES.AMOUNT)
      > FROM
      > PRODUCT,
      > STORE,
      > SALES
      > WHERE
      > ( PRODUCT.PRODUCT_ID=SALES.PRODUCT_ID )
      > AND ( STORE.STORE_ID=SALES.STORE_ID )
      > AND ( PRODUCT.CATEGORY='BEER' )
      > GROUP BY
      > PRODUCT.PRODUCT_NAME,
      > STORE.REGION
      >
      > And I want to reformat the SQL to look like:
      > SELECT product.product_name,
      > store.region,
      > SUM(sales.amount)
      > FROM product,
      > store,
      > sales
      > WHERE product.product_id=sales.product_id
      > AND store.store_id=sales.store_id
      > AND product.category='BEER'
      > GROUP BY product.product_name,
      > store.region
      >
      > My plan is to convert everything that is not between a pair
      > of single quotes into lowercase, then change all of the SQL
      > keywords into uppercase. Next, remove the pointless
      > parenthesis in the WHERE and AND lines. Then, go through the
      > file to get the words lined up as I like.
      >
      >
      > I was going to do this as a plug in, but I couldn't get the
      > plugin to use the \< and \> atoms, which I need to find the
      > SQL keywords. Now, I plan on writing a script file and
      > executing that.
      >
      > Back to the initial problem of putting everything in
      > lowercase characters EXCEPT those in single quotes. Benji
      > Fisher suggested:
      > ******************************************************************
      > qqv/\('\|\%$\)<CR>unnq
      >
      > and then 999@q (depending on the length of your file).
      >
      > qq Record into register q (easier, I find, than qa or qx or ...)
      > v Enter Visual mode (characterwise).
      > /\('\|\%$\)<CR> Find the next 'single quote' or the end
      > of the file.
      > u Make lower case, return to Normal mode at start of selection.
      > nn Go to the ' (or EOF) that you just found, and then the next one.
      > q End recording
      > 999@q Repeat the macro recorder into register @q lots of times.
      > ******************************************************************
      >
      > I don't understand what the parentheis in the >>
      > /\('\|\%$\)<CR> << is suppose to do. If I change that
      > portion to >> /\'\|\%$<CR> <<, I almost get what I want. It
      > works great as I go through the file, but then it wraps
      > around and starts through the file again. The second time
      > through it changes to lowercase all of the text between the
      > single quotes. I thought I would change it use a while loop,
      > but I don't know how to determine if I am at the end of the file.
      >
      > Roberto Bonvallet suggested:
      > ******************************************************************
      > qqguf';;q:1,$norm 99@q
      > :1,$norm $guF'
      >
      > Yeah, I know he said 'quickest way', so what? :)
      >
      > Mine works for files of any number of lines, but there can't be more
      > than 99 quoted strings per line. There also can't be quoted
      > string which start in one line and end in another.
      >
      > Explanation:
      > qq starts recording
      > gu waits to lowercase on move
      > f' goes to next '
      > ;; repeat last move twice
      > q stops recording
      > :1,$norm 99@q executes register q 99 times in every line
      > :1,$norm $guF' lowercases from the end of each line to the last
      > ******************************************************************
      > Unfortunately, this doesn't work for lines without single
      > quotes. The f' doesn't find anything, so no characters get
      > changed to lower case.
      >
      > Any help would be greatly appreciated.
      >
      > DWD
      >
      > __________________________________
      > Do you Yahoo!?
      > Find out what made the Top Yahoo! Searches of 2003
      > http://search.yahoo.com/top2003
      >
    • Show all 19 messages in this topic