Add docstrings

This commit is contained in:
Phil Bajsicki 2025-04-25 22:13:22 +02:00
parent 6628097077
commit a6f1e628ad
2 changed files with 60 additions and 7 deletions

View file

@ -249,6 +249,7 @@ The rationale behind using ~list-buffers~ is the same as with dired. They both d
Seems to be one of the most reliable tools in the basket... mostly because Seems to be one of the most reliable tools in the basket... mostly because
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tool--list-buffers (&optional arg) (defun gptel-org-tool--list-buffers (&optional arg)
"Return list of buffers."
(list-buffers-noselect) (list-buffers-noselect)
(with-current-buffer "*Buffer List*" (with-current-buffer "*Buffer List*"
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -277,6 +278,7 @@ Be sure to customize the function to point to your org directory, if you wish. I
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--dir (dir) (defun gptel-org-tools--dir (dir)
"Return directory listing."
(with-temp-buffer (with-temp-buffer
(dired (or dir "~")) (dired (or dir "~"))
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -299,6 +301,7 @@ Be sure to customize the function to point to your org directory, if you wish. I
Disabled for now, as it's causing some issues. Disabled for now, as it's causing some issues.
#+begin_src elisp :tangle no :results none #+begin_src elisp :tangle no :results none
(defun gptel-org-tools--find-buffer-visiting (filename) (defun gptel-org-tools--find-buffer-visiting (filename)
"Return the buffer visiting file FILENAME."
(concat (concat
(bufferp (bufferp
(find-buffer-visiting (find-buffer-visiting
@ -319,6 +322,7 @@ Disabled for now, as it's causing some issues.
Continuation from above. Open a file into a buffer for processing, once it's found by dired-list. Continuation from above. Open a file into a buffer for processing, once it's found by dired-list.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--open-file-inactive (file) (defun gptel-org-tools--open-file-inactive (file)
"Open FILE in a buffer."
(with-current-buffer (get-buffer-create file) (with-current-buffer (get-buffer-create file)
(insert-file-contents file) (insert-file-contents file)
(concat (concat
@ -338,6 +342,7 @@ Continuation from above. Open a file into a buffer for processing, once it's fou
This reads file contents, This reads file contents,
#+begin_src elisp :tangle no #+begin_src elisp :tangle no
(defun gptel-org-tools--read-file-contents (file) (defun gptel-org-tools--read-file-contents (file)
"Return contents of FILE."
(with-temp-buffer (with-temp-buffer
(insert-file-contents (expand-file-name filename)) (insert-file-contents (expand-file-name filename))
(concat (concat
@ -358,6 +363,7 @@ This reads file contents,
**** describe-variable **** describe-variable
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--describe-variable (var) (defun gptel-org-tools--describe-variable (var)
"Return documentation for VAR."
(let ((symbol (intern var))) (let ((symbol (intern var)))
(if (boundp symbol) (if (boundp symbol)
(prin1-to-string (symbol-value symbol)) (prin1-to-string (symbol-value symbol))
@ -377,6 +383,7 @@ This reads file contents,
**** describe-function **** describe-function
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--describe-function (fun) (defun gptel-org-tools--describe-function (fun)
"Return documentation for FUN."
(let ((symbol (intern fun))) (let ((symbol (intern fun)))
(if (fboundp symbol) (if (fboundp symbol)
(prin1-to-string (documentation symbol 'function)) (prin1-to-string (documentation symbol 'function))
@ -425,6 +432,7 @@ Pretty simple, does what it says on the tin. It gets all the tags from the =buff
This is not, by any means, sufficient, but I do tag people and specific events frequently enough that it helps save on the context window. This is not, by any means, sufficient, but I do tag people and specific events frequently enough that it helps save on the context window.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-extract-tags (buffer) (defun gptel-org-tools--org-extract-tags (buffer)
"Return all tags from BUFFER."
(with-current-buffer buffer (with-current-buffer buffer
(let ((tags '())) (let ((tags '()))
(org-map-entries (org-map-entries
@ -454,6 +462,7 @@ Then we need to pull /some/ information from the buffer, without dragging the en
Therefore, headings. A reasonable amount of information, and still keeping the signal-to-noise ratio pretty decent. Therefore, headings. A reasonable amount of information, and still keeping the signal-to-noise ratio pretty decent.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-extract-headings (buffer) (defun gptel-org-tools--org-extract-headings (buffer)
"Return all headings from BUFFER."
(if (member buffer gptel-org-tools-skip-heading-extraction) (if (member buffer gptel-org-tools-skip-heading-extraction)
(user-error "Buffer %s has too many headings, use org-extract-tags or org-ql-select-rifle." buffer) (user-error "Buffer %s has too many headings, use org-extract-tags or org-ql-select-rifle." buffer)
(with-current-buffer buffer (with-current-buffer buffer
@ -489,6 +498,9 @@ Currently *not* tangled, as I'm testing breaking out each type of query into its
#+begin_src elisp :tangle no #+begin_src elisp :tangle no
(defun gptel-org-tools--org-ql-select (buf query) (defun gptel-org-tools--org-ql-select (buf query)
"Return entries matching QUERY from BUFFER.
QUERY can be any valid org-ql-select query."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
(if (stringp query) (if (stringp query)
@ -566,6 +578,11 @@ DATE is the date or date range to match."
Original (works but not ideal). Original (works but not ideal).
#+begin_src elisp :tangle no #+begin_src elisp :tangle no
(defun gptel-org-tools--org-ql-select-by-date (buf date) (defun gptel-org-tools--org-ql-select-by-date (buf date)
"Return headings from BUFFER matching DATE.
DATE must be in the form of YYYY, YYYY-MM, or YYYY-MM-DD."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(heading ,date) `(heading ,date)
@ -591,6 +608,7 @@ This is still work in progress, the idea is to have the LLM check my calendar an
It works, in principle, but I haven't been able to find a use for it yet. The real challenge is in building a context where the tools integrate with each-other in a way that makes sense. For now, this exists. It works, in principle, but I haven't been able to find a use for it yet. The real challenge is in building a context where the tools integrate with each-other in a way that makes sense. For now, this exists.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-agenda-seek (days) (defun gptel-org-tools--org-agenda-seek (days)
"Return the results of org-agenda-list spanning now to DAYS."
(with-temp-buffer (with-temp-buffer
(org-agenda-list (or days 14)) (org-agenda-list (or days 14))
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -616,6 +634,7 @@ The following tools are still very much WIP, and I think they're self-explanator
Retrieve the headings where the heading matches query.. Retrieve the headings where the heading matches query..
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-headings (buf query) (defun gptel-org-tools--org-ql-select-headings (buf query)
"Return headings matching QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(heading ,query) `(heading ,query)
@ -640,6 +659,7 @@ Retrieve the headings where the heading matches query..
Retrieve all the headings where either heading or content matches query. Retrieve all the headings where either heading or content matches query.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-headings-rifle (buf query) (defun gptel-org-tools--org-ql-select-headings-rifle (buf query)
"Return headings of entries (body included) that match keyword QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(rifle ,query) `(rifle ,query)
@ -664,6 +684,7 @@ Retrieve all the headings where either heading or content matches query.
This pulls all the headings (and their contents) when they match tags (without inheritance.) This pulls all the headings (and their contents) when they match tags (without inheritance.)
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-tags-local (buf query) (defun gptel-org-tools--org-ql-select-tags-local (buf query)
"Return entries whose tags match QUERY in BUFFER, without inheritance."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags-local ,query) `(tags-local ,query)
@ -688,6 +709,7 @@ This pulls all the headings (and their contents) when they match tags (without i
This pulls all the local tags (without inheritance) from buffer, and returns the number of these tagged headings. This pulls all the local tags (without inheritance) from buffer, and returns the number of these tagged headings.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-tags-local-count (buf query) (defun gptel-org-tools--org-ql-select-tags-local-count (buf query)
"Return count of entries tagged QUERY in BUFFER."
(length (org-ql-select (length (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags-local ,query) `(tags-local ,query)
@ -712,6 +734,7 @@ This pulls all the local tags (without inheritance) from buffer, and returns the
This pulls all the headings (and their contents) when they match tags (with inheritance; if a parent entry has the tag, descendant entries do, too.) This pulls all the headings (and their contents) when they match tags (with inheritance; if a parent entry has the tag, descendant entries do, too.)
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-tags (buf query) (defun gptel-org-tools--org-ql-select-tags (buf query)
"Return every entry tagged QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags ,query) `(tags ,query)
@ -735,6 +758,7 @@ This pulls all the headings (and their contents) when they match tags (with inhe
And, the "grab everything that matches" tool. And, the "grab everything that matches" tool.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-rifle (buf query) (defun gptel-org-tools--org-ql-select-rifle (buf query)
"Return every entry matching keyword QUERY from BUFFER."
(let ((buffer (get-buffer buf))) (let ((buffer (get-buffer buf)))
(if buffer (if buffer
(org-ql-select (org-ql-select
@ -761,6 +785,8 @@ And, the "grab everything that matches" tool.
This pulls all the headings (and their contents) when they match tags (without inheritance.) This pulls all the headings (and their contents) when they match tags (without inheritance.)
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-all-tags-local (query) (defun gptel-org-tools--org-ql-select-all-tags-local (query)
"Return entries whose tags match QUERY in org-agenda-files.
QUERY is the tag to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(tags-local ,query) `(tags-local ,query)
@ -782,6 +808,9 @@ This pulls all the headings (and their contents) when they match tags (without i
This pulls all the headings (and their contents) when they match tags (with inheritance; if a parent entry has the tag, descendant entries do, too.) This pulls all the headings (and their contents) when they match tags (with inheritance; if a parent entry has the tag, descendant entries do, too.)
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-all-tags (query) (defun gptel-org-tools--org-ql-select-all-tags (query)
"Return entries whose tags match QUERY,
with inheritance, in org-agenda-files.
QUERY is the tag to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(tags ,query) `(tags ,query)
@ -811,6 +840,8 @@ This means that /every org-mode file I have/ is part of this search.
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-all-rifle (query) (defun gptel-org-tools--org-ql-select-all-rifle (query)
"Return entries containing QUERY from org-agenda-files.
QUERY is the keyword to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(rifle ,query) `(rifle ,query)
@ -831,11 +862,12 @@ This means that /every org-mode file I have/ is part of this search.
***** org-ql-select-all-regexp ***** org-ql-select-all-regexp
#+begin_src elisp #+begin_src elisp
(defun gptel-org-tools--org-ql-select-all-regexp (query) (defun gptel-org-tools--org-ql-select-all-regexp (query)
(let ((bound-query (format "\\b%s\\b" (regexp-quote query)))) "Return all entries matching regexp QUERY in org-agenda-files.
QUERY is a regular expression."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(regexp ,bound-query) `(regexp ,query)
:action #'gptel-org-tools--heading-body))) :action #'gptel-org-tools--heading-body))
(add-to-list 'gptel-org-tools (add-to-list 'gptel-org-tools
(gptel-make-tool (gptel-make-tool
@ -848,7 +880,6 @@ This means that /every org-mode file I have/ is part of this search.
:category "org-ql")) :category "org-ql"))
#+end_src #+end_src
** End ** End
#+begin_src elisp #+begin_src elisp
(provide 'gptel-org-tools) (provide 'gptel-org-tools)

View file

@ -62,6 +62,7 @@
"---\n")) "---\n"))
(defun gptel-org-tool--list-buffers (&optional arg) (defun gptel-org-tool--list-buffers (&optional arg)
"Return list of buffers."
(list-buffers-noselect) (list-buffers-noselect)
(with-current-buffer "*Buffer List*" (with-current-buffer "*Buffer List*"
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -79,6 +80,7 @@
:category "emacs")) :category "emacs"))
(defun gptel-org-tools--dir (dir) (defun gptel-org-tools--dir (dir)
"Return directory listing."
(with-temp-buffer (with-temp-buffer
(dired (or dir "~")) (dired (or dir "~"))
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -97,6 +99,7 @@
:category "filesystem")) :category "filesystem"))
(defun gptel-org-tools--open-file-inactive (file) (defun gptel-org-tools--open-file-inactive (file)
"Open FILE in a buffer."
(with-current-buffer (get-buffer-create file) (with-current-buffer (get-buffer-create file)
(insert-file-contents file) (insert-file-contents file)
(concat (concat
@ -112,6 +115,7 @@
:category "filesystem")) :category "filesystem"))
(defun gptel-org-tools--describe-variable (var) (defun gptel-org-tools--describe-variable (var)
"Return documentation for VAR."
(let ((symbol (intern var))) (let ((symbol (intern var)))
(if (boundp symbol) (if (boundp symbol)
(prin1-to-string (symbol-value symbol)) (prin1-to-string (symbol-value symbol))
@ -128,6 +132,7 @@
:category "emacs")) :category "emacs"))
(defun gptel-org-tools--describe-function (fun) (defun gptel-org-tools--describe-function (fun)
"Return documentation for FUN."
(let ((symbol (intern fun))) (let ((symbol (intern fun)))
(if (fboundp symbol) (if (fboundp symbol)
(prin1-to-string (documentation symbol 'function)) (prin1-to-string (documentation symbol 'function))
@ -145,6 +150,7 @@
:category "emacs")) :category "emacs"))
(defun gptel-org-tools--org-extract-tags (buffer) (defun gptel-org-tools--org-extract-tags (buffer)
"Return all tags from BUFFER."
(with-current-buffer buffer (with-current-buffer buffer
(let ((tags '())) (let ((tags '()))
(org-map-entries (org-map-entries
@ -167,6 +173,7 @@
:category "org-mode")) :category "org-mode"))
(defun gptel-org-tools--org-extract-headings (buffer) (defun gptel-org-tools--org-extract-headings (buffer)
"Return all headings from BUFFER."
(if (member buffer gptel-org-tools-skip-heading-extraction) (if (member buffer gptel-org-tools-skip-heading-extraction)
(user-error "Buffer %s has too many headings, use org-extract-tags or org-ql-select-rifle." buffer) (user-error "Buffer %s has too many headings, use org-extract-tags or org-ql-select-rifle." buffer)
(with-current-buffer buffer (with-current-buffer buffer
@ -216,6 +223,7 @@ DATE is the date or date range to match."
:category "org")) :category "org"))
(defun gptel-org-tools--org-agenda-seek (days) (defun gptel-org-tools--org-agenda-seek (days)
"Return the results of org-agenda-list spanning now to DAYS."
(with-temp-buffer (with-temp-buffer
(org-agenda-list (or days 14)) (org-agenda-list (or days 14))
(let ((content (buffer-string))) (let ((content (buffer-string)))
@ -233,6 +241,7 @@ DATE is the date or date range to match."
:category "org")) :category "org"))
(defun gptel-org-tools--org-ql-select-headings (buf query) (defun gptel-org-tools--org-ql-select-headings (buf query)
"Return headings matching QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(heading ,query) `(heading ,query)
@ -253,6 +262,7 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-headings-rifle (buf query) (defun gptel-org-tools--org-ql-select-headings-rifle (buf query)
"Return headings of entries (body included) that match keyword QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(rifle ,query) `(rifle ,query)
@ -273,6 +283,7 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-tags-local (buf query) (defun gptel-org-tools--org-ql-select-tags-local (buf query)
"Return entries whose tags match QUERY in BUFFER, without inheritance."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags-local ,query) `(tags-local ,query)
@ -293,6 +304,7 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-tags-local-count (buf query) (defun gptel-org-tools--org-ql-select-tags-local-count (buf query)
"Return count of entries tagged QUERY in BUFFER."
(length (org-ql-select (length (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags-local ,query) `(tags-local ,query)
@ -313,6 +325,7 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-tags (buf query) (defun gptel-org-tools--org-ql-select-tags (buf query)
"Return every entry tagged QUERY from BUFFER."
(org-ql-select (org-ql-select
(get-buffer buf) (get-buffer buf)
`(tags ,query) `(tags ,query)
@ -332,6 +345,7 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-rifle (buf query) (defun gptel-org-tools--org-ql-select-rifle (buf query)
"Return every entry matching keyword QUERY from BUFFER."
(let ((buffer (get-buffer buf))) (let ((buffer (get-buffer buf)))
(if buffer (if buffer
(org-ql-select (org-ql-select
@ -354,6 +368,8 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-all-tags-local (query) (defun gptel-org-tools--org-ql-select-all-tags-local (query)
"Return entries whose tags match QUERY in org-agenda-files.
QUERY is the tag to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(tags-local ,query) `(tags-local ,query)
@ -371,6 +387,9 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-all-tags (query) (defun gptel-org-tools--org-ql-select-all-tags (query)
"Return entries whose tags match QUERY,
with inheritance, in org-agenda-files.
QUERY is the tag to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(tags ,query) `(tags ,query)
@ -387,6 +406,8 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-all-rifle (query) (defun gptel-org-tools--org-ql-select-all-rifle (query)
"Return entries containing QUERY from org-agenda-files.
QUERY is the keyword to search for."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(rifle ,query) `(rifle ,query)
@ -403,11 +424,12 @@ DATE is the date or date range to match."
:category "org-ql")) :category "org-ql"))
(defun gptel-org-tools--org-ql-select-all-regexp (query) (defun gptel-org-tools--org-ql-select-all-regexp (query)
(let ((bound-query (format "\\b%s\\b" (regexp-quote query)))) "Return all entries matching regexp QUERY in org-agenda-files.
QUERY is a regular expression."
(org-ql-select (org-ql-select
(org-agenda-files) (org-agenda-files)
`(regexp ,bound-query) `(regexp ,query)
:action #'gptel-org-tools--heading-body))) :action #'gptel-org-tools--heading-body))
(add-to-list 'gptel-org-tools (add-to-list 'gptel-org-tools
(gptel-make-tool (gptel-make-tool