百度汉语 - Helm 搜索建议

百度汉语 https://dict.baidu.com/ 搜成语和诗词时会给搜索建议(补全)

这个补全由这样的 https://dict.baidu.com/hanyu/ajax/sugs?mainkey=百度 提供,这个链接不在浏览器中也能用,所以我写了个 Helm 界面:

代码(需要开启 lexical-binding

(defun helm-baidu-dict-suggest-fetch (query)
  (let* ((url-user-agent (format "%s <%s>" user-full-name user-mail-address))
         (url (format "https://dict.baidu.com/hanyu/ajax/sugs?mainkey=%s"
                      (url-hexify-string query)))
         (buffer (url-retrieve-synchronously url)))
    (with-current-buffer buffer
      (set-buffer-multibyte t)
      (goto-char (point-min))
      (re-search-forward "^\r?\n")
      (let-alist (let ((json-array-type 'list))
                   (json-read))
        .data.ret_array))))

(defun helm-baidu-dict-suggest-candidates (&optional query)
  (mapcar
   (lambda (x)
     (let ((type (car (alist-get 'type x))))
       (pcase type
         ((guard (member type '("query" "term" "idiom")))
          (let ((sid (car (alist-get 'sid x)))
                (name (car (alist-get 'name x))))
            (cons name sid)))
         ("poemline"
          (let ((title (car (alist-get 'source_poem x)))
                (body (car (alist-get 'source_poem_body x)))
                (author (car (alist-get 'literature_author x)))
                (sid (car (alist-get 'source_poem_sid x))))
            (cons
             (format "%s (%s)\n%s"
                     title (propertize author 'face font-lock-comment-face)
                     (propertize body 'face font-lock-comment-face))
             sid)))
         ("poem"
          (let ((title (car (alist-get 'display_name x)))
                (body (car (alist-get 'body x)))
                (author (car (alist-get 'literature_author x)))
                (sid (car (alist-get 'sid x))))
            (cons
             (format "%s (%s)\n%s"
                     title (propertize author 'face font-lock-comment-face)
                     (propertize body 'face font-lock-comment-face))
             sid)))
         (_ (warn "WARNING: 不支持的类型 %S" x)
            nil))))
   (helm-baidu-dict-suggest-fetch (or query helm-pattern))))

(defun helm-baidu-dict-suggest ()
  "百度汉语搜索补全.
URL `https://dict.baidu.com/'."
  (interactive)
  (helm
   :sources
   (helm-build-sync-source "百度汉语"
     :header-name
     (lambda (name)
       (format "%s <%s>" name "https://dict.baidu.com/"))
     :candidates
     #'helm-baidu-dict-suggest-candidates
     :action
     ;; `lexical-binding' must be on
     (let ((URL "https://dict.baidu.com/shici/detail?pid=%s"))
       (helm-make-actions
        "Browse URL"
        (lambda (sid) (browse-url (format URL sid)))
        "EWW URL"
        (lambda (sid) (eww (format URL sid)))))
     :volatile t
     :multiline t
     :requires-pattern 1)
   :full-frame t
   :buffer "*helm 百度汉语*"))
3 个赞

helm-baidu-dict-suggest报错,奇怪啊

uncompressing publicsuffix.txt.gz...done
error in process filter: url-http-parse-headers: Transfer interrupted!
error in process filter: Transfer interrupted!

尽量一次性输入所有查询词,一个字符一个字符慢慢输入会有很多网络请求,出错不奇怪,用户输入中间本应加点延迟的,不过我不清楚怎么操作。

我不是慢慢输入的,我是快快输入的,确实无法查询

这个要是集成到pyim会不会很酷