emacs一键插入-日期,农历,月相,万年历
1. wttr.in天气信息的API调取
wttr.in的地址 https://github.com/chubin/wttr.in
1: ;;添加济南天气的操作 2: (defun insert-weather () 3: (interactive) 4: (insert (shell-command-to-string "curl wttr.in/jinan?format=4 2> /dev/null"))) 5: 6: (global-set-key (kbd "C-c m w") #'insert-weather)
2. pyphoon月相的调取
pyphoon的地址 https://github.com/chubin/pyphoon
1: ;;添加月相的操作 2: (defun insert-pyphoon () 3: (interactive) 4: (insert (shell-command-to-string "source ~/mypython/bin/activate && pyphoon"))) 5: 6: (global-set-key (kbd "C-c m o") #'insert-pyphoon)
3. 农历API调取地址
3.1. 先写一个shellscript来调取农历API地址中的字符串
例如这个效果: "2025-02-11 孟春 正月 十四 蛇 乙巳 戊寅 辛亥 喜神:西南 福神:西北 财神:正东阳贵:东北 阴贵:正南 " 配置文件如下:
1: #!/bin/bash 2: 3: # 获取当前日期 4: current_date=$(date +"%Y-%m-%d") 5: 6: # 使用 wget 命令获取当天的农历日期,并且静默过程 7: response=$(wget -qO- "https://www.36jxs.com/api/Commonweal/almanac?sun=${current_date}") 8: 9: # 解析农历日期 10: lunar_GregorianDateTime=$(echo $response | grep -o '"GregorianDateTime":"[^"]*' | grep -o '[^"]*$') 11: lunar_month=$(echo $response | grep -o '"LMonth":"[^"]*' | grep -o '[^"]*$') 12: lunar_day=$(echo $response | grep -o '"LDay":"[^"]*' | grep -o '[^"]*$') 13: lunar_year=$(echo $response | grep -o '"LYear":"[^"]*' | grep -o '[^"]*$') 14: lunar_tiangany=$(echo $response | grep -o '"TianGanDiZhiYear":"[^"]*' | grep -o '[^"]*$') 15: lunar_tianganm=$(echo $response | grep -o '"TianGanDiZhiMonth":"[^"]*' | grep -o '[^"]*$') 16: lunar_tiangand=$(echo $response | grep -o '"TianGanDiZhiDay":"[^"]*' | grep -o '[^"]*$') 17: lunar_shenwei=$(echo $response | grep -o '"ShenWei":"[^"]*' | grep -o '[^"]*$') 18: lunar_monthname=$(echo $response | grep -o '"LMonthName":"[^"]*' | grep -o '[^"]*$') 19: 20: 21: 22: 23: echo " $lunar_GregorianDateTime $lunar_monthname $lunar_month $lunar_day $lunar_year $lunar_tiangany $lunar_tianganm $lunar_tiangand $lunar_shenwei"
3.2. 这个效果" <孟春-正月-十四 蛇 乙巳-戊寅-辛亥> "
配置文件如下:
1: #!/bin/bash 2: 3: # 获取当前日期 4: current_date=$(date +"%Y-%m-%d") 5: 6: # 使用 wget 命令获取当天的农历日期,并且静默过程 7: response=$(wget -qO- "https://www.36jxs.com/api/Commonweal/almanac?sun=${current_date}") 8: 9: # 解析农历日期 10: #lunar_GregorianDateTime=$(echo $response | grep -o '"GregorianDateTime":"[^"]*' | grep -o '[^"]*$') 11: lunar_monthname=$(echo $response | grep -o '"LMonthName":"[^"]*' | grep -o '[^"]*$') 12: lunar_month=$(echo $response | grep -o '"LMonth":"[^"]*' | grep -o '[^"]*$') 13: lunar_day=$(echo $response | grep -o '"LDay":"[^"]*' | grep -o '[^"]*$') 14: lunar_year=$(echo $response | grep -o '"LYear":"[^"]*' | grep -o '[^"]*$') 15: lunar_tiangany=$(echo $response | grep -o '"TianGanDiZhiYear":"[^"]*' | grep -o '[^"]*$') 16: lunar_tianganm=$(echo $response | grep -o '"TianGanDiZhiMonth":"[^"]*' | grep -o '[^"]*$') 17: lunar_tiangand=$(echo $response | grep -o '"TianGanDiZhiDay":"[^"]*' | grep -o '[^"]*$') 18: #lunar_shenwei=$(echo $response | grep -o '"ShenWei":"[^"]*' | grep -o '[^"]*$') 19: 20: 21: 22: 23: 24: echo " <$lunar_monthname-$lunar_month-$lunar_day $lunar_year $lunar_tiangany-$lunar_tianganm-$lunar_tiangand> "
3.3. 然后就能用elisp调用这两个文件
1: (defun insert-nongli () 2: (interactive) 3: (insert (shell-command-to-string "~/.emacs.d/loadfile/nongli.sh"))) 4: 5: (global-set-key (kbd "C-c m n") #'insert-nongli) 6: 7: (defun insert-nonglijian () 8: (interactive) 9: (insert (shell-command-to-string "~/.emacs.d/loadfile/nonglijian.sh"))) 10: 11: (global-set-key (kbd "C-c m j") #'insert-nonglijian) 12:
4. 还可以在已启动的那个*scratch*页面显示出来,虽然没什么用,但是总比那个啥信息都没有的强.
将以下代码添加到你的 Emacs 配置文件中:
1: ;; 在 Emacs 启动时清空 *scratch* 缓冲区并插入特定函数代码 2: (setq initial-scratch-message "") ; 清空初始的 *scratch* 消息 3: (with-current-buffer "*scratch*" 4: ;(let ((result1 (insert-weather))) 5: ;(insert (format " %s\n" result1))) 6: (call-interactively 'insert-nongli) 7: (call-interactively 'insert-weather) 8: (call-interactively 'insert-pyphoon) 9: )
4.1. 效果

4.2. 设置的快捷键,可以随意调取
C-c m o 插入月相(刚好是满月)
.------------. .--' o . . `--. .-' . O . . `-. -'@ @@@@@@@ . @@@@@ `-. /@@@ @@@@@@@@@@@ @@@@@@@ . \ / o @@@@@@@@@@@ @@@@@@@ . \. @@ o @@@@@@@@@@@. @@@@@@@ O \ @@@@ . @@@@@@@o @@@@@@@@@@ @@@ \ |@@@@@ . @@@@@@@@@@@@@ o @@@@| @@@@@ O `.-./ . @@@@@@@@@@@@ @@ \ First Quarter + @@@@ --`-' o @@@@@@@@ @@@@ | 6 5:40:26 |@ @@@ ` o . @@ . @@@@@@@ | Full Moon - @@ @ .-. @@@ @@@@@@@ | 1 0:10:09 . @ @@@ `-' . @@@@ @@@@ o / | @@ @@@@@ . @@ . | @@@@ @\@@ / . O . o . / o @@ \ \ / . . / \ . .\.-.___ . . .-. /' \ `-' `-' / -. o / | o O . .-' `-. / . . .-' `--. . .--' `------------'
C-c m w 插入济南的天气
jinan: 🌦 🌡️+7°C 🌬️↗8km/h
C-c m n 插入农历
2025-02-11 孟春 正月 十四 蛇 乙巳 戊寅 辛亥 喜神:西南 福神:西北 财神:正东阳贵:东北 阴贵:正南
C-c m j 简介版本的农历
<孟春-正月-十四 蛇 乙巳-戊寅-辛亥>
C-c m y 显示出当年时间已经流逝的百分比
[/// ] 11%
5. 配置文件
1: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2: ;; -*- coding: utf-8; lexical-binding: t -*- 3: ;;pengshao-openweather.el---自动插入天气的教程 4: ;;<2025-02-08/Saturday 19:32:10> 5: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 6: 7: ;;添加济南天气的操作 8: (defun insert-weather () 9: (interactive) 10: (insert (shell-command-to-string "curl wttr.in/jinan?format=4 2> /dev/null"))) 11: 12: 13: (global-set-key (kbd "C-c m w") #'insert-weather) 14: 15: 16: ;;添加月相的操作 17: (defun insert-pyphoon () 18: (interactive) 19: (insert (shell-command-to-string "source ~/mypython/bin/activate && pyphoon"))) 20: 21: 22: (global-set-key (kbd "C-c m o") #'insert-pyphoon) 23: 24: 25: (defun insert-nongli () 26: (interactive) 27: (insert (shell-command-to-string "~/.emacs.d/loadfile/nongli.sh"))) 28: 29: (global-set-key (kbd "C-c m n") #'insert-nongli) 30: 31: (defun insert-nonglijian () 32: (interactive) 33: (insert (shell-command-to-string "~/.emacs.d/loadfile/nonglijian.sh"))) 34: 35: (global-set-key (kbd "C-c m j") #'insert-nonglijian) 36: 37: 38: 39: (provide 'pengshao-openweather)