Maintaining patterns
Hi all :) Another weird question O:) Let's suppose that I search frequently for a very complex pattern. This is easy to do using a keymap, for example. But let's assume that I also search frequently using the same pattern preceded by an arbitrary char *AND* followed by the same char. The char varies from search to search. In Perl I would store the complex pattern in some scalar, but I don't know how to do it in VimL. Probably it can be done with let and eval, but it won't work for syntax highlighting, AFAIC. Moreover, if I'm writing a syntax file and have a lot of syntax items that contains the same complex pattern preceded and followed by a character (this is only an example), that's difficult to maintain because each time I have to change the complex pattern I have to change it everywhere. Please note that this cannot be fixed modifying the pattern adding an whatever\? atom at the front and end of it, because the whatever MUST be present at BOTH ends. For the syntax highlighting, using contains solves the issue, but again, this doesn't work for searches. In the end, the problem is to be able to reuse a pattern, no matter if in a search, substitute, syntax highlighting, etc. And not, \z(\) is not a solution because it only works in start= in syntax regions. Is there a way of storing a pattern and reusing it in searchs, substitutions, syntax highlighting, etc? Thanks a lot in advance :) Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net It's my PC and I'll cry if I want to... RAmen!
Re: Maintaining patterns
DervishD wrote: Hi all :) Another weird question O:) Let's suppose that I search frequently for a very complex pattern. This is easy to do using a keymap, for example. But let's assume that I also search frequently using the same pattern preceded by an arbitrary char *AND* followed by the same char. The char varies from search to search. In Perl I would store the complex pattern in some scalar, but I don't know how to do it in VimL. Probably it can be done with let and eval, but it won't work for syntax highlighting, AFAIC. In Vim you can store the pattern in a variable or in a register. At the keyboard, you can recall a register in Command-line or Insert/Replace modes by hitting Ctrl-R followed by the register name (e.g. ^R/ to recall the latest search pattern, ^Rw to recall register w, ^R= followed by an expression then Enter to evaluate that expression, etc.) When interacting with Vim at the keyboard, you can also recall the previously used pattern from search history (using Up and Down after hitting / or ? ) and modify it on the command-line before hitting Enter (or Esc to abord) on the modified pattern. Or you can use :let to set @/ (the search register) and immediately do searches based on the new value. For instance, to repeat the latest search but as a self-standing word: :let @/ = '\' . @/ . '\' Moreover, if I'm writing a syntax file and have a lot of syntax items that contains the same complex pattern preceded and followed by a character (this is only an example), that's difficult to maintain because each time I have to change the complex pattern I have to change it everywhere. Please note that this cannot be fixed modifying the pattern adding an whatever\? atom at the front and end of it, because the whatever MUST be present at BOTH ends. Any ex-command (including the :syntax command) can be constructed as a string expression, argument of the :exe command. Part of it can be the constant part of the pattern, use the . (concatenate) operator to cancatenate strings (any of these can be variables). In :exe as in :echo, if there are more than one operands, they are concatenated with intervening spaces. Here's a silly example: let s:pattern = 'azertyuiopqsdfyghjklmwxcvbn' ... fun DefineSynMatch(groupname, FirstPart, LastPart) exe 'syn match' \ a:groupname '' \ . a:FirstPart \ . s:pattern \ . a:LastPart \ . '' endfun For the syntax highlighting, using contains solves the issue, but again, this doesn't work for searches. In the end, the problem is to be able to reuse a pattern, no matter if in a search, substitute, syntax highlighting, etc. And not, \z(\) is not a solution because it only works in start= in syntax regions. Is there a way of storing a pattern and reusing it in searchs, substitutions, syntax highlighting, etc? Thanks a lot in advance :) Raúl Núñez de Arenas Coronado Best regards, Tony.
Re: Maintaining patterns
Hi Tony :) Once more, Antoine came to the rescue :)) You're great, dude :) * A.J.Mechelynck [EMAIL PROTECTED] dixit: DervishD wrote: In Perl I would store the complex pattern in some scalar, but I don't know how to do it in VimL. Probably it can be done with let and eval, but it won't work for syntax highlighting, AFAIC. In Vim you can store the pattern in a variable or in a register. At the keyboard, you can recall a register in Command-line or Insert/Replace modes by hitting Ctrl-R followed by the register name (e.g. ^R/ to recall the latest search pattern, ^Rw to recall register w, ^R= followed by an expression then Enter to evaluate that expression, etc.) Yes, I knew this, but I thought that patterns were special in the sense that they couldn't be stored in a register (well, really I thought that only buffer text could be stored in a register). When interacting with Vim at the keyboard, you can also recall the previously used pattern from search history (using Up and Down after hitting / or ? ) and modify it on the command-line before hitting Enter (or Esc to abord) on the modified pattern. Cool! Or you can use :let to set @/ (the search register) and immediately do searches based on the new value. For instance, to repeat the latest search but as a self-standing word: :let @/ = '\' . @/ . '\' And I just forgot completely about the search register. My fault :( Moreover, if I'm writing a syntax file and have a lot of syntax items that contains the same complex pattern preceded and followed by a character (this is only an example), that's difficult to maintain because each time I have to change the complex pattern I have to change it everywhere. Please note that this cannot be fixed modifying the pattern adding an whatever\? atom at the front and end of it, because the whatever MUST be present at BOTH ends. Any ex-command (including the :syntax command) can be constructed as a string expression, argument of the :exe command. Again, I thought that syntax could be used in eval but not with exe. I definitely must study the VimL syntax in the structural level as shown in :help expression-syntax and think of any ex-command as an ex-command and not as syntax command, text manipulation commands, etc. Being vim an editor and not a programming language, I find sometimes difficult not to categorize its capabilities. I just tend to forget that VimL is a real programming language. Tony, again you've not only solved my doubt, but gave me an important lesson about VimL. I owe you yet another one. I just hope someday I will be able to return at least part of your kindness with me. Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net It's my PC and I'll cry if I want to... RAmen!
Re: Maintaining patterns
DervishD wrote: [...] Tony, again you've not only solved my doubt, but gave me an important lesson about VimL. I owe you yet another one. I just hope someday I will be able to return at least part of your kindness with me. Raúl Núñez de Arenas Coronado I used to be a teacher, and I don't believe that the best teacher is the one who gives the most students a failing note. To me, the greatest reward for a teacher lies in watching his students' successes. :-) Best regards, Tony.
Re: Maintaining patterns
Hi Tony :) * A.J.Mechelynck [EMAIL PROTECTED] dixit: DervishD wrote: [...] Tony, again you've not only solved my doubt, but gave me an important lesson about VimL. I owe you yet another one. I just hope someday I will be able to return at least part of your kindness with me. I used to be a teacher, and I don't believe that the best teacher is the one who gives the most students a failing note. To me, the greatest reward for a teacher lies in watching his students' successes. :-) I think exactly the same :) In fact, the thing I regret the most regarding vim is that I'm not proficient enough in VimL to help people in the list, and since vim is an editor and not a programming language, and so it has commands to deal with text etc. ,learning VimL won't be as easy as learning the other languages I already know. It took a month to really master Perl, studying and practicing intensively, which, in hours, was much more than the time I took to master C, for example. VimL will take much longer, I'm afraid, but I'll try my best to do it, in return for all the help I'm receiving from you and the other people in the list (Tim Chase comes to mind, but I won't give more names because I'm going to forget someone). Again, thanks a lot :) Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net It's my PC and I'll cry if I want to... RAmen!