0

I am using the folding minor mode in Emacs which I get installed with the following use-package macro:

(use-package folding
  :ensure t)

The above works and ensures that folding is installed whenever I fire up Emacs. However, when I visit a buffer with the folded marks ( // {{{ and // }}}), the minor mode is not automatically loaded and I have to load it explicitly with:

M-x folding-mode

I looked up the documentation and saw that for the Folding mode to start automatically when a folded file is loaded you need to do this:

(if (load "folding" 'nomessage 'noerror)
         (folding-mode-add-find-file-hook))

However, changing my use-package macro to the following:

(use-package folding
  :ensure t
  :config
  (if (load "folding" 'nomessage 'noerror)
      (folding-mode-add-find-file-hook))
  )

… makes no difference. I still have to load folding mode manually with M-x folding-mode. What gives?

update

Turns out that in addition to the accepted answer, I also had to add an Emacs file variable at the end of the file:

// {{{ folding-mode

// Local Variables:
// folded-file: t
// End:

// }}}

But adding this editor-specific markup in every source file is too much for me. Is there a way to automatically always activate folding-mode when visiting a buffer?

I have tried

(use-package folding
   :ensure t
   :config
     (folding-mode))

… and:

(use-package folding
   :ensure t
   :config
     (setq 'folding-mode t))

… but none of the above is working.

1

Just do

(use-package folding
  :ensure t
  :config
  (folding-mode-add-find-file-hook))

As to why, the

if (load "folding" 'nomessage 'noerror)

bit is really saying "try and load this package, and if it works invoke the next command" (see help for load). You take care of that using use-package already, so you just need to call the next command directly in :config.

Edit:

Based on the update, try something like this:

(use-package folding
  :ensure t
  :config
  (folding-install-hooks)
  (defun my-always-fold ()
    (setq-local folded-file t))
  (advice-add #'folding-mode-find-file :before #'my-always-fold))

That seems to work in my tests.

  • I tried that. Makes no difference. Having that in my .emacs and loading a file with // {{{ markings and then doing C-h v folding-mode reports that the value of the folding-mode is nil. I still have to do M-x folding-mode to activate it. – Marcus Junius Brutus Sep 16 '16 at 6:19
  • See updated question. Is there a way to enter folding-mode without having to set the Emacs file variable folded-file at the end of each file? – Marcus Junius Brutus Sep 16 '16 at 6:58
  • the updated use-package incantation worked like a charm. – Marcus Junius Brutus Sep 17 '16 at 22:32
0

Try:

(use-package folding
  :ensure t
  :init
  (folding-mode-add-find-file-hook))

It seems that you want to only load folding-mode the first time you visit a folded file, but the hook that detects whether a file is folded is in the folding-mode package! This of course is impossible, you'll have to uncoditionally load folding-mode as above.

You may prefer to stick with:

(use-package folding
  :ensure t
  :config
  (folding-mode-add-find-file-hook))

It's not as bad you might think: it's true that on the very first folded file you open in a given session you'll have to manually run M-x folding-mode, but then the hook gets installed and any other folded file you open will get detected and put in folding-mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.