diff --git a/config.el b/config.el index ac74fcc..9152d7c 100644 --- a/config.el +++ b/config.el @@ -299,144 +299,6 @@ (insert description)))) -(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 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)"))) -(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.*") - ;; craft of emacs (add-load-path! "~/.doom.d/coe-xx") (require 'coe-xx) @@ -447,7 +309,7 @@ only the description" (add-load-path! "~/.doom.d/qwan") (require 'qwan-blog) - +(require 'qwan-refactor) ;; org-roam v1 port to v2, insert node without opening buffer diff --git a/qwan/qwan-blog.el b/qwan/qwan-blog.el index aaf7fad..9863baf 100644 --- a/qwan/qwan-blog.el +++ b/qwan/qwan-blog.el @@ -27,3 +27,82 @@ (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 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)"))) diff --git a/qwan/qwan-refactor.el b/qwan/qwan-refactor.el new file mode 100644 index 0000000..fac17ba --- /dev/null +++ b/qwan/qwan-refactor.el @@ -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.*")