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

Allow user files via elisp-demos-user-files, always show Demos section and include an "[Add]" button #18

Merged
merged 8 commits into from
Jan 16, 2024
Prev Previous commit
let's see if this fixes the spaces
  • Loading branch information
sachac committed Jan 16, 2024
commit b6bea11b8b3a4ba3998eccad0f6a4cde309426d5
152 changes: 76 additions & 76 deletions elisp-demos.el
Original file line number Diff line number Diff line change
@@ -38,28 +38,28 @@
elisp-demos--load-dir))

(defcustom elisp-demos-user-files nil
"Files to search in addition to the one from the elisp-demos package.
"Files to search in addition to the one from the elisp-demos package.
If set, new notes are added to the first file in this list."
:group 'help
:type '(repeat file))
:group 'help
:type '(repeat file))

(defun elisp-demos--search (symbol)
(let (results)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(delay-mode-hooks (org-mode))
(when-let ((pos (org-find-exact-headline-in-buffer (symbol-name symbol))))
(goto-char pos)
(org-end-of-meta-data)
(push (string-trim
(buffer-substring-no-properties
(point)
(org-end-of-subtree)))
results)))))
(when results
(string-join (nreverse results) "\n\n"))))
(let (results)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(delay-mode-hooks (org-mode))
(when-let ((pos (org-find-exact-headline-in-buffer (symbol-name symbol))))
(goto-char pos)
(org-end-of-meta-data)
(push (string-trim
(buffer-substring-no-properties
(point)
(org-end-of-subtree)))
results)))))
(when results
(string-join (nreverse results) "\n\n"))))

(defun elisp-demos--syntax-highlight (orgsrc)
(with-temp-buffer
@@ -72,15 +72,15 @@ If set, new notes are added to the first file in this list."
(buffer-string)))

(defun elisp-demos--symbols ()
(let (symbols)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(push (org-entry-get (point) "ITEM") symbols))))
(mapcar 'intern (sort (seq-uniq symbols) #'string<))))
(let (symbols)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(push (org-entry-get (point) "ITEM") symbols))))
(mapcar 'intern (sort (seq-uniq symbols) #'string<))))

(declare-function org-show-entry "org" ())
(declare-function org-insert-heading "org" (&optional arg invisible-ok top))
@@ -100,14 +100,14 @@ If set, new notes are added to the first file in this list."
(mapcar #'symbol-name symbols)
nil t nil nil default-val)))))
(cl-assert symbol)
(catch 'found
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-current-buffer (find-file-noselect file)
(let ((pos (org-find-exact-headline-in-buffer (symbol-name symbol))))
(when pos
(goto-char pos)
(org-show-entry)
(throw 'found (point)))))))
(catch 'found
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-current-buffer (find-file-noselect file)
(let ((pos (org-find-exact-headline-in-buffer (symbol-name symbol))))
(when pos
(goto-char pos)
(org-show-entry)
(throw 'found (point)))))))
t)

;; Borrowed from `helpful--read-symbol'
@@ -136,7 +136,7 @@ If set, new notes are added to the first file in this list."
(macrop sym))))))
;; Try to reuse existing window
(let* ((file (or (car elisp-demos-user-files) elisp-demos--elisp-demos\.org))
(buffer (get-file-buffer file))
(buffer (get-file-buffer file))
(window (and buffer (get-buffer-window buffer))))
(if window
(select-window window)
@@ -214,17 +214,17 @@ If set, new notes are added to the first file in this list."
(goto-char (line-beginning-position))
(let ((inhibit-read-only t))
(insert
(helpful--heading "Demos")
(if (and src (not (string= src "")))
(concat
(propertize (elisp-demos--syntax-highlight src)
'start (point)
'symbol helpful--sym
'keymap elisp-demos-help-keymap)
"\n\n")
"")
(buttonize "[Add]" #'elisp-demos-add-demo helpful--sym)
"\n\n"))))))
(helpful--heading "Demos")
(if (and src (not (string= src "")))
(concat
(propertize (elisp-demos--syntax-highlight src)
'start (point)
'symbol helpful--sym
'keymap elisp-demos-help-keymap)
"\n\n")
"")
(buttonize "[Add]" #'elisp-demos-add-demo helpful--sym)
"\n\n"))))))

;;;###autoload
(defun elisp-demos-for-helpful ()
@@ -239,34 +239,34 @@ If set, new notes are added to the first file in this list."
(defun elisp-demos--export-json-file (json-file)
"Export all demos as json to JSON-FILE."
(require 'json)
(let ((output-buffer (generate-new-buffer " *elisp-demos-json*"))
title body beg end)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(setq title (org-entry-get (point) "ITEM"))
(setq beg (save-excursion
(forward-line 1)
(line-beginning-position)))
(setq end (save-excursion
(if (re-search-forward "^\\* " nil t)
(line-beginning-position)
(point-max))))
(setq body (buffer-substring-no-properties beg end))
(setq title (string-trim title))
(setq body (string-trim body))
(with-current-buffer output-buffer
(insert
(json-encode-string title) ": " (json-encode-string body) ",\n")))))
(with-current-buffer output-buffer
(delete-char -2)
(goto-char (point-min)) (insert "{\n")
(goto-char (point-max)) (insert "}\n")
(write-region (point-min) (point-max) json-file))
(kill-buffer output-buffer)))
(let ((output-buffer (generate-new-buffer " *elisp-demos-json*"))
title body beg end)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(setq title (org-entry-get (point) "ITEM"))
(setq beg (save-excursion
(forward-line 1)
(line-beginning-position)))
(setq end (save-excursion
(if (re-search-forward "^\\* " nil t)
(line-beginning-position)
(point-max))))
(setq body (buffer-substring-no-properties beg end))
(setq title (string-trim title))
(setq body (string-trim body))
(with-current-buffer output-buffer
(insert
(json-encode-string title) ": " (json-encode-string body) ",\n")))))
(with-current-buffer output-buffer
(delete-char -2)
(goto-char (point-min)) (insert "{\n")
(goto-char (point-max)) (insert "}\n")
(write-region (point-min) (point-max) json-file))
(kill-buffer output-buffer)))

(provide 'elisp-demos)
;;; elisp-demos.el ends here
Loading