emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] startup hook: How do I detect if the current buffer has been ope


From: Rudi C
Subject: Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
Date: Tue, 9 Apr 2024 23:22:23 +0330

`find-file-hook` seems not to have false negatives, at least. Using `(window-live-p (get-buffer-window (current-buffer) 'visible))` has false negatives for me (i.e., it returns nil for some interactive buffers).

Thanks, everyone.

On Tue, Apr 9, 2024 at 11:06 PM Karthik Chikmagalur <karthikchikmagalur@gmail.com> wrote:
>> Org-mode occasionally opens files automatically, for instance, when
>> inserting or opening ID links, or during certain searches. I need to
>> determine if a buffer was opened programmatically or manually by the user
>> within the startup hooks. This distinction is important because, e.g., I
>> want to automatically preview all LaTeX fragments if the buffer was opened
>> by the user, but not if it was opened programmatically.
>
> AFAIK, there is no reliable way to do this.
> You may play around with `find-file-hook'. See `org-with-file-buffer'
> macro in org-macs.el (on main).

A heuristic I use is to check if the window is visible.  It's not
perfect but good enough for my use (which includes previewing LaTeX
fragments):

(defun my/latex-preview-maybe ()
  (when (window-live-p (get-buffer-window (current-buffer)))
    (org-latex-preview 'buffer)
    (org-latex-preview-auto-mode 1)))

(add-hook 'org-mode-hook #'my/latex-preview-maybe)

Karthik

reply via email to

[Prev in Thread] Current Thread [Next in Thread]