Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuutayamada/5958399 to your computer and use it in GitHub Desktop.
Save yuutayamada/5958399 to your computer and use it in GitHub Desktop.
(require 'zencoding-mode)
(eval-when-compile (require 'cl))
(require 'flyspell)
(defun my/zencoding-expand ()
"Execute zencoding if current point is in string and
you can execute from minibuffer if you push C-u before this command"
(interactive)
(lexical-let*
((quote "['\"]")
(statement "")
(correct-condition-p
(lambda ()
(and (flyspell-generic-progmode-verify)
(case (face-at-point)
(font-lock-string-face t)
(font-lock-comment-face nil)
(t nil)))))
(move-start-point
(lambda ()
(if (funcall correct-condition-p)
(while (and (funcall correct-condition-p)
(search-backward-regexp quote))))
(forward-char 1)))
(move-end-point
(lambda ()
(while (flyspell-generic-progmode-verify)
(forward-char 1))
(backward-char 2)))
(set-statement
(lambda ()
(funcall move-start-point)
(set-mark-command nil)
(funcall move-end-point)
(kill-region (region-beginning) (region-end))
(setq statement (car kill-ring))))
(do-zencoding-from-minibuffer
(lambda ()
(setq statement (read-from-minibuffer "zencoding: "))
(insert (zencoding-transform (car (zencoding-expr statement))))))
(flyspell-state (if flyspell-mode t nil)))
(when (and (funcall correct-condition-p)
(case major-mode
(js2-mode t)
(javascript-mode t)
(t nil)))
(if current-prefix-arg
(funcall do-zencoding-from-minibuffer)
(when flyspell-state (turn-off-flyspell))
(funcall move-start-point)
(set-mark-command nil)
(funcall move-end-point)
(funcall set-statement)
(insert (replace-regexp-in-string
"[\n ]" ""
(zencoding-transform (car (zencoding-expr statement)))))
(when flyspell-state (turn-on-flyspell))))))
;; if you want to use with yasnippet
(defadvice yas-expand-from-trigger-key
(around ad-add-zencoding-expand activate)
(if (flyspell-generic-progmode-verify)
(my/zencoding-expand)
ad-do-it))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment