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

It doesn't work in orgmode #13

Closed
Ypot opened this issue Aug 10, 2021 · 14 comments
Closed

It doesn't work in orgmode #13

Ypot opened this issue Aug 10, 2021 · 14 comments

Comments

@Ypot
Copy link

Ypot commented Aug 10, 2021

It works in my init.el file, but it doesn't work in my .org files.
It looks great, I hope that could be solved.

These are my settings:

;; Load fancy-dabbrev.el:
(require 'fancy-dabbrev)
;; Enable fancy-dabbrev previews everywhere:
(global-fancy-dabbrev-mode)
;; Bind fancy-dabbrev-expand and fancy-dabbrev-backward to your keys of
;; choice, here "TAB" and "Shift+TAB":
(global-set-key (kbd "TAB") 'fancy-dabbrev-expand)
(global-set-key (kbd "<backtab>") 'fancy-dabbrev-backward)
;; If you want TAB to indent the line like it usually does when the cursor
;; is not next to an expandable word, use 'fancy-dabbrev-expand-or-indent
;; instead of `fancy-dabbrev-expand`:
;; (global-set-key (kbd "TAB") 'fancy-dabbrev-expand-or-indent)
;; (global-set-key (kbd "<backtab>") 'fancy-dabbrev-backward)
(require 'popup)
@jrosdahl
Copy link
Owner

jrosdahl commented Aug 11, 2021

I don't use org-mode myself so I will need help to fix this. Pull requests welcome.

For starters, what do you mean by "doesn't work"?

Is it simply the case that TAB and/or <backtab> collide with org-mode's key bindings? If so, consider using other key bindings.

@Ypot
Copy link
Author

Ypot commented Aug 11, 2021

Hi. I mean that no word is suggested. Like if fancy-dabbrev wouldn't exist.

@DivineDominion
Copy link

@Ypot Does the tab key not trigger the popup, or do you expect regular dabbrev suggestion while you type and these also don't work?

Can you look what the tab key is bound to? C-h k TAB usually reveals org-cycle, because tab in org-mode-map is rebound. -- I'm experimenting with a fix for this to add a hook, so that when e.g. org-mode is triggered, fancy-dabbrev-expand-or-indent can forward to org-cycle instead of indent-for-tab-command.

@Ypot
Copy link
Author

Ypot commented Aug 16, 2021

@Ypot Does the tab key not trigger the popup, or do you expect regular dabbrev suggestion while you type and these also don't work?

Can you look what the tab key is bound to? C-h k TAB usually reveals org-cycle, because tab in org-mode-map is rebound. -- I'm experimenting with a fix for this to add a hook, so that when e.g. org-mode is triggered, fancy-dabbrev-expand-or-indent can forward to org-cycle instead of indent-for-tab-command.

Hi!
TAB writes down the one and only suggested word, if I am in my "init.el" buffer. If I am in orgmode, no suggestion is presented.

You are correct:

<tab> runs the command org-cycle (found in org-mode-map), which is an
autoloaded interactive compiled Lisp function in ‘org.el’.

@DivineDominion
Copy link

@Ypot for a quick fix you might want to look into rebinding tab in org-mode so you can expand and also do the org-cycle stuff otherwise.

fancy-dabbrev-expand-or-indent provides something similar, but if there's nothing to expand, it falls back to indent; you want to fall-back to org-cycle, so you'll need a similar-looking function for org:

(defun my/fancy-dabbrev-expand-or-org-cycle ()
  (interactive)
  (unless (fancy-dabbrev--expand)
    (call-interactively org-cycle)))
(define-key 'org-mode-map (kbd "<tab>") #'my/fancy-dabbrev-expand-or-org-cycle)

(Not tested, but this is the gist)

@Ypot
Copy link
Author

Ypot commented Aug 19, 2021

Thanks

I added this code:

(defun my/fancy-dabbrev-expand-or-org-cycle ()
  (interactive)
  (unless (fancy-dabbrev--expand)
    (call-interactively org-cycle))
:bind (:map org-mode-map
	     ("<tab>" . my/fancy-dabbrev-expand-or-org-cycle)))

Now TAB completes the word I am writing, but I can not choose. I can't see any option.

@DivineDominion
Copy link

You're right. It appears to behave as if one calls (fancy-dabbrev--expand) Using M-: -- which behaves different when you call it like that from when you call it with a shortcut.

Triggering the expansion popup, not just 1-time dabbrev expansion, might interfere more with org-cycle, though. Do you use the cycle command? If not, you can also bind tab to just fancy-dabbrev-expand in the org-mode-map without any fallbacks and see if that helps


Here's the internal implementation of expansion:

(defun fancy-dabbrev--expand ()
  "[internal] Perform expansion.

The function returns non-nil if an expansion was made, otherwise
nil."
  (let ((last-command-did-expand
         (and (fancy-dabbrev--is-fancy-dabbrev-command last-command)
              fancy-dabbrev--expansions)))
    (if (and (not last-command-did-expand)
             (or (not (fancy-dabbrev--looking-back-at-expandable))
                 (and fancy-dabbrev-expansion-on-preview-only
                      (not fancy-dabbrev--preview-overlay-was-visible))))
        (setq fancy-dabbrev--expansions nil)
      (if (fancy-dabbrev--any-bound-and-true fancy-dabbrev-no-expansion-for)
          (fancy-dabbrev--without-progress-reporter
           (dabbrev-expand nil))
        (add-hook 'post-command-hook #'fancy-dabbrev--post-command-hook)
        (if last-command-did-expand
            (fancy-dabbrev--expand-again t)
          (fancy-dabbrev--expand-first-time)))
      t)))

It has a check whatever was run last; and it calls fancy-dabbrev--expand-again on repeated invocation of that command.

You could try to inline more of the expansion function into your own function here, but that sounds cumbersome.

I personally would prefer to have making an expansion trigger an internal minor mode, like "is expanding, showing the popup", and then we have focused key bindings for what has to happen when expansion is active, but not settled.

jrosdahl added a commit that referenced this issue Aug 23, 2021
@jrosdahl
Copy link
Owner

jrosdahl commented Aug 23, 2021

org-mode + fancy-dabbrev seem to work fine for me with f3b05ad and something like this:

(use-package org
  :bind (:map org-mode-map
	     ("<tab>" . fancy-dabbrev-expand-or-indent)
         ("<backtab>" . fancy-dabbrev-backward))
  :config
  (setq org-cycle-emulate-tab nil)
  (add-hook 'org-mode-hook (lambda () (setq-local fancy-dabbrev-indent-command 'org-cycle))))

(use-package fancy-dabbrev
  :config
  (global-fancy-dabbrev-mode)
  (global-set-key (kbd "TAB") 'fancy-dabbrev-expand-or-indent)
  (global-set-key (kbd "<backtab>") 'fancy-dabbrev-backward))

@Ypot: Does this work good enough for you?

@Ypot
Copy link
Author

Ypot commented Aug 24, 2021

org-mode + fancy-dabbrev seem to work fine for me with f3b05ad and something like this:

(use-package org
  :bind (:map org-mode-map
	     ("<tab>" . fancy-dabbrev-expand-or-indent)
         ("<backtab>" . fancy-dabbrev-backward))
  :config
  (setq org-cycle-emulate-tab nil)
  (add-hook 'org-mode-hook (lambda () (setq-local fancy-dabbrev-indent-command 'org-cycle))))

(use-package fancy-dabbrev
  :config
  (global-fancy-dabbrev-mode)
  (global-set-key (kbd "TAB") 'fancy-dabbrev-expand-or-indent)
  (global-set-key (kbd "<backtab>") 'fancy-dabbrev-backward))

@Ypot: Does this work good enough for you?

Not bad! It works, but now I can't expand the org subtrees*
Thanks for your work.

*Subtrees can be expanded, but the cursor must be placed after a blank space.

@elazdins
Copy link

The above snippet helps me get the expansion menu, but not the preview. Are there any options of having the preview be shown in orgmode?

@jrosdahl
Copy link
Owner

@Ypot wrote:

Subtrees can be expanded, but the cursor must be placed after a blank space.

Right, since that's a valid expansion context the keypress will not be forwarded to org-cycle. That's expected.

Not bad! It works, but now I can't expand the org subtrees

I actually think that it would be a good idea not to use the same key binding for expansion and cycling since there will always be cases where you want something else than what happens. It's the same thing for me with ordinary indentation, by the way, I solve that by letting C-TAB do indentation instead of TAB.

@jrosdahl
Copy link
Owner

@elazdins wrote:

The above snippet helps me get the expansion menu, but not the preview. Are there any options of having the preview be shown in orgmode?

Perhaps you missed the "with f3b05ad" part? That is, you have to upgrade to f3b05ad to get the preview in org-mode.

@Ypot
Copy link
Author

Ypot commented Aug 25, 2021

@Ypot wrote:

Subtrees can be expanded, but the cursor must be placed after a blank space.

Right, since that's a valid expansion context the keypress will not be forwarded to org-cycle. That's expected.

Not bad! It works, but now I can't expand the org subtrees

I actually think that it would be a good idea not to use the same key binding for expansion and cycling since there will always be cases where you want something else than what happens. It's the same thing for me with ordinary indentation, by the way, I solve that by letting C-TAB do indentation instead of TAB.

Thanks. I will try these keybindings:

(use-package org
  :bind (:map org-mode-map
	     ("M-7" . fancy-dabbrev-expand-or-indent)
         ("M-8" . fancy-dabbrev-backward))
  :config
  (setq org-cycle-emulate-tab nil)
  (add-hook 'org-mode-hook (lambda () (setq-local fancy-dabbrev-indent-command 'org-cycle))))

(use-package fancy-dabbrev
  :config
  (global-fancy-dabbrev-mode)
  (global-set-key (kbd "M-7") 'fancy-dabbrev-expand-or-indent)
  (global-set-key (kbd "M-8") 'fancy-dabbrev-backward))

@elazdins
Copy link

Thank you Joel - just wanted to confirm that with f3b05ad now preview is working in org-mode too. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants