Created
November 30, 2008 09:15
-
-
Save twada/30410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;; functions | |
(defun funcall-replace-region (start end func) | |
"replace region with funcall result. region content(between start/end) is passed to func as string" | |
(let ((orig (buffer-substring start end))) | |
(save-excursion | |
(save-restriction | |
(narrow-to-region start end) | |
(delete-region start end) | |
(insert-string (funcall func orig)))))) | |
(defun chomp (str) | |
"perl like chomp function. see http://www.emacswiki.org/emacs/ElispCookbook" | |
(let ((s (if (symbolp str)(symbol-name str) str))) | |
(save-excursion | |
(while (and | |
(not (null (string-match "^\\( \\|\f\\|\t\\|\n\\)" s))) | |
(> (length s) (string-match "^\\( \\|\f\\|\t\\|\n\\)" s))) | |
(setq s (replace-match "" t nil s))) | |
(while (and | |
(not (null (string-match "\\( \\|\f\\|\t\\|\n\\)$" s))) | |
(> (length s) (string-match "\\( \\|\f\\|\t\\|\n\\)$" s))) | |
(setq s (replace-match "" t nil s)))) | |
s)) | |
(defun snakecase (str) | |
"snakecase string (chomp, downcase, underscore-joined). e.g. 'Foo Bar' => 'foo_bar'" | |
(let ((s (if (symbolp str)(symbol-name str) str))) | |
(replace-regexp-in-string "\s+" "_" (downcase (chomp s))))) | |
(defun ruby-symbolize (str) | |
"symbolize string in ruby style (snakecase, insert colon at beginning). e.g. 'Foo Bar' => ':foo_bar'" | |
(let ((s (if (symbolp str)(symbol-name str) str))) | |
(concat ":" (snakecase s)))) | |
;;;;;;;;;; commands | |
(defun snakecase-region (start end) | |
"snakecase region" | |
(interactive "r") | |
(funcall-replace-region start end | |
(function (lambda (str) (snakecase str))))) | |
(defun symbolize-region (start end) | |
"ruby-symbolize region" | |
(interactive "r") | |
(funcall-replace-region start end | |
(function (lambda (str) (ruby-symbolize str))))) | |
(defun i18n-region (start end) | |
"ruby-symbolize region then enclose it in Rails2.2 I18n tag" | |
(interactive "r") | |
(funcall-replace-region start end | |
(function (lambda (str) (concat "<%=t " (ruby-symbolize str) " %>"))))) | |
(defun gt-region (quote) | |
"enclose region in gettext braces (with optional quotes)" | |
(interactive "sQuote: ") | |
(funcall-replace-region (region-beginning) (region-end) | |
(function (lambda (str) (concat (format "_(%s" quote) str (format "%s)" quote)))))) | |
;;;;;;;;;; tests | |
(if (boundp 'reduce) (require 'cl)) ;; require cl to use reduce function | |
(defun my-tiny-xunit (exp-list) | |
"indicates xUnit like assertion results (dots and 'F's)" | |
(reduce '(lambda (collector exp) | |
(concat collector (if (eval exp) "." "F"))) | |
exp-list | |
:initial-value "")) | |
(my-tiny-xunit '( | |
(string= "foo" "foo") | |
(string= "Foo" (chomp " Foo ")) | |
(string= "foo" (snakecase "Foo")) | |
(string= "foo_bar" (snakecase "Foo Bar")) | |
(string= "foo_bar" (snakecase " FOO BAR ")) | |
(string= ":foo" (ruby-symbolize "Foo")) | |
(string= ":foo_bar" (ruby-symbolize "Foo Bar")) | |
(string= ":foo_bar" (ruby-symbolize " FOO BAR ")) | |
)) ;; C-j here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment