January 4, 2025
By: Kevin'
常用的elisp函数/配置
配置
启停emacsd的proxy
(setq url-proxy-services
'(("http". "127.0.0.1:7890")
("sock5". "127.0.0.1:7890")
("https". "127.0.0.1:7890")))
(setq url-proxy-services nil)
数据时候自动替换全角标点为半角标点
;; 映射全角字符到半角字符
(let (($replacePairs
[["·" "`"]
["~" "~"]
["?" "?"]
["," ","]
["。" "."]
[";" ";"]
[":" ":"]
["「" "{"]
["」" "}"]
["【" "["]
["】" "]"]
["(" "("]
[")" ")"]
["!" "!"]
["、" "\\"]
["/" "/"]
["《" "<"]
["》" ">"]
["¥" "$"]
["‘" "'"]
["’" "'"]
["“" "\""]
["”" "\""]
;; 全角字符的特殊快捷键
["M-," "M-,"]
["M-。" "M-."]
["M-…" "M-^"]
["M-》" "M->"]
["C-—" "C--"]
["C-c C-," "C-c C-,"]]))
(mapcar (lambda(x) (define-key key-translation-map
(kbd (elt x 0)) (kbd (elt x 1)))) $replacePairs))
函数
把剪切板中的图片保存到本地
并且加入链接到org-mode
;; 同时支持osx(安装了pngpaste)和windwos(需要powershell)
(defun org-insert-clipboard-image (&optional file)
(interactive "F")
(let ((file (or file (read-file-name "Save image as: " nil nil nil "image.png"))))
(if (eq system-type 'windows-nt)
(shell-command
(concat
"powershell -command (Get-Clipboard -Format Image).Save('"
(expand-file-name (subst-char-in-string ?/ ?\\ file))
"')"))
(shell-command (concat "pngpaste " file)))
(insert (concat "[[file:" (file-relative-name file) "]]"))
(org-display-inline-images)))
替换所有中文标点为英文
(defun replace-chinese-punctuation ()
"Replace Chinese punctuation marks with their English counterparts in the current buffer."
(interactive)
(let ((replacements '(("," . ",")
("。" . ".")
("?" . "?")
("!" . "!")
(":" . ":")
(";" . ";")
("“" . "\"")
("”" . "\"")
("‘" . "'")
("’" . "'")
("(" . "(")
(")" . ")")
("【" . "[")
("】" . "]")
("《" . "<")
("》" . ">")
("、" . ", ")
("——" . "--")
("…" . "...")
("—" . "-"))))
(save-excursion
(dolist (pair replacements)
(goto-char (point-min))
(while (search-forward (car pair) nil t)
(replace-match (cdr pair) t t))))))
从eww中下载所有的图片到特定目录
(require 'org-download)
;; Drag-and-drop to `dired`
(add-hook 'dired-mode-hook 'org-download-enable)
(defun eww-extract-image-urls ()
"Extract all image URLs from the current EWW buffer's source and return them as a list."
(interactive)
(let ((base-url (eww-current-url))
(urls '()))
;; Open the source buffer
(eww-view-source)
;; Switch to the source buffer
(with-current-buffer "*eww-source*"
(save-excursion
(goto-char (point-min))
;; Extract src attributes
(while (re-search-forward "<img[^>]+src=\\(\"[^\"]*\"\\|'[^']*'\\|[^\"' >]+\\)" nil t)
(let ((src (match-string 1)))
;; Remove quotes if present
(setq src (replace-regexp-in-string "[\"']" "" src))
(push (url-expand-file-name src base-url) urls)))
;; Extract data-src attributes
(goto-char (point-min))
(while (re-search-forward "<img[^>]+data-src=\\(\"[^\"]*\"\\|'[^']*'\\|[^\"' >]+\\)" nil t)
(let ((src (match-string 1)))
;; Remove quotes if present
(setq src (replace-regexp-in-string "[\"']" "" src))
(push (url-expand-file-name src base-url) urls)))
;; Extract base64 images
(goto-char (point-min))
(while (re-search-forward "<img[^>]+src=\\(\"[^\"]*\"\\|'[^']*'\\|[^\"' >]+\\)" nil t)
(let ((src (match-string 1)))
(when (string-prefix-p "data:image/" src)
(push src urls)))))
;; Close the source buffer
(kill-buffer "*eww-source*")
;; Return the list of URLs
(nreverse urls))))
(defun eww-download-all-images ()
"Extract all image URLs from the current EWW buffer and download them to a user-selected directory."
(interactive)
(let* ((urls (eww-extract-image-urls))
(default-directory (if (and buffer-file-name (file-directory-p (file-name-directory buffer-file-name)))
(file-name-directory buffer-file-name)
default-directory))
(target-dir (read-directory-name "Download images to directory: " default-directory nil nil)))
(unless (file-exists-p target-dir)
(if (y-or-n-p (format "Directory '%s' does not exist. Create it? " target-dir))
(make-directory target-dir t)
(error "Directory does not exist and user cancelled.")))
(let ((original-directory default-directory)) ; Store original directory
(unwind-protect
(progn
(setq default-directory target-dir) ; Change default directory
(dolist (url urls)
(org-download-image url)))
(setq default-directory original-directory))))) ; Restore original directory