Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create plus add-on #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions org-transclusion-plus.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
;;; org-transclusion-src-lines.el --- Extension -*- lexical-binding: t; -*-

;;; Commentary:
;; (add-to-list 'org-transclusion-extensions 'org-transclusion-plus)
;;
;;; Code:

(require 'org-element)
;; (declare-function text-clone-make-overlay 'text-clone)
;; (declare-function org-transclusion-live-sync-buffers-others-default
;; 'org-transclusion)

;;;; Customization
(defcustom org-transclusion-plus-enable-cache t
"Define whether to enable plus use cache file."
:type 'boolean
:group 'org-transclusion)

;;;; Setting up the extension

;; Add a new transclusion type
(add-hook 'org-transclusion-add-functions
#'org-transclusion-add-plus)
;; Keyword values
(add-hook 'org-transclusion-keyword-value-functions
#'org-transclusion-keyword-value-plus)
(add-hook 'org-transclusion-keyword-plist-to-string-functions
#'org-transclusion-keyword-plist-to-string-plus)

;; Transclusion content formating
;; Not needed. Default works for text files.


;;; Functions

(defun org-transclusion-add-plus (link plist)
"Return a list for non-Org text and source file.
Determine add function based on LINK and PLIST.

Return nil if PLIST does not contain \":plus\" properties."
(cond
((plist-get plist :plus)
(append '(:tc-type "plus")
(org-transclusion-plus-payload link plist)))
)
)

(defun org-transclusion-plus-payload (link plist)
"Return a payload processed with default procedure use the file
that created by plus ."
(let* ((work-file (org-transclusion-plus-content link plist))
(new-link (concat "[[file:" work-file "]]"))
(plist (plist-put plist :cache-file work-file)) ;; reserve for live edit
(new-plist (copy-sequence plist))
(new-plist (plist-put new-plist :link new-link))
(new-link (org-transclusion-wrap-path-to-link new-link))
(org-transclusion-add-functions
(remove 'org-transclusion-add-plus org-transclusion-add-functions ))
(payload (run-hook-with-args-until-success
'org-transclusion-add-functions new-link new-plist)))

payload
))


(defun org-transclusion-plus-content (link plist)
"Return a temp file processed with plus eval."
(let* ((path (org-element-property :path link))
(r-file (if (file-name-absolute-p path) path (expand-file-name path)))
(work-file-name (file-name-nondirectory r-file))
(plus (string-trim (plist-get plist :plus)))
(cache-id (md5 (concat path plus)))
(work-dir (concat (temporary-file-directory) cache-id "/"))
(work-file (expand-file-name work-file-name work-dir))
(src (cond
((string-match "\(.*\)" plus) plus)
((string-match "^[^\(\)]+$" plus) (org-babel-expand-noweb-references (org-babel-lob--src-info plus)))
(t "()")
))
)
(unless (and org-transclusion-plus-enable-cache
(file-exists-p! work-file-name work-dir)
(time-less-p
(file-attribute-modification-time (file-attributes r-file))
(file-attribute-modification-time (file-attributes work-file)))
)
(make-directory work-dir t)
(copy-file r-file work-dir t)
(let* ((buf (find-file-noselect work-file)))
(when buf
(with-current-buffer buf

(goto-char (point-min))
(eval (car (read-from-string (format "(progn %s)" src))))
(save-buffer)
))
)
)

work-file)
)



(defun org-transclusion-keyword-value-plus (string)
"It is a utility function used converting a keyword STRING to plist.
It is meant to be used by `org-transclusion-get-string-to-plist'.
It needs to be set in
`org-transclusion-get-keyword-values-hook'.
Double qutations are mandatory."
(when (string-match ":plus +\\([^:]*\\)" string)
(list :plus (org-strip-quotes (match-string 1 string)))))

(defun org-transclusion-keyword-plist-to-string-plus (plist)
"Convert a keyword PLIST to a string.
This function is meant to be used as an extension for function
`org-transclusion-keyword-plist-to-string'. Add it to the
abnormal hook
`org-transclusion-keyword-plist-to-string-functions'."
(let ((string nil)
(plus (plist-get plist :plus))
)
(concat string
(when plus (format ":plus %s" plus))
)))


(provide 'org-transclusion-plus)
;;; org-transclusion-plus.el ends here
56 changes: 56 additions & 0 deletions test/test-plus.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#+TITLE: Test Plus




# Test REGEXP select and :lines together
#+transclude: [[file:test-2.0.org]] :plus (keep-lines "link") :lines 3-10 :src org

#+transclude: [[file:test-2.0.org]] :plus (insert "test for plus\n") :lines 1-10 :src org

# Test plus with code define in doc
#+transclude: [[file:test-2.0.org]] :plus test-1 :src org

# Test transclude self with
#+transclude: [[file:test-plus.org]] :plus (flush-lines "|.*| xs") :lines 30- :src org

* Test code

#+NAME: test-1
#+begin_src emacs-lisp
(goto-line 5 )
(kill-line 3)

(goto-line 12 )
(kill-line 3)

;; Do anything .....
#+end_src

* Test table
| Operator | Type(A) | Type(B) | Function |
|----------+----------------------+----------------------+---------------------------------------------------|
| A + B | numeric | numeric | op:numeric-add(A, B) |
| A + B | xs:date | xs:yearMonthDuration | op:add-yearMonthDuration-to-date(A, B) |
| A + B | xs:yearMonthDuration | xs:date | op:add-yearMonthDuration-to-date(B, A) |
| A + B | xs:date | xs:dayTimeDuration | op:add-dayTimeDuration-to-date(A, B) |
| A + B | xs:dayTimeDuration | xs:date | op:add-dayTimeDuration-to-date(B, A) |
| A + B | xs:time | xs:dayTimeDuration | op:add-dayTimeDuration-to-time(A, B) |
| A + B | xs:dayTimeDuration | xs:time | op:add-dayTimeDuration-to-time(B, A) |
| A + B | xs:dateTime | xs:yearMonthDuration | op:add-yearMonthDuration-to-dateTime(A, B) |
| A + B | xs:yearMonthDuration | xs:dateTime | op:add-yearMonthDuration-to-dateTime(B, A) |
| A + B | xs:dateTime | xs:dayTimeDuration | op:add-dayTimeDuration-to-dateTime(A, B) |
| A + B | xs:dayTimeDuration | xs:dateTime | op:add-dayTimeDuration-to-dateTime(B, A) |
| A + B | xs:yearMonthDuration | xs:yearMonthDuration | op:add-yearMonthDurations(A, B) |
| A + B | xs:dayTimeDuration | xs:dayTimeDuration | op:add-dayTimeDurations(A, B) |
| A - B | numeric | numeric | op:numeric-subtract(A, B) |
| A - B | xs:date | xs:date | op:subtract-dates(A, B) |
| A - B | xs:date | xs:yearMonthDuration | op:subtract-yearMonthDuration-from-date(A, B) |
| A - B | xs:date | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-date(A, B) |
| A - B | xs:time | xs:time | op:subtract-times(A, B) |
| A - B | xs:time | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-time(A, B) |
| A - B | xs:dateTime | xs:dateTime | op:subtract-dateTimes(A, B) |
| A - B | xs:dateTime | xs:yearMonthDuration | op:subtract-yearMonthDuration-from-dateTime(A, B) |
| A - B | xs:dateTime | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-dateTime(A, B) |
| A - B | xs:yearMonthDuration | xs:yearMonthDuration | op:subtract-yearMonthDurations(A, B) |
| A - B | xs:dayTimeDuration | xs:dayTimeDuration | op:subtract-dayTimeDurations(A, B) |