From 9452ff5d9d36974d522fcb9d66ea7243202b1eb5 Mon Sep 17 00:00:00 2001 From: Brian Leung Date: Thu, 15 Aug 2019 04:42:37 +0200 Subject: [PATCH] Change handling of optional dependencies. org-recent-headings-helm: `declare-function` does not seem to work on macros, so `helm-make-source` is used instead of helm-build-sync-source. org-recent-headings-ivy: Add dispatch actions. --- org-recent-headings.el | 89 ++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/org-recent-headings.el b/org-recent-headings.el index aea76f5..335fb89 100644 --- a/org-recent-headings.el +++ b/org-recent-headings.el @@ -222,27 +222,26 @@ With prefix argument ARG, turn on if positive, otherwise off." ;;;;; Helm -(with-eval-after-load 'helm - - ;; This declaration is absolutely necessary for some reason. Even if `helm' is loaded - ;; before this package is loaded, an "invalid function" error will be raised when this - ;; package is loaded, unless this declaration is here. Even if I manually "(require - ;; 'helm)" and then load this package after the error (and Helm is already loaded, and I've - ;; verified that `helm-build-sync-source' is defined), once Emacs has tried to load this - ;; package thinking that the function is invalid, it won't stop thinking it's invalid. It - ;; also seems to be related to `defvar' not doing anything when run a second time (unless - ;; called with `eval-defun'). But at the same time, the error didn't always happen in my - ;; config, or with different combinations of `with-eval-after-load', "(when (fboundp 'helm) - ;; ...)", and loading packages in a different order. I don't know exactly why it's - ;; happening, but at the moment, this declaration seems to fix it. Let us hope it really - ;; does. I hope no one else is suffering from this, because if so, I have inflicted mighty - ;; annoyances upon them, and I wouldn't blame them if they never used this package again. - (declare-function helm-build-sync-source "helm") - (declare-function helm-exit-and-execute-action "helm") - (declare-function helm-marked-candidates "helm") - (declare-function with-helm-alive-p "helm") - (declare-function helm-make-actions "helm-lib") +;; This declaration is absolutely necessary for some reason. Even if `helm' is loaded +;; before this package is loaded, an "invalid function" error will be raised when this +;; package is loaded, unless this declaration is here. Even if I manually "(require +;; 'helm)" and then load this package after the error (and Helm is already loaded, and I've +;; verified that `helm-build-sync-source' is defined), once Emacs has tried to load this +;; package thinking that the function is invalid, it won't stop thinking it's invalid. It +;; also seems to be related to `defvar' not doing anything when run a second time (unless +;; called with `eval-defun'). But at the same time, the error didn't always happen in my +;; config, or with different combinations of `with-eval-after-load', "(when (fboundp 'helm) +;; ...)", and loading packages in a different order. I don't know exactly why it's +;; happening, but at the moment, this declaration seems to fix it. Let us hope it really +;; does. I hope no one else is suffering from this, because if so, I have inflicted mighty +;; annoyances upon them, and I wouldn't blame them if they never used this package again. +(declare-function helm-make-source "ext:helm-source") +(declare-function helm-exit-and-execute-action "ext:helm") +(declare-function helm-marked-candidates "ext:helm") +;; (declare-function with-helm-alive-p "ext:helm") +(declare-function helm-make-actions "ext:helm-lib") +(with-eval-after-load 'helm (defvar helm-map) (defvar org-recent-headings-helm-map (let ((map (copy-keymap helm-map))) @@ -251,7 +250,7 @@ With prefix argument ARG, turn on if positive, otherwise off." "Keymap for `helm-source-org-recent-headings'.") (defvar helm-source-org-recent-headings - (helm-build-sync-source " Recent Org headings" + (helm-make-source " Recent Org headings" 'helm-source-sync :candidates (lambda () (org-recent-headings--prepare-list) org-recent-headings-list) @@ -297,16 +296,54 @@ With prefix argument ARG, turn on if positive, otherwise off." ;;;;; Ivy -(with-eval-after-load 'ivy +(declare-function ivy-read "ext:ivy") +(declare-function ivy-set-actions "ext:ivy") - ;; TODO: Might need to declare `ivy-completing-read' also, but I - ;; haven't hit the error yet. +(with-eval-after-load 'ivy + (defun org-recent-headings-make-tuple (entry) + "Make a tuple containing the display text of ENTRY paired with ENTRY." + (let* ((init-display (org-recent-headings-entry-display entry)) + (heading-fragment (substring init-display + (next-property-change 0 init-display))) + (final-display (concat (org-recent-headings-entry-file entry) + ":" + heading-fragment))) + (cons final-display entry))) + + (defun org-recent-headings--show-entry-indirect-ivy (cand) + "Ivy-friendly wrapper for org-recent-headings--show-entry-indirect." + (org-recent-headings--show-entry-indirect + (cdr cand))) + (defun org-recent-headings--show-entry-direct-ivy (cand) + "Ivy-friendly wrapper for org-recent-headings--show-entry-direct." + (org-recent-headings--show-entry-direct + (cdr cand))) + (defun org-recent-headings--bookmark-entry-ivy (cand) + "Ivy-friendly wrapper for org-recent-headings--bookmark-entry" + (org-recent-headings--bookmark-entry + (cdr cand))) + (defun org-recent-headings--show-entry-default-ivy (cand) + "Ivy-friendly wrapper for org-recent-headings--show-entry-default." + (funcall org-recent-headings-show-entry-function + (cdr cand))) (defun org-recent-headings-ivy () "Choose from recent Org headings with Ivy." (interactive) - (let ((completing-read-function #'ivy-completing-read)) - (org-recent-headings)))) + (ivy-read "Recent Org headings: " + (progn + (org-recent-headings--prepare-list) + (-map #'org-recent-headings-make-tuple + org-recent-headings-list)) + :history 'org-recent-headings-ivy-history + :caller 'org-recent-headings-ivy + :action 'org-recent-headings--show-entry-default-ivy)) + + (ivy-set-actions + 'org-recent-headings-ivy + '(("n" org-recent-headings--show-entry-indirect-ivy "show in indirect buffer") + ("d" org-recent-headings--show-entry-direct-ivy "show in direct buffer") + ("b" org-recent-headings--bookmark-entry-ivy "bookmark heading")))) ;;;; Functions -- 2.22.0