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

139370Re: Something to wrap a selection

Expand Messages
  • Paul Isambert
    Sep 28, 2013
      Harry Putnam <reader@...> a écrit:
      > Paul Isambert <zappathustra@...> writes:
      >
      > > The code relies on an analysis of the “comments” option, which is
      > > normally set by filetype. For .conf files, I can see that the option
      > > is ambiguous (for our purpose), as it contains several characters for
      > > one-line comments, and the code picked up the wrong one (the first
      > > encountered).
      >
      > Thanks for your efforts. The script is doing what I wanted alright
      > but it appears not to know any comment char other than //.
      >
      > I named the input file:
      > some.conf
      > some.rc
      > some.bash
      > some.pl
      >
      > And every case it inserts '//'.
      >
      > Finally I opened a real, honest to god, perl script and tried it
      > there. And still it inserts '//'

      There’s obviously something wrong with filetypes or the setting of the
      ‘comments’ option. Since I don’t know where it comes from, and since
      relying on the ‘comments’ option was not very sound anyway, we’ll
      follow another course of action, see below.

      > I wonder if there is a way to make vim tell me what kind of file it
      > thinks its editing?
      >
      > Or maybe better still a way for me to tell vim what kind of file I
      > want it to be editing (temporarily at least)
      >
      > My googling keeps getting tangled up with piles of *.vim files,
      > echoing $RUNTIME blah blah and so on and so on. Not finding a definitive
      > way to choke it out of vim for the current file.
      >
      > Just typing :Filetype lets me know:
      > filetype detection:ON plugin:ON indent:ON
      >
      > But how can I coax vim into telling me what the current filetype is?

      Use ‘:echo &filetype’ to see the buffer’s filetype, and ‘:echo &comments’
      for the comments (and ‘:echo &option’ in general for any Vim option).
      The ‘comments’ option might be a bit cryptic, see ‘:help format-comments’
      if you want to understand it – but the code below doesn’t rely on all
      that anyway.

      > Or barring all that maybe just make the script insert octathorps in
      > all cases. At least that would be useful most of the time.

      Let’s not give up! What you’re asking for is really not complicated, I
      just shouldn’t have relied on ‘comments’ to begin with (the option is
      not meant to insert comments, but to format commented paragraph
      properly; it doesn’t have to be unambiguous for its own purpose).

      The new command works as follows; type:

      :MarkChange {optional keywords}

      (The keywords work as in the previous versions of the code.)

      If no comment sign is recorded for the current filetype, the code will
      analyze the ‘comments’ option and ask you whether the character(s) it
      found are ok; if not, you can give it something else. Then you will be
      asked whether you want that comment to be remembered, and it can be
      remembered either for the current session only (until you exit Vim) or
      until the end of time (more or less); in the latter case, an external
      file will be used to remember comments from session to session. If a
      comment is remembered for a given filetype, you will not be asked
      again to confirm whether it’s ok or not, unless you use:

      :MarkChange! {optional keywords}

      which asks for confirmation and allows you to change the comment
      again, as if none were recorded.

      (Not using a recorded comment and/or not remembering the comment you
      will use may be useful for files containing several languages; for
      instance, a vimscript file can contain snippets of Lua or Ruby or
      whatever, which you’ll comment with a special character, but you’ll
      probably want to keep the default Vimscript comment.)

      The external file used to record comments is created the first time
      you want a comment to be definitely remembered. It will be called
      ‘.markchangerc’, and you’ll be able to modify it by hand. It will be
      located in the same directory as the file where the ‘:MarkChange’
      command is defined; if that is in your .vimrc file, then
      ‘.markchangerc’ will be where your .vimrc file is ($HOME, I should
      guess). Actually, I guess you’d better put the entire code in a file
      of its own (say ‘markchange.vim’) and put that file in ‘.vim/plugin/’
      (itself in $HOME by default), so the ‘.markchangerc’ file will be
      there too and won’t puzzle you a few years hence when you find it in
      $HOME, and you’ll be able to delete all relevant files when the code
      doesn’t suit your needs anymore. Better yet, use ‘pathogen’ (see
      http://www.vim.org/scripts/script.php?script_id=2332).

      I say goodbye here, the code follows. I’ll hope you’ll be satisfied
      this time (there might be glitches, I did not test the command much,
      just basic tries).

      Best,
      Paul

      " Put this somewhere.
      let s:markchangecomments = {}

      let s:commentfile = expand("<sfile>:p:h") . "/.markchangerc"
      if filereadable(s:commentfile)
      let s:data = readfile(s:commentfile)
      for line in s:data
      if line !~ '^#' && line =~ '='
      let s:matches = matchlist(line, '\([^=[:blank:]]\+\)\s*=\s*\(\S\+\)')
      if len(s:matches)
      let s:markchangecomments[s:matches[1]] = s:matches[2]
      endif
      endif
      endfor
      endif

      function! s:markchange (kw, confirm) range
      if has_key(s:markchangecomments, &filetype)
      let comment = s:markchangecomments[&filetype]
      else
      " Retrieve the one-line comment string, hoping it exists (could be
      " defined otherwise, too).
      let comment = matchstr(&comments, '[[:alnum:]]\@<!:\zs[^,]*\ze')
      endif

      " Confirm comment.
      if !has_key(s:markchangecomments, &filetype) || a:confirm
      let newcomment = input("I will use "
      \ . comment
      \ . " to comment lines. Press <Enter> if it is ok, or type in what you wish to use.\n")
      if newcomment =~ '\S'
      let comment = newcomment
      endif
      let remember = input("\nShall I remember this comment? [y{es}, n{o}, o{nly for this session}]\n")
      while 1
      if remember =~ '^y'
      let s:markchangecomments[&filetype] = comment
      " Remember the comment in an external file.
      let cms = ['# Format of this file:', '# {filetype} = {comment}', '']
      for k in keys(s:markchangecomments)
      let cms += [k . ' = ' . s:markchangecomments[k]]
      endfor
      call writefile(cms, s:commentfile)
      elseif remember =~ '^o'
      let s:markchangecomments[&filetype] = comment
      endif
      if remember =~ '^[yno]'
      break
      else
      let remember = input("\nCould you please repeat? [y{es}, n{o}, o{nly for this session}]\n")
      endif
      endwhile
      redraw
      endif
      let comment .= " "

      " Ask for keywords if not given when calling MarkChange.
      if a:kw !~ '\S'
      let keywords = input("Keywords, please?\n")
      else
      let keywords = a:kw
      endif
      let keywords = "Keywords: " . keywords

      " Append the end symbol.
      call append(a:lastline, "&&")

      " Append the keywords and date lines; use :undojoin so ``u'' will undo
      " the entire operation.
      undojoin
      call append(a:firstline-1, [keywords, strftime("[Keydate: %c.]")])

      " Comment everything.
      let i = a:firstline
      while i <= a:lastline+3
      call setline(i, comment . getline(i))
      let i += 1
      endwhile
      endfunction

      com! -bang -range -nargs=* MarkChange <line1>,<line2>call s:markchange(<q-args>, <bang>0)
      " End of code.

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Show all 10 messages in this topic