emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
@ 2024-04-08 17:31 Rudi C
  2024-04-09 14:24 ` Ihor Radchenko
  2024-04-11 16:37 ` Max Nikulin
  0 siblings, 2 replies; 5+ messages in thread
From: Rudi C @ 2024-04-08 17:31 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 445 bytes --]

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.

PS: Please use reply-to-all.

[-- Attachment #2: Type: text/html, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
  2024-04-08 17:31 [Q] startup hook: How do I detect if the current buffer has been opened programmatically? Rudi C
@ 2024-04-09 14:24 ` Ihor Radchenko
  2024-04-09 19:36   ` Karthik Chikmagalur
  2024-04-11 16:37 ` Max Nikulin
  1 sibling, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2024-04-09 14:24 UTC (permalink / raw)
  To: Rudi C; +Cc: emacs-orgmode

Rudi C <rudiwillalwaysloveyou@gmail.com> writes:

> 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).

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
  2024-04-09 14:24 ` Ihor Radchenko
@ 2024-04-09 19:36   ` Karthik Chikmagalur
  2024-04-09 19:52     ` Rudi C
  0 siblings, 1 reply; 5+ messages in thread
From: Karthik Chikmagalur @ 2024-04-09 19:36 UTC (permalink / raw)
  To: Ihor Radchenko, Rudi C; +Cc: emacs-orgmode

>> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
  2024-04-09 19:36   ` Karthik Chikmagalur
@ 2024-04-09 19:52     ` Rudi C
  0 siblings, 0 replies; 5+ messages in thread
From: Rudi C @ 2024-04-09 19:52 UTC (permalink / raw)
  To: Karthik Chikmagalur; +Cc: Ihor Radchenko, emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1369 bytes --]

`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
>

[-- Attachment #2: Type: text/html, Size: 1805 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically?
  2024-04-08 17:31 [Q] startup hook: How do I detect if the current buffer has been opened programmatically? Rudi C
  2024-04-09 14:24 ` Ihor Radchenko
@ 2024-04-11 16:37 ` Max Nikulin
  1 sibling, 0 replies; 5+ messages in thread
From: Max Nikulin @ 2024-04-11 16:37 UTC (permalink / raw)
  To: Rudi C, emacs-orgmode

On 09/04/2024 00:31, Rudi C wrote:
> I want to automatically preview all LaTeX fragments if the buffer was 
> opened by the user

Be careful and limit automatic preview to files you created yourself. 
Doing so for an arbitrary file downloaded from net might cause huge 
amount of garbage files in /tmp.

Emacs-29.3 includes a fix for CVE-2024-30204 that is specific for email 
attachment inline preview. When a regular file is opened, weird LaTeX 
code inside still may be executed. There is no general way to determine 
if specific file should be treated as trusted.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-11 16:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-08 17:31 [Q] startup hook: How do I detect if the current buffer has been opened programmatically? Rudi C
2024-04-09 14:24 ` Ihor Radchenko
2024-04-09 19:36   ` Karthik Chikmagalur
2024-04-09 19:52     ` Rudi C
2024-04-11 16:37 ` Max Nikulin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).