Commits

ZyX_I committed c2061c2

Added fallback for escape and unescape functions: in case s/\= cannot be used

  • Participants
  • Parent commits 2b3270f

Comments (0)

Files changed (1)

File plugin/aurum/cmdutils.vim

             \'`': "'", '$': 'd',
         \}
 let s:epattern='\v['.escape(join(keys(s:ecodes), ''), '\]-^').']'
+let s:fbecodes=copy(s:ecodes)
+unlet s:fbecodes['%']
 function s:F.oescape(str)
-    " TODO fallback in case of being inside \=
-    return substitute(a:str, s:epattern, '\="%".s:ecodes[submatch(0)]', 'g')
+    if substitute('a', '.', '\="a"', '') is# 'a'
+        return substitute(a:str, s:epattern, '\="%".s:ecodes[submatch(0)]', 'g')
+    else
+        let r=substitute(a:str, '\V%', '%%', 'g')
+        for [c, l] in items(s:fbecodes)
+            let r=substitute(r, '\V'.escape(c, '\'), '%'.escape(l, '\~&'), 'g')
+        endfor
+        return r
+    endif
 endfunction
 "▶1 ounescape :: String → String
 let s:unecodes={}
 call map(copy(s:ecodes), 'extend(s:unecodes, {v:val : v:key}, "error")')
 let s:unepattern='\V%\(\['.escape(join(keys(s:unecodes), ''), '\]-^').']\)'
 function s:F.ounescape(str)
-    let str=tr(a:str, '\', '/')
-    return substitute(str, s:unepattern, '\=s:unecodes[submatch(1)]', 'g')
+    if substitute('a', '.', '\="a"', '') is# 'a'
+        return substitute(a:str, s:unepattern, '\=s:unecodes[submatch(1)]', 'g')
+    else
+        let lstr=len(a:str)
+        let i=0
+        let r=''
+        while i<lstr
+            let idx=stridx(a:str, '%', i)
+            if idx==-1
+                let r.=a:str[(i):]
+                break
+            else
+                if idx
+                    let r.=a:str[(i):(idx-1)]
+                endif
+                let r.=s:unecodes[a:str[idx+1]]
+                let i=idx+2
+            endif
+        endwhile
+        return r
+    endif
 endfunction
 "▶1 encodeopts :: opts → String
 let s:ignkeys=['crrestrict', 'filepats', 'revs', 'cmd', 'repo']