Skip to content

Commit

Permalink
use camel case algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Jul 5, 2018
1 parent b4e8904 commit ab5cbda
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 30 deletions.
39 changes: 15 additions & 24 deletions lisp/init-spelling.el
Expand Up @@ -40,7 +40,7 @@

(defun my-flyspell-predicate (word)
"Use aspell to check WORD. If it's typo return true."
(if (string-match-p (concat "^& " word)
(if (string-match-p "^&"
(shell-command-to-string (format "echo %s | %s %s pipe"
word
ispell-program-name
Expand All @@ -49,18 +49,6 @@
" "))))
t))

(defmacro my-flyspell-predicate-factory (preffix)
`(lambda (word)
(let* ((pattern (concat "^\\(" ,preffix "\\)\\([A-Z]\\)"))
rlt)
(cond
((string-match-p pattern word)
(setq word (replace-regexp-in-string pattern "\\2" word))
(setq rlt (my-flyspell-predicate word)))
(t
(setq rlt t)))
rlt)))

(defun js-flyspell-verify ()
(let* ((case-fold-search nil)
(font-matched (memq (get-text-property (- (point) 1) 'face)
Expand All @@ -75,6 +63,7 @@
rjsx-text
rjsx-tag
rjsx-attr)))
subwords
word
(rlt t))
(cond
Expand All @@ -83,18 +72,20 @@
((not (string-match-p "aspell$" ispell-program-name))
;; Only override aspell's result
(setq rlt t))
((string-match-p "^[a-zA-Z][a-zA-Z]$"
(setq word (thing-at-point 'word)))
;; ignore two character word
((< (length (setq word (thing-at-point 'word))) 2)
(setq rlt nil))
((string-match-p "\\([A-Z][a-z]\\|^[a-z][a-z]\\)[A-Z]\\|[a-z][A-Z][a-zA-Z]$"
word)
;; strip two character interior words
;; abcAzAbc => abcAbc; aaBcd => Bcd
(setq word (replace-regexp-in-string "\\([A-Z][a-z]\\|^[a-z][a-z]\\)\\([A-Z]\\)" "\\2" word))
;; abcAb => abc; abcAB => abc
(setq word (replace-regexp-in-string "\\([a-z]\\)[A-Z][a-zA-Z]$" "\\1" word))
;; check stripped world
(setq rlt (my-flyspell-predicate word)))
;; handle camel case word
((and (setq subwords (split-camel-case word)) (> (length subwords) 1))
(let* ((s (mapconcat (lambda (w)
(cond
((< (length w) 3)
"")
((not (string-match-p "^[a-zA-Z]*$" w))
"")
(t
w))) subwords " ")))
(setq rlt (my-flyspell-predicate s))))
(t
(setq rlt (funcall extra-flyspell-predicate word))))
rlt))
Expand Down
73 changes: 67 additions & 6 deletions lisp/init-utils.el
Expand Up @@ -11,19 +11,80 @@
,@clean-up))

;; {{ copied from http://ergoemacs.org/emacs/elisp_read_file_content.html
(defun get-string-from-file (filePath)
"Return filePath's file content."
(defun get-string-from-file (file)
"Return FILE's content."
(with-temp-buffer
(insert-file-contents filePath)
(insert-file-contents file)
(buffer-string)))

(defun read-lines (filePath)
"Return a list of lines of a file at filePath."
(defun read-lines (file)
"Return a list of lines of FILE."
(with-temp-buffer
(insert-file-contents filePath)
(insert-file-contents file)
(split-string (buffer-string) "\n" t)))
;; }}

(defun split-camel-case (word)
"Split camel case WORD into a list of strings.
Ported from 'https://github.com/fatih/camelcase/blob/master/camelcase.go'."
(let* ((case-fold-search nil)
(len (length word))
;; ten sub-words is enough
(runes [nil nil nil nil nil nil nil nil nil nil])
(runes-length 0)
(i 0)
ch
(last-class 0)
(class 0)
rlt)

;; split into fields based on class of character
(while (< i len)
(setq ch (elt word i))
(cond
;; lower case
((and (>= ch ?a) (<= ch ?z))
(setq class 1))
;; upper case
((and (>= ch ?A) (<= ch ?Z))
(setq class 2))
((and (>= ch ?0) (<= ch ?9))
(setq class 3))
(t
(setq class 4)))

(cond
((= class last-class)
(aset runes
(1- runes-length)
(concat (aref runes (1- runes-length)) (char-to-string ch))))
(t
(aset runes runes-length (char-to-string ch))
(setq runes-length (1+ runes-length))))
(setq last-class class)
;; end of while
(setq i (1+ i)))

;; handle upper case -> lower case sequences, e.g.
;; "PDFL", "oader" -> "PDF", "Loader"
(setq i 0)
(while (< i (1- runes-length))
(let* ((ch-first (aref (aref runes i) 0))
(ch-second (aref (aref runes (1+ i)) 0)))
(when (and (and (>= ch-first ?A) (<= ch-first ?Z))
(and (>= ch-second ?a) (<= ch-second ?z)))
(aset runes (1+ i) (concat (substring (aref runes i) -1) (aref runes (1+ i))))
(aset runes i (substring (aref runes i) 0 -1))))
(setq i (1+ i)))

;; construct final result
(setq i 0)
(while (< i runes-length)
(when (> (length (aref runes i)) 0)
(setq rlt (add-to-list 'rlt (aref runes i) t)))
(setq i (1+ i)))
rlt))

(defun nonempty-lines (s)
(split-string s "[\r\n]+" t))

Expand Down

0 comments on commit ab5cbda

Please sign in to comment.