Wednesday, February 28, 2024

Re: surrounding a word with tag with a dynamic attribute

I forgot to say that I was hoping that using the "repeat last" function, namely the ".", would apply that modified command at the next text location where a new string is highlited.

Eric

On Wednesday 28 February 2024 at 16:12:30 UTC-5 Eric Marceau wrote:
Also,

Can someone offer the modified version of that which would permit specifying the desired single tag-string (i.e. 'em') as a parameter for the function call, instead of the open-ended user entry of a string?

I visualize doing something like   ':TagSelection em' for the action to be applied without further interaction.  Is that possible?

Thank you in advance for anyone's contributing that modified code.

Eric

On Wednesday 28 February 2024 at 15:45:11 UTC-5 Eric Marceau wrote:
Hi David,

Your posting of the tip dates quite a while ago.

Any idea if that would still work for vim/gvim 8.1 ?

Is that added to the ~/.vim/vimrc  ?

Finally, is that action triggered by typing the sequence   ':TagSelection'   ?

Thank you,

Eric

On Wednesday 5 May 2010 at 10:23:52 UTC-4 David Fishburn wrote:
On Tue, May 4, 2010 at 7:23 PM, discipulus <ger...@gmail.com> wrote:
...
> It should have been {span class="sX"}discipulus{/span}
>
> I wrote this function just now:
>
> function! mytest()
>  let l:current_column=col('.')
>  exe 'normal b'
>  let l:beginning_column=col('.')
>  let l:diff = l:current_column - l:beginning_column + 1
> " this relies on the surround plugin
>  exe 'normal vs'
> endfunction
>
> What do you think and thanks for your response!

I wrote my own function specifically to wrap XML tags around some
highlighted text. If the text is part of a line I wrap it inline. If
the text (visually selected) spans multiple lines, then I add the tags
on new lines before and after the text.

I also allow you to add attributes and remove those when adding the closing tag.

" Tip #346: Tag Select/Wrapper:/*{{{*/
" http://vim.sourceforge.net/tips/tip.php?tip_id=346
" Author: David Fishburn
" These mappings and TagSelection function will allow you to place
" an XML tag around either the current word, or the current selected
" text.
" If the visual select is on a single line, the tag is wrapped
" around the text <this>way</this>. If the visual select extends
" over multiple lines, the tag is wrapped around the text
" <this>
" way
" </this>
"
" When you are prompted for the tag name, you can enter:
" Tag name? p class="classname" attri="bute"
" The select is wrapped with:
" <p class="classname" attri="bute">
" Your selection
" </p>
" Notice the attributes have been stripped from the closing tag.
"
" Use nmap, not nnoremap, since we do want to use an existing mapping
nmap ,,, viw,,,
vnoremap ,,, <Esc>:call TagSelection()<CR>

function! TagSelection()
let tag = input("Tag name (include attributes)? ")

if strlen(tag) == 0
return
endif

" Save b register
let saveB = @b
" <C-R> seems to automatically reindent the line for some filetypes
" this will disable it until we have applied our changes
let saveIndent = &indentexpr
let curl = line(".")
let curc = col(".")
let &indentexpr = ''

" If the visual selection is over multiple lines, then place the
" data between the tags on newlines:
" <tag>
" data
" </tag>
let newline = ''
if getline("'>") != getline("'<")
let newline = "\n"
let curl = line("'>")
endif

" Strip off all but the first word in the tag for the end tag
let @b = newline . substitute( tag, '^[ \t"]*\(\<\S*\>\).*', '<\/\1>\e', "" )
let curc = curc + strlen(@b)
exec "normal `>a\<C-R>b"

let @b = substitute( tag, '^[ \t"]*\(\<.*\)', '<\1>\e', "" ) . newline
let curc = curc + strlen(@b)
exec "normal `<i\<C-R>b"

" Now format the area
exec "normal `<V'>j="

" Restore b register
let @b = saveB
let &indentexpr = saveIndent

call cursor(curl, curc)
endfunction
" /*}}}*/


If it is of any use to you.

Enjoy.
Dave

--
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 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@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/d09dba03-dde5-4a98-aa32-7a125f06d837n%40googlegroups.com.

No comments: