snippet-helpers.el 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ;;; javascript
  2. (defun js-method-p ()
  3. (save-excursion
  4. (word-search-backward "function")
  5. (looking-back ": ")))
  6. (defun js-function-declaration-p ()
  7. (save-excursion
  8. (word-search-backward "function")
  9. (looking-back "^\\s *")))
  10. (defun snippet--function-punctuation ()
  11. (if (js-method-p)
  12. (when (not (looking-at "[ \n\t\r]*[},]"))
  13. (insert ","))
  14. (unless (js-function-declaration-p)
  15. (if (looking-at "$") (insert ";")))))
  16. (defun snippet--function-name ()
  17. (if (js-function-declaration-p) "name" ""))
  18. ;;; clojure
  19. (defun snippet--clojure-namespace-from-buffer-file-name ()
  20. (replace-regexp-in-string "_" "-"
  21. (replace-regexp-in-string "/" "."
  22. (chop-prefix "test/"
  23. (chop-prefix "src/"
  24. (chop-suffix ".clj"
  25. (substring (buffer-file-name) (length eproject-root))))))))
  26. (defun snippet--clojure-namespace-under-test ()
  27. (replace-regexp-in-string "-test" "" (snippet--clojure-namespace-from-buffer-file-name)))
  28. ;; snippet-helper-helpers
  29. (defun chop-suffix (suffix s)
  30. "Remove string 'suffix' if it is at end of string 's'"
  31. (let ((pos (- (length suffix))))
  32. (if (and (>= (length s) (length suffix))
  33. (string= suffix (substring s pos)))
  34. (substring s 0 pos)
  35. s)))
  36. (defun chop-prefix (prefix s)
  37. "Remove string 'prefix' if it is at start of string 's'"
  38. (let ((pos (length prefix)))
  39. (if (and (>= (length s) (length prefix))
  40. (string= prefix (substring s 0 pos)))
  41. (substring s pos)
  42. s)))