Skip to content

Commit

Permalink
l-snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ran9er committed Feb 18, 2013
1 parent b04c460 commit afd23df
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 84 deletions.
3 changes: 2 additions & 1 deletion _autoload-conf/skeleton-pair.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(setq skeleton-pair-cond-alist
'(
((char-bf '(?$ ?=)) . (?\{ _ "}"))
((char-bf '(?+ ?-)) . (?\\ "+" _ "+\\"))
((or (char-bf ?/)(char-bf ?=)) . (?\[ n _ n "]"))
((bolp) . (?/ "*" n _ n "*/"))
(t . (?/ _))
((bolp) . (?. -1 "->"))
(t . (?. _))
))
(skeleton-pair-alist-update)

Expand Down
219 changes: 144 additions & 75 deletions _lib/l-snippets.el
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(setq l-snippets-repo (expand-file-name "sandbox/repo" *init-dir*))
;; * init
(add-to-list 'debug-ignored-errors "^Beginning of buffer$")
(add-to-list 'debug-ignored-errors "^End of buffer$")
Expand Down Expand Up @@ -48,13 +49,20 @@ l-interactive set to nil."
(defun l-snippets-temp-name (make-temp-name (format "--%s-"(buffer-name))))

(defvar l-snippets-syntax-table
'(head "\\$" open "{" close "}" delimiter ":" path-separator "%%"))
'(head "\\$" open "{" close "}" delimiter ":" path-separator "%%" id "[[:digit:]]+"))

(defvar l-snippets-token-regexp
(format "\\(%s%s[[:digit:]]+\\)\\|\\(%s[[:digit:]]+\\)"
(plist-get l-snippets-syntax-table 'head)
(plist-get l-snippets-syntax-table 'open)
(plist-get l-snippets-syntax-table 'head)))
(defmacro l-snippets-gen-regexp (str &rest tags)
`(format
,str
,@(mapcar
(lambda(x)
(plist-get l-snippets-syntax-table x))
tags)))

(defvar l-snippets-token-regexp-open
(l-snippets-gen-regexp
"\\(%s%s%s\\)\\|\\(%s%s\\)"
head open id head id))

(defvar l-snippets-enable-overlays-pool nil)

Expand Down Expand Up @@ -137,11 +145,9 @@ l-interactive set to nil."

(defun l-snippets-overlay-update-text (ov text)
(let ((beg (overlay-start ov)))
;; (goto-char beg)
;; (insert text)
;; (delete-region (point) (overlay-end ov))
(goto-char beg)
(delete-char (- (overlay-end ov) beg))
;; (delete-char (- (overlay-end ov) beg))
(delete-region beg (overlay-end ov))
(insert text)
(move-overlay ov beg (point))))

Expand Down Expand Up @@ -177,14 +183,89 @@ l-interactive set to nil."
(defun l-snippets-previous-field ()
(interactive))

;; * index
(defun l-snippets-read-index (idx)
(let (var)
(with-current-buffer
(let ((enable-local-variables nil))
(find-file-noselect idx))
(prog2
(goto-char (point-min))
(setq var
(condition-case err
(read (current-buffer))
(error
nil)))
(kill-buffer (current-buffer))))))

(defun l-snippets-update-index ()
(let ((mtime (lambda(x)(nth 5 (file-attributes x))))
(l-snippets-index-file
(expand-file-name
l-snippets-index-file
(file-name-directory
l-snippets-repo))))
(if (time-less-p
(or (funcall mtime l-snippets-index-file)
'(0 0 0))
(funcall mtime l-snippets-repo))
(with-temp-file
(let ((enable-local-variables nil)
(find-file-hook nil))
l-snippets-index-file)
(insert (pp-to-string
(directory-files l-snippets-repo nil "^[^_].*\\'")))
(message (format "Save %s." l-snippets-index-file))))
(l-snippets-read-index l-snippets-index-file)))

(setq l-snippets-index
(l-snippets-update-index))

(defun l-snippets-convert-to-snippet-name (abbrev)
(mapconcat 'symbol-name
(list major-mode abbrev)
(plist-get
l-snippets-syntax-table
'path-separator)))

(defun l-snippets-get-snippet (snippet)
(or
(gethash snippet l-snippets-cache)
(if (member snippet l-snippets-index)
(puthash snippet
(l-snippets-gen-token
(expand-file-name snippet l-snippets-repo))
l-snippets-cache)
(message (format "%s is not define" snippet)))))

;; * prase
(defun l-snippets-to-alist (lst)
(if lst
(cons
(cons (nth 0 lst) (nth 1 lst))
(l-snippets-to-alist (nthcdr 2 lst)))))

(defun l-snippets-find-close-parenthesis (x y)
(defun l-snippets-make-lst (n)
(let* ((i n)(x nil))
(while (> i 0)
(setq x (cons i x))
(setq i (1- i)))
x))

(defun l-snippets-search-str (regexp str &rest sub)
(let (result)
(with-temp-buffer
(insert str)
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(setq
result
(cons
(mapcar (lambda(x)(match-string x)) sub)
result))))
result))

(defun l-snippets-find-close-paren (x y)
(let ((count 1)
open close)
(save-excursion
Expand Down Expand Up @@ -218,22 +299,65 @@ l-interactive set to nil."
(cons
(match-beginning 0)
(if (match-beginning 1)
(l-snippets-find-close-parenthesis
(l-snippets-find-close-paren
(plist-get l-snippets-syntax-table 'open)
(plist-get l-snippets-syntax-table 'close))
(match-end 2))) a)))
(match-end 2)))
a)))
a)))))

(defvar l-snippets-token-defines
(list
;; "${1:$(eval)}"
(list
2
(l-snippets-gen-regexp "%s%s\\(%s\\)%s%s\\(.*\\)%s" head open id delimiter head close))
;; "${1:prompt}"
(list
2
(l-snippets-gen-regexp "%s%s\\(%s\\)%s\\(.*\\)%s" head open id delimiter close))
;; ${(eval)}
(list
1
(l-snippets-gen-regexp "%s%s\\(.*\\)%s" head open close))
;; "$1"
(list
1
(l-snippets-gen-regexp "%s\\(%s\\)" head id))
))

(defun mk-lg-lst (x)
(let ((l (lambda(i n r)
(if (< i n)
(funcall l (1+ i) n (cons (* (car r) 2) r))
r))))
(funcall l 1 x '(1))))

(defun l-snippets-token-property (n)
;; I=>id P=>prompt E=>exp R=>rest
(mapcar (lambda(x) (if (zerop (logand n x)) nil t))
(mk-lg-lst 8)))

(defun l-snippets-prase-token (str)
(let ((str (substring-no-properties str 1))
(s1 (substring-no-properties str 1)))
(cond
((equal s1 (plist-get l-snippets-syntax-table 'open))
)
)))
(let ((len (length l-snippets-token-defines))
(x 0) result)
(with-temp-buffer
(insert str)
(while (and (< x len) (null result))
(goto-char (point-min))
(re-search-forward (nth 1 (nth x l-snippets-token-defines)) nil t)
(setq
result
(remove nil
(mapcar (lambda(x)(match-string x))
(l-snippets-make-lst
(nth 0 (nth x l-snippets-token-defines)))))
x (1+ x))))
result))


(defun l-snippets-gen-token (file &optional regexp)
(let* ((regexp l-snippets-token-regexp)
(let* ((regexp l-snippets-token-regexp-open)
(var (l-snippets-read-file file regexp))
(str (nth 0 var))
(len (length str))
Expand Down Expand Up @@ -261,61 +385,6 @@ l-interactive set to nil."
(substring-no-properties str a b))))
mkr)))

;; * index
(defun l-snippets-read-index (idx)
(let (var)
(with-current-buffer
(let ((enable-local-variables nil))
(find-file-noselect idx))
(prog2
(goto-char (point-min))
(setq var
(condition-case err
(read (current-buffer))
(error
nil)))
(kill-buffer (current-buffer))))))

(defun l-snippets-update-index ()
(let ((mtime (lambda(x)(nth 5 (file-attributes x))))
(l-snippets-index-file
(expand-file-name
l-snippets-index-file
(file-name-directory
l-snippets-repo))))
(if (time-less-p
(or (funcall mtime l-snippets-index-file)
'(0 0 0))
(funcall mtime l-snippets-repo))
(with-temp-file
(let ((enable-local-variables nil)
(find-file-hook nil))
l-snippets-index-file)
(insert (pp-to-string
(directory-files l-snippets-repo nil "^[^_].*\\'")))
(message (format "Save %s." l-snippets-index-file))))
(l-snippets-read-index l-snippets-index-file)))

(setq l-snippets-index
(l-snippets-update-index))

(defun l-snippets-convert-to-snippet-name (abbrev)
(mapconcat 'symbol-name
(list major-mode abbrev)
(plist-get
l-snippets-syntax-table
'path-separator)))

(defun l-snippets-get-snippet (snippet)
(or
(gethash snippet l-snippets-cache)
(if (member snippet l-snippets-index)
(puthash snippet
(l-snippets-gen-token
(expand-file-name snippet l-snippets-repo))
l-snippets-cache)
(message (format "%s is not define" snippet)))))

;; * insert
(defun l-snippets-insert (snippet)
(let* ((snippet (l-snippets-get-snippet snippet))
Expand Down
6 changes: 0 additions & 6 deletions _lib/logic.el
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
;; -*- encoding: utf-8-unix; -*-
;; File-name: <+logic.el>
;; Create: <2012-02-17 22:20:09 ran9er>
;; Time-stamp: <2012-02-17 22:20:28 ran9er>
;; Mail: <[email protected]>

;;;###autoload
(defmacro xor (fn &rest lst)
"like find-if"
Expand Down
5 changes: 3 additions & 2 deletions _lib/skeleton-pair.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(defvar skeleton-pair-cond-alist
'(
((or (char-bf ?$) (char-bf ?=)) . (?\{ _ "}"))
((char-bf '(?+ ?-)) . (?\\ "+" _ "+\\"))
((char-bf '(?$ ?=)) . (?\{ _ "}"))
((or (char-bf ?/)(char-bf ?=)) . (?\[ n _ n "]"))
((bolp) . (?/ "*" n _ n "*/"))
(t . (?/ _))
((bolp) . (?. -1 "->"))
(t . (?. _))
))
(defadvice skeleton-pair-insert-maybe (around xxx activate)
(let ((skeleton-pair-alist skeleton-pair-alist)
Expand Down

0 comments on commit afd23df

Please sign in to comment.