diff options
author | Yuchen Pei <id@ypei.org> | 2023-09-09 12:53:02 +1000 |
---|---|---|
committer | Yuchen Pei <id@ypei.org> | 2023-09-09 12:53:02 +1000 |
commit | 07a6e4601ac4459fc337f25e140650f4bdf8866b (patch) | |
tree | 3fea2006f46b3eab796a2e62f2d33dd5be5c59b8 /emacs/.emacs.d/lisp | |
parent | 66fb229bc11dff59c839527e001d8767eb532d81 (diff) |
[emacs] Fixing my-fetch-url etc.
Diffstat (limited to 'emacs/.emacs.d/lisp')
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-net.el | 45 | ||||
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-org.el | 10 |
2 files changed, 46 insertions, 9 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-net.el b/emacs/.emacs.d/lisp/my/my-net.el index 1ffbfae..0eafb7a 100644 --- a/emacs/.emacs.d/lisp/my/my-net.el +++ b/emacs/.emacs.d/lisp/my/my-net.el @@ -36,14 +36,47 @@ (car (url-path-and-query (url-generic-parse-url (url-unhex-string url))))))) -(defun my-fetch-url (url) +;; stolen from `eww-make-unique-file-name' +(defun my-make-unique-file-name (file directory) + "Uniquefy FILE under DIRECTORY. + +Like `expand-file-name', but make sure the file name has not been taken." + (cond + ((zerop (length file)) + (setq file "!")) + ((string-match "\\`[.]" file) + (setq file (concat "!" file)))) + (let ((count 1) + (stem file) + (suffix "")) + (when (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file) + (setq stem (match-string 1 file) + suffix (match-string 2 file))) + (while (file-exists-p (expand-file-name file directory)) + (setq file (format "%s(%d)%s" stem count suffix)) + (setq count (1+ count))) + (expand-file-name file directory))) + +(defun my-fetch-url (url &optional no-overwrite) + "Fetch URL to a buffer, save it to a file, and switch to the buffer. + +The file is saved under `my-download-dir'. +If NO-OVERWRITE is non-nil, do not overwrite any existing file." (interactive "sURL: ") - (let ((file-name (expand-file-name (my-make-file-name-from-url url) - my-download-dir))) + (let ((file-name + (if no-overwrite + (my-make-unique-file-name + (my-make-file-name-from-url url) + my-download-dir) + (expand-file-name + (my-make-file-name-from-url url) + my-download-dir)))) (url-retrieve url 'my-fetch-url-save-and-switch (list file-name)))) - (defun my-fetch-url-save-and-switch (status file-name) + "A `url-retrieve' callback that saves the payload and switch to it. + +It checks the STATUS, and if it is ok, saves the payload to FILE-NAME." (when (plist-get status :error) (error "My fetch failed: %s" (plist-get status :error))) (my-delete-http-header) @@ -52,7 +85,9 @@ (coding-system-for-write 'utf-8)) (kill-buffer) (with-current-buffer buffer - (insert to-insert) + (let ((inhibit-read-only t)) + (erase-buffer) + (insert to-insert)) (goto-char (point-min)) (save-buffer) (revert-buffer t t)) diff --git a/emacs/.emacs.d/lisp/my/my-org.el b/emacs/.emacs.d/lisp/my/my-org.el index 38eca7b..016bdd7 100644 --- a/emacs/.emacs.d/lisp/my/my-org.el +++ b/emacs/.emacs.d/lisp/my/my-org.el @@ -1281,10 +1281,12 @@ With a prefix arg, yank and exit immediately." (when (equal type "src") (insert (string-remove-suffix "-mode" (prin1-to-string (my-read-major-mode))))) - (org-edit-special) - (when current-prefix-arg - (yank) - (org-edit-src-exit))) + (let ((mark-was-active mark-active)) + (org-edit-special) + (when current-prefix-arg + (unless mark-was-active + (yank)) + (org-edit-src-exit)))) (defun my-link-to-line-number-in-prog-mode () "When in prog-mode, use line number as search item." |