annotate.el icon indicating copy to clipboard operation
annotate.el copied to clipboard

[Feature Request] helper function to register minor-mode

Open pascalfleury opened this issue 7 years ago • 7 comments

By default, annotate-mode is only a local minor-mode, so it has to be registered in all relevant modes. Given its across the board usefulness, it would be nice to have a function that helps register it more broadly.

Making it a global minor-mode by registering it everywhere lead to issues though. This

(define-globalized-minor-mode global-annotate-mode annotate-mode
  (lambda () (annotate-mode 1)))
(global-annotate-mode 1)  ;; will interfere in some modes

will make it available everywhere. It does interfere with some modes that a) use many faces [magit, org-agenda-mode] and b) are not backed by an actual file [where I think it makes not much sense].

A helper function to register it in any mode backed by a file, or letting the user specify in which major-mode s/he does not want the annotate-mode would help setting it up faster.

pascalfleury avatar Nov 06 '18 22:11 pascalfleury

Good idea! Do you want to create a pull request for this?

bastibe avatar Nov 10 '18 11:11 bastibe

Hi! I tried to add this feature using the suggestion of the first poster ( @pascalfleury ). The best i was able to figure out is use the find-file-hook to ensure to load annotate-mode only when the buffer is filled from the contents of a file (plus i added a blacklist of major modes that annotate should not register to).

I wonder if this a good solution, though. There is a better way to do this?

Bye! C.

cage2 avatar Sep 18 '19 14:09 cage2

I wonder if the restriction on real files is actually necessary. It might be useful to annotate, say, a term buffer, then integrate the annotations and export the buffer.

In general, I think @pascalfleury's solution is more obvious and natural than hooking into find-file-hook. It should be possible to put all the blacklist and is-it-a-file logic into the lambda in @pascalfleury's code example. Or am I missing something, there?

At any rate, I would prefer to add the finished code to the README, as opposed to annotate.el itself, as it seems unintuitive to me to want to annotate every single buffer.

Just out of curiosity, what is your reasoning for using annotate.el in every single buffer?

bastibe avatar Sep 19 '19 06:09 bastibe

On Wed, Sep 18, 2019 at 11:23:34PM -0700, Bastian Bechtold wrote:

Hello!

I wonder if the restriction on real files is actually necessary. It might be useful to annotate, say, a term buffer, then integrate the annotations and export the buffer.

Good point! :)

In general, I think @pascalfleury's solution is more obvious and natural than hooking into find-file-hook. It should be possible to put all the blacklist and is-it-a-file logic into the lambda in @pascalfleury's code example. Or am I missing something, there?

i tried but i got some weird effects (duplicated annotation), i think i am lost some piece of the puzzle here, likely because i have no experience with elisp. More efforts needed here. :)

At any rate, I would prefer to add the finished code to the README, as opposed to annotate.el itself, as it seems unintuitive to me to want to annotate every single buffer. Just out of curiosity, what is your reasoning for using annotate.el in every single buffer?

What i do is use the 'find-file-hook' to load annotate mode, then i check if the major mode of the buffer of the just visited file is in the blacklist, if not i run some code contained in a function that is mentioned in the keyword :after-hook of define-minor-mode annotate-mode ..., is this wrong?

Bye! C.

cage2 avatar Sep 19 '19 14:09 cage2

That sounds like annotate-mode is somehow activated multiple times. But honestly, I have no idea.

bastibe avatar Sep 20 '19 10:09 bastibe

Hi! I'm using this hook myself to make sure annotate loads the overlays the least as possible:

(defun my-annotate-mode-hook ()
  (let ((file-name (buffer-file-name))
        (annotation-files (mapcar #'car (annotate-load-annotation-data))))
    (when (and file-name
               (member file-name annotation-files))
      (annotate-mode +1))))

(add-hook 'find-file-hook #'my-annotate-mode-hook)

I notice that this speeds up loading a file quite a bit compared to unconditionally adding annotate-mode to find-file-hook.

danilevy1212 avatar Mar 16 '22 02:03 danilevy1212

Hi!

Thanks for reviving this old issue! :+1:

The only change i would suggest would be to substitute car with annotate-filename-from-dump (i.e. (mapcar #'annotate-filename-from-dump (annotate-load-annotation-data)...); using the latter function will prevent the mapping to fails if the format of the annotations database will change in future (unlikely, but better safe than sorry ;-)).

Do you think there is a way to integrate your hook into the main code someway or, if not, adding it as a FAQ's answers in the README file?

Would you like to file an PR about one of the two solutions?

Bye and thanks for sharing your code! C.

cage2 avatar Mar 19 '22 10:03 cage2