abo-abo / avy

Jump to things in Emacs tree-style
1.74k stars 111 forks source link

Org-mode functions? #209

Closed alphapapa closed 7 years ago

alphapapa commented 7 years ago

Hi Oleh,

I have a few Org-mode-related Avy functions, and I wondered if you would be open to have them in Avy itself, similar to how Helm has helm-org.el with Org-related Helm functions. :) Let me know and I'd be glad to send a PR.

Thanks.

abo-abo commented 7 years ago

Any improvement PR is welcome. I just need to check for an Emacs CA before merging, since avy is in GNU ELPA. Do you have one?

alphapapa commented 7 years ago

Ah, I forgot about that. I haven't done it yet, but it's on my to-do list. :)

Just FYI, here's what I have in mind at the moment:

(defun ap/avy-org-goto-heading-timer (&optional arg)
  "Read one or many consecutive chars and jump to the first one.
The window scope is determined by `avy-all-windows' (ARG negates it)."
  (interactive "P")
  (let* ((avy-all-windows (if arg
                              (not avy-all-windows)
                            avy-all-windows))
         (input (ap/read-string-with-timeout))
         (regexp (rx-to-string `(seq bol
                                     (1+ "*")
                                     (1+ space)
                                     (0+ not-newline)
                                     ,input
                                     (0+ not-newline)
                                     eol))))
    (avy-with avy-goto-char-timer
      (avy--process (avy--regex-candidates regexp)
                    (avy--style-fn avy-style)))))

(cl-defun ap/read-string-with-timeout (&optional (timeout 0.3))
  "Read string from minibuffer with a timeout."
  ;; It's a shame that only `read-char' has the timeout option, so we
  ;; have to do this funky loop ourselves.
  (cl-loop with charnum
           with string = ""
           while (not (equal 13 charnum))
           for charnum = (read-char (format "Prompt: %s" string )
                                    t
                                    (unless (string-empty-p string)
                                      timeout))
           if (and charnum
                   (not (equal 13 charnum)))
           concat (make-string 1 charnum) into string
           else do (setq charnum 13)
           finally return string))

Works really well so far! You type an arbitrary number of characters with the input timer, and it only considers Org headings.

I'll send a PR when I get the CA done. Thanks!

abo-abo commented 7 years ago

Looks good. The initial highlighting isn't as nice as with avy-goto-char-timer, but that can be fixed.