UP | HOME

+emacs+开机启动wttrin天气界面

[2025-01-08 周三 23:22]

让每次启动Emacs时都能看见指定位置的天气。

1. 安装软件

melpa安装wttrin

2. 设置本地天气

  1. 进入wttrin网页,查看左上角显示的本地城市名称,如AAA, China
  2. 如下配置wttrin包
(use-package wttrin
  :config
  (setq wttrin-default-languages '("Accept-Language" . "zh-CN,zh")))

3. 调整显示界面

如果你用的全局中英文等宽字体,忽略以下部分。

如果不是,先设置字体界面。在custom-set-faces中加入相关字体样式:

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
  '(wttrin-buffer-face ((t (:family "Sarasa Term SC")))))

然后为wttrin-buffer-face设置中英文字体:

(defun create-variable-pitch-fontset (name ascii-font han-font)
  "Create fontset named NAME with given ASCII-FONT and HAN-FONT."
  (let ((registry (concat "fontset-variable pitch " name)))
    (create-fontset-from-fontset-spec
     (font-xlfd-name
      (font-spec :family ascii-font
                 :registry registry)))
    (set-fontset-font registry 'han
                      (font-spec :family han-font))
    (set-fontset-font registry 'cjk-misc
                      (font-spec :family han-font))))
(create-variable-pitch-fontset "regular"
                               "Sarasa Term SC"
                               "Sarasa Term SC")

(set-face-attribute 'wttrin-buffer-face nil :fontset "fontset-variable pitch regular")

最后覆盖wttrin-qurey函数:

(defun wttrin-query (location-name)
  "Query weather of LOCATION-NAME via wttrin, display the result in new buffer."
  (let ((raw-string (wttrin-fetch-raw-string location-name)))
    (if (string-match "ERROR" raw-string)
        (message "Cannot retrieve weather data. Perhaps the location was
misspelled?")
      (let ((buffer (get-buffer-create (format "*wttr.in*")))
            date-time-stamp location-info)
        (switch-to-buffer buffer)
        (setq buffer-read-only nil)
        (erase-buffer)

        ;; Enable `truncate-lines` to avoid garbling the output in small windows
        (setq truncate-lines t)

        ;; set the preferred font attributes for this buffer only
        ;; (setq buffer-face-mode-face `(:family ,wttrin-font-name :height
        ;;                                    ,wttrin-font-height))
        (setq buffer-face-mode-face 'wttrin-buffer-face)
        ;; display buffer text and insert wttr.in data
        (buffer-face-mode t)
        (insert (xterm-color-filter raw-string))

        ;; rearrange header information
        (goto-char (point-min))
        (forward-line 4)
        (setq date-time-stamp (buffer-substring-no-properties
                               (line-beginning-position) (line-end-position)))
        (goto-char (point-min))
        (forward-line 6)
        (setq location-info (buffer-substring-no-properties
                             (line-beginning-position) (line-end-position)))
        (goto-char (point-min))
        (forward-line 8)
        (delete-region (point-min) (line-beginning-position))

        (insert "\n" location-info "\n" date-time-stamp "\n\n\n")

        ;; provide user instructions
        (goto-char (point-max))
        (insert "\nPress: [g] to query another location\n")
        (insert "       [q] to quit\n")

        ;; align buffer to top
        (goto-char (point-min))

        ;; create choice keymap and disallow modifying buffer
        (use-local-map (make-sparse-keymap))
        (local-set-key "q" 'wttrin-exit)
        (local-set-key "g" 'wttrin-requery)
        (setq buffer-read-only t)))))

4. 启动时显示天气

在启动文件的最后一行加上以下命令:

(wttrin-query "AAA, China")

▲ 编辑于 [2025-01-08 周三 23:33] | © Published by Emacs 31.0.50 (Org mode 9.7.19) on [2025-01-08 周三 23:33] | RSS