nobiot / org-transclusion

Emacs package to enable transclusion with Org Mode
https://nobiot.github.io/org-transclusion/
GNU General Public License v3.0
919 stars 45 forks source link

Feat/after add hook #124

Closed nobiot closed 2 years ago

nobiot commented 2 years ago

Refer to discussions in PR #119.

A new abnormal hook, org-transclusion-after-add-functions has been added.

"Functions to be called after a transclusion content has been added. The hook runs after the content and the read-only text property have been added so it is not supposed to manipulate the content but to add further text properties. For example, it is used by the org-transclusion-indent-mode' extension to support org-indent-mode'. The functions are called with arguments beg and end, pointing to the beginning and end of the transcluded content."

The existing extension, org-transclusion-indent-mode has been refactored to use the newly added hook. In this refactoring, the part which existed in the remove function has been removed and tested OK to do so.

Documentation is WIP and to be added before a new version makes its way to ELPA.

joshcho commented 2 years ago

Thank you for writing this.

I tried org-display-inline-images and it worked, but with org-latex-preview, I am getting the following error message:

Debugger entered--Lisp error: (wrong-number-of-arguments #<subr org-latex-preview> 2)
  org-latex-preview(147 302)
  run-hook-with-args(org-latex-preview 147 302)
  (let ((beg (line-beginning-position)) (end)) (let* ((modified (buffer-modified-p)) (inhibit-read-only t)) (unwind-protect (progn (if (save-excursion (end-of-line) (insert-char 10) (org-transclusion-content-insert keyword-plist tc-type src-content src-buf src-beg src-end) (if (eobp) nil (delete-char 1)) (setq end (point)) t) (progn (org-transclusion-keyword-remove)))) (if modified nil (restore-buffer-modified-p nil)))) (run-hook-with-args 'org-transclusion-after-add-functions beg end))
  (if (or (string= src-content "") (eq src-content nil)) (progn (message (format "No content found with \"%s\".  Check the link at poi..." (org-element-property :raw-link link) (point) (org-current-line)) nil)) (let ((beg (line-beginning-position)) (end)) (let* ((modified (buffer-modified-p)) (inhibit-read-only t)) (unwind-protect (progn (if (save-excursion (end-of-line) (insert-char 10) (org-transclusion-content-insert keyword-plist tc-type src-content src-buf src-beg src-end) (if ... nil ...) (setq end ...) t) (progn (org-transclusion-keyword-remove)))) (if modified nil (restore-buffer-modified-p nil)))) (run-hook-with-args 'org-transclusion-after-add-functions beg end)) t)
  (let* ((keyword-plist (org-transclusion-keyword-string-to-plist)) (link (org-transclusion-wrap-path-to-link (plist-get keyword-plist :link))) (payload (run-hook-with-args-until-success 'org-transclusion-add-functions link keyword-plist)) (tc-type (plist-get payload :tc-type)) (src-buf (plist-get payload :src-buf)) (src-beg (plist-get payload :src-beg)) (src-end (plist-get payload :src-end)) (src-content (plist-get payload :src-content))) (if (or (string= src-content "") (eq src-content nil)) (progn (message (format "No content found with \"%s\".  Check the link at poi..." (org-element-property :raw-link link) (point) (org-current-line)) nil)) (let ((beg (line-beginning-position)) (end)) (let* ((modified (buffer-modified-p)) (inhibit-read-only t)) (unwind-protect (progn (if (save-excursion ... ... ... ... ... t) (progn ...))) (if modified nil (restore-buffer-modified-p nil)))) (run-hook-with-args 'org-transclusion-after-add-functions beg end)) t))
  (progn (if org-transclusion-mode nil (let ((org-transclusion-add-all-on-activate nil)) (org-transclusion-mode 1))) (let* ((keyword-plist (org-transclusion-keyword-string-to-plist)) (link (org-transclusion-wrap-path-to-link (plist-get keyword-plist :link))) (payload (run-hook-with-args-until-success 'org-transclusion-add-functions link keyword-plist)) (tc-type (plist-get payload :tc-type)) (src-buf (plist-get payload :src-buf)) (src-beg (plist-get payload :src-beg)) (src-end (plist-get payload :src-end)) (src-content (plist-get payload :src-content))) (if (or (string= src-content "") (eq src-content nil)) (progn (message (format "No content found with \"%s\".  Check the link at poi..." (org-element-property :raw-link link) (point) (org-current-line)) nil)) (let ((beg (line-beginning-position)) (end)) (let* ((modified (buffer-modified-p)) (inhibit-read-only t)) (unwind-protect (progn (if ... ...)) (if modified nil (restore-buffer-modified-p nil)))) (run-hook-with-args 'org-transclusion-after-add-functions beg end)) t)))
  (if (org-transclusion-check-add) (progn (if org-transclusion-mode nil (let ((org-transclusion-add-all-on-activate nil)) (org-transclusion-mode 1))) (let* ((keyword-plist (org-transclusion-keyword-string-to-plist)) (link (org-transclusion-wrap-path-to-link (plist-get keyword-plist :link))) (payload (run-hook-with-args-until-success 'org-transclusion-add-functions link keyword-plist)) (tc-type (plist-get payload :tc-type)) (src-buf (plist-get payload :src-buf)) (src-beg (plist-get payload :src-beg)) (src-end (plist-get payload :src-end)) (src-content (plist-get payload :src-content))) (if (or (string= src-content "") (eq src-content nil)) (progn (message (format "No content found with \"%s\".  Check the link at poi..." (org-element-property :raw-link link) (point) (org-current-line)) nil)) (let ((beg (line-beginning-position)) (end)) (let* ((modified ...) (inhibit-read-only t)) (unwind-protect (progn ...) (if modified nil ...))) (run-hook-with-args 'org-transclusion-after-add-functions beg end)) t))))
  org-transclusion-add()
  funcall-interactively(org-transclusion-add)
  command-execute(org-transclusion-add record)
  execute-extended-command(nil "org-transclusion-add" "org tr")
  funcall-interactively(execute-extended-command nil "org-transclusion-add" "org tr")
  command-execute(execute-extended-command)
nobiot commented 2 years ago

org-latex-preview(147 302)

org-latex-preview takes only one argument, and the hook passes two (beg and end) so you need to adjust your hook accordingly. You do this from your PR commit: (org-latex-preview '(16)... So it would be something like this.

(add-hook 'org-transclusion-after-add-functions
          #'(lambda (_beg _end) (org-latex-preview '(16))))

org-latex-preview docstring says it works on "the Latex fragment at point", so... I don't know if you need to move to the point using beg and end (e.g. by using save-excursion).

joshcho commented 2 years ago

Works well, thank you!

nobiot commented 2 years ago

Works well, thank you!

Thank you for testing and reporting back. Merged.