Skip to content

Instantly share code, notes, and snippets.

@kana
Created November 5, 2011 18:34
Show Gist options
  • Save kana/1341862 to your computer and use it in GitHub Desktop.
Save kana/1341862 to your computer and use it in GitHub Desktop.
smartchr.xire
(function (smartchr#loop ...)
(return (smartchr#_expand #t ...)))
(function (smartchr#one_of ...)
(return (smartchr#_expand #f ...)))
(define s:DEFAULT-CONTEXT (dict))
(function (smartchr#_expand loop-p args)
(let* ([last-arg (ref args -1)]
[context (if (s:context? last-arg)
last-arg
s:DEFAULT-CONTEXT)]
[%literals (if (s:context? last-arg)
(slice args 0 -2)
args)]
[literals (if loop-p
(+ %literals (list (ref %literals 0)))
%literals)])
(when (s:in-valid-context? context)
(for i (range (- (len literals) 1) 1 -1)
(let ([literal1 (ref literals i)]
[literal2 (ref literals (- i 1))])
(when (s:cursor-preceded-with? literal2)
(return (.. (if (pumvisible) (kbd "<C-e>") "")
(repeat (kbd "<BS>") (len literal2))
literal1)))))
(return (ref literals 0)))
(return (get context "fallback" (ref %literals -1)))))
(function (smartchr#_scope)
(return s:))
(function (smartchr#_sid)
(return (maparg (kbd "<SID>") "n")))
;(nnoremap "<SID>" "<SID>")
(function (s:context? value)
(return (== (type value) (type (dict)))))
(function (s:cursor-preceded-with? s)
(if (==# (ref (mode) 0) "c")
; Command-line mode.
; getcmdpos() is 1-origin and we want to the position of the character
; just before the cursor.
(let ([p (- (getcmdpos) 1 1)]
[l (len s)])
(return (and (<= l (+ p 1))
(==# (slice (getcmdline) (+ (- p l) 1) p) s))))
; Insert mode and other modes except Commnd-line mode.
(return (search (.. #/\V/ (escape s "\\") #/\%#/) "bcn"))))
(function (s:in-valid-context? context)
(if (has_key context "ctype")
(return (if (==# (mode) "c")
(<= 0 (stridx (-> context ctype) (getcmdtype)))
#t))
; Valild context is not specified - smartchr is enabled in any context.
(return #t)))
; vim: filetype=scheme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment