port qwan code from mac

This commit is contained in:
Willem van den Ende
2025-11-09 16:36:14 +00:00
parent 9fd20cb6ee
commit 46a8d76abf
2 changed files with 187 additions and 0 deletions
+121
View File
@@ -0,0 +1,121 @@
;;; qwan/qwan-blog.el -*- lexical-binding: t; -*-
;;;
(provide 'qwan-blog)
(defun qwan/copy-filename ()
"Push the file name of the currently visited buffer to the kill ring. Useful for markdown links"
(interactive)
(unless buffer-file-name
(error "The current buffer doesn't visit a file"))
(kill-new buffer-file-name)
(message "Copied %s" buffer-file-name))
(defun qwan/publish-draft ()
"Publish this in the _posts folder"
(interactive)
(let* ((dest-dir "../_posts")
(current-file (buffer-file-name))
(blogpost-name (file-name-nondirectory current-file))
(current-date (format-time-string "%Y-%m-%d"))
(new-filename (concat current-date "-" blogpost-name))
(source-path current-file)
(dest-path (concat dest-dir "/" new-filename)))
(save-buffer)
(rename-file source-path dest-path)
(set-visited-file-name dest-path)
(set-buffer-modified-p nil)
(message "Successfully moved %s to %s" source-path dest-path)))
;; functions to convert emacs org-roam towards markdown without internal links
(defun qwan/org-replace-link-by-link-description ()
"Remove the link part of an org-mode link at point and keep
only the description"
(interactive)
(let ((elem (org-element-context)))
(if (eq (car elem) 'link)
(let* ((content-begin (org-element-property :contents-begin elem))
(content-end (org-element-property :contents-end elem))
(link-begin (org-element-property :begin elem))
(link-end (org-element-property :end elem)))
(if (and content-begin content-end)
(let ((content
(buffer-substring-no-properties
content-begin content-end)))
(delete-region link-begin (- link-end 1))
(insert content)))))))
(bind-key "C-c r" 'qwan/org-replace-link-by-link-description)
(defun strip-url (url)
"Remove everything after ? from the url, to hinder tracking."
(replace-regexp-in-string "\\?.*$" "" url))
;; TODO define key for this maybe SPC q is available? no, that is quit.
(defun qwan/paste-stripped-url ()
"Paste a URL without tracking from the clipboard into the current buffer."
(interactive)
(let ((url (x-get-selection 'CLIPBOARD)))
(when url
(insert (strip-url url)))))
(defun qwan/org-roam-replace-file-link-by-website-or-description ()
"If a linked org-roam file has a website as key,
use that as the link target instead of the filename.
Otherwise, remove the link, and leave just the description.
This may destroy existing links, or change their targets so
use in a copy of buffers you hold dearly as notes"
(interactive)
(let ((elem (org-element-context)))
(if (eq (car elem) 'link)
(let* ((content-begin (org-element-property :contents-begin elem))
(content-end (org-element-property :contents-end elem))
(target-type (org-element-property :type elem))
(target-path (org-element-property :path elem))
(link-begin (org-element-property :begin elem))
(link-end (org-element-property :end elem)))
(if (and content-begin
content-end
(string-equal target-type "file")
target-path
link-begin
link-end)
(let ((new-link
(org-roam-description-file-to-org-url
(buffer-substring-no-properties content-begin content-end)
target-path)))
(delete-region link-begin (- link-end 1))
(insert new-link)))))))
(defun qwan/string-as-markdown-image-link (a-string)
(concat "![alt-text goes here](" a-string ")"))
(defun qwan/image-mode-copy-filename-as-markdown-image-link ()
"Push a markdown link to the currently visited image onto the kill ring."
(interactive)
(unless buffer-file-name
(error "The current buffer doesn't visit an image file"))
(let ((markdown-link
(qwan/string-as-markdown-image-link
(qwan/relative-path-to-attachments buffer-file-name))))
(kill-new markdown-link)
(message "Copied %s" markdown-link)))
(defun qwan/relative-path-to-attachments (full-path)
(substring full-path (string-search "/attachments" full-path 0)))
(require 'ert)
(ert-deftest relative-path-to-attachments-removes-prefix ()
(should
(equal
(qwan/attachments-remove-prefix-path "prefix/attachments") "/attachments")))
(ert-deftest
relative-path-to-attachments-does-not-remove-prefix-when-no-attachments
()
(should
(equal
(qwan/attachments-remove-prefix-path "prefix/somethingelse")
"prefix/somethingelse")))
(ert-deftest markdown-link-tokens-should-surround-string ()
(should
(equal
(qwan/string-as-markdown-image-link "prefix/image.png")
"![alt-text goes here](prefix/image.png)")))
+66
View File
@@ -0,0 +1,66 @@
;;; qwan/qwan-refactor.el -*- lexical-binding: t; -*-
;;;
(provide 'qwan-refactor)
(defun qwan-echo (new-name)
(interactive "SRename function to:")
(message "Will rename to %s" new-name))
;; from @tbf@functional.cafe
(defun movd (from to)
(fset to (symbol-function from))
(fset from nil)
to)
;; from @tbf@functional.cafe
(defmacro frename (from to)
`(movd ',from ',to))
(defun qrefactor/rename (new-name)
"Rename function at point. You must modify references yourself"
(interactive "MRename function to:")
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds
(progn
(let* ((begin (car bounds))
(end (cdr bounds))
(old-name (buffer-substring-no-properties begin end))
(new-function-name (as-function-name new-name)))
(message "Will rename to %s" new-function-name)
;; should use as-function-name here
(movd (intern old-name) (intern new-function-name))
(ert-make-test-unbound (intern old-name))
(delete-region begin end)
(insert new-function-name)
(message "Renamed %s to %s." old-name new-name)))
(message "No symbol under cursor."))))
(defun qrefactor/remove-test ()
"Renmove test at point from memory.
See also ert-delete-test if you want to delete from a list of existing tests."
(interactive)
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds
(progn
(let* ((begin (car bounds))
(end (cdr bounds))
(old-name (buffer-substring-no-properties begin end)))
(ert-make-test-unbound (intern old-name))
(message "Undefined %s." old-name))))
(message "No symbol under cursor.")))
;; tests are better than literate programming when you want to triangulate on the same function
;; org only shows the last return value by default, you can return a list of course, but still
(defun as-function-name (candidate-name)
"Take the string and make it suitable to be a function name.
Replaces space with dash and make string all lowercase."
(declare (side-effect-free t))
(s-join "-" (mapcar 'downcase (split-string candidate-name " " t))))
;; (s-join "-" (mapcar 'downcase (s-split-words s))))
(ert-deftest refactor-rename-should-leave-forward-slash ()
(should (equal (as-function-name "qwan/echo") "qwan/echo")))
(ert-deftest refactor-rename-should-replace-space-with-dash ()
(should (equal (as-function-name "qwan/echo chamber") "qwan/echo-chamber")))
(ert-deftest refactor-rename-replaces-uppercase-with-lowercase ()
(should (equal (as-function-name "Echo-chamber") "echo-chamber")))
(ert-deftest refactor-renamed-thrice ()
(should (equal 1 1)))
(ert-make-test-unbound 'refactor-rename-test-for-fun)
;; (ert "refactor.*")