Просмотр исходного кода

use ilookup library

pull/1/head
10sr 10 лет назад
Родитель
Сommit
44579d502f
1 измененных файлов: 66 добавлений и 251 удалений
  1. +66
    -251
      emacs.el

+ 66
- 251
emacs.el Просмотреть файл

@@ -2329,262 +2329,77 @@ this is test, does not rename files."

;; (my-real-function-subr-p 'my-real-function-subr-p)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;
;; ilookup

(defvar ilookup-prompt ">>> "
"Prompt string for ilookup input.")

(defvar ilookup-interval 0.5
"Time in second to show sdcv result for current input.")

(defvar ilookup-current-prompt-point nil
"Point of beginning of current prompt.")
(make-variable-buffer-local 'ilookup-current-prompt-point)

(defvar ilookup-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-m") 'ilookup-enter)
(define-key map (kbd "C-u") 'ilookup-kill-input)
(define-key map (kbd "C-a") 'ilookup-goto-bol)
map))

(define-derived-mode ilookup-mode fundamental-mode
"iLookUp"
"Major mode for incremental sdcv buffer."
(set (make-local-variable 'font-lock-function)
'ignore))

(defun ilookup-enter ()
"Enter function for `ilookup-mode'.
Freeze current input and show next prompt."
(interactive)
(let ((pword (and
;; do not get if currently on prompt
(not (ilookup--on-prompt-p))
(thing-at-point 'word))))
;; print result is done only when currently on prompt
(ilookup--print-result-from-input)
(goto-char (point-max))
(unless (eq (point)
(point-at-bol))
(newline))
(setq ilookup-current-prompt-point
(point))
(insert ilookup-prompt)
(and pword
(insert pword))))

(defun ilookup-kill-input ()
"Delete `ilookup-bol' to current point."
(interactive)
(delete-region (ilookup-bol)
(point)))

(defun ilookup-goto-bol ()
"Go to ilookup bol."
(interactive)
(goto-char (ilookup-bol)))

(defvar ilookup-sdcv-command "sdcv -n '%s'"
"Command of sdcv.")
(setq ilookup-sdcv-command "sdcv -n -u jmdict-en-ja '%s'")

(defvar ilookup-buffer nil
"Pointer to ilookup buffer.")

(defvar ilookup--timer nil
"Idle timer object for ilookup.")
(when (fetch-library
"https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
t)
(lazy-load-eval 'ilookup
'(ilookup-open)
(setq ilookup-alist
'(
("en" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
word))))
("ja" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
word))))
("jaj" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u jmdict-en-ja '%s'"
word))))
("jag" .
(lambda (word)
(with-temp-buffer
(insert (shell-command-to-string
(format "sdcv -n -u 'Genius English-Japanese' '%s'"
word)))
(html2text)
(buffer-substring (point-min)
(point-max)))))
("alc" . (lambda (word)
(shell-command-to-string
(format "alc '%s' | head -n 20"
word))))
("app" . (lambda (word)
(shell-command-to-string
(format "dict_app '%s'"
word))))
;; letters broken
("ms" .
(lambda (word)
(let ((url (concat
"http://api.microsofttranslator.com/V2/Ajax.svc/"
"Translate?appId=%s&text=%s&to=%s"))
(apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
(target "ja")
(eword (url-hexify-string word)))
(with-current-buffer (url-retrieve-synchronously
(format url
apikey
eword
target))
(message "")
(goto-char (point-min))
(search-forward-regexp "^$"
nil
t)
(url-unhex-string (buffer-substring-no-properties
(point)
(point-max)))))))
))
;; (funcall (cdr (assoc "ms"
;; ilookup-alist))
;; "dictionary")

(defvar ilookup--last-input nil
"Last input queryed.")
;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))

(defvar ilookup-default "sdcv"
"Default command for ilookup.
This value should be a key of `ilookup-alist'.")
;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))

(defvar ilookup-alist
'(
("sdcv" . (lambda (word)
(shell-command-to-string (format "sdcv -n '%s'"
word))))
)
"Alist of ilookup functions.
Each element should be in the form of (NAME . FUNCTION).
FUNCTION must accept one argument as word to search and return the string of
result for that word.")

(setq ilookup-alist
'(
("en" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
word))))
("ja" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
word))))
("jaj" . (lambda (word)
(shell-command-to-string
(format "sdcv -n -u jmdict-en-ja '%s'"
word))))
("jag" . (lambda (word)
(with-temp-buffer
(insert (shell-command-to-string
(format "sdcv -n -u 'Genius English-Japanese' '%s'"
word)))
(html2text)
(buffer-substring (point-min)
(point-max)))))
("alc" . (lambda (word)
(shell-command-to-string
(format "alc '%s' | head -n 20"
word))))
("app" . (lambda (word)
(shell-command-to-string
(format "dict_app '%s'"
word))))
;; letters broken
("ms" .
(lambda (word)
(let ((url (concat "http://api.microsofttranslator.com/V2/Ajax.svc/"
"Translate?appId=%s&text=%s&to=%s"))
(apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
(target "ja")
(eword (url-hexify-string word)))
(with-current-buffer (url-retrieve-synchronously
(format url
apikey
eword
target))
(message "")
(goto-char (point-min))
(search-forward-regexp "^$"
nil
t)
(url-unhex-string (buffer-substring-no-properties (point)
(point-max)))))))
))
(setq ilookup-default "ja")))

;; (funcall (cdr (assoc "ms"
;; ilookup-alist))
;; "dictionary")

;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))

;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))

(setq ilookup-default "ja")

(defun ilookup--timer-add ()
"Entry idle timer for ilookup."
(and (eq ilookup-buffer
(current-buffer))
;; do not duplicate timer
(not ilookup--timer)
(setq ilookup--timer
(run-with-idle-timer ilookup-interval
t
'ilookup--print-result-from-input))))

(defun ilookup--timer-remove ()
"Remove idle timer for ilookup."
;; remote only when killing buffer is ilookup buffer
(and (eq ilookup-buffer
(current-buffer))
ilookup--timer
(cancel-timer ilookup--timer)))

(defun ilookup-bol ()
"Return point to bol ignoring prompt."
(save-excursion
(beginning-of-line)
(or (search-forward ilookup-prompt
(point-at-eol)
t)
(point))))

(defun ilookup--get-output-start ()
"Return point where outputs should be inserted.
This function insert newline if required."
(when ilookup-current-prompt-point
(save-excursion
(goto-char ilookup-current-prompt-point)
(forward-line 1)
(when (ilookup--on-prompt-p)
(end-of-line)
(newline))
(point-at-bol))))

(defun ilookup--print-result-from-input ()
"Get entry for current ilookup input."
(let ((input (ilookup--get-input))
(outpoint (ilookup--get-output-start)))
(and input
;; do not query same word twice
(not (equal ilookup--last-input
input))
outpoint
(let* (
;; colon sepatated list of input
(inputl (split-string input
":"))
;; funcname for `ilookup-alist'
(fname (if (eq (length inputl)
2)
(car inputl)
ilookup-default))
(func (cdr (assoc fname
ilookup-alist)))
(word (if (eq (length inputl)
2)
(nth 1
inputl)
(car inputl))))
(save-excursion
(setq ilookup--last-input input)
(goto-char outpoint)
(delete-region (point)
(point-max))
(insert (if func
(funcall func
word)
(format "No func found for `%s'"
fname))))))))

(defun ilookup--get-result (word fname)
"Return result string for WORD with FNAME."
nil)

(defun ilookup--get-input ()
"Get current input for ilookup buffer.
Return nil if current position is not on prompt line."
(and (eq ilookup-buffer
(current-buffer))
(ilookup--on-prompt-p)
(buffer-substring-no-properties (ilookup-bol)
(point-at-eol))))

(defsubst ilookup--on-prompt-p ()
"Return true if currently on prompt line."
(eq (line-number-at-pos)
(line-number-at-pos ilookup-current-prompt-point)))

(defun ilookup-open ()
"Open ilookup buffer."
(interactive)
(if ilookup-buffer
(pop-to-buffer ilookup-buffer)
(with-current-buffer (setq ilookup-buffer
(get-buffer-create "*ilookup*"))
(ilookup-mode)
(font-lock-mode t)
(ilookup-enter)
(ilookup--timer-add)
(add-hook 'kill-buffer-hook
'ilookup--timer-remove)
)
(pop-to-buffer ilookup-buffer)))

;;; emacs.el ends here

Загрузка…
Отмена
Сохранить