From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms1.migadu.com with LMTPS id uBk5BNecFWaAWwAAqHPOHw:P1 (envelope-from ) for ; Tue, 09 Apr 2024 21:53:59 +0200 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id uBk5BNecFWaAWwAAqHPOHw (envelope-from ) for ; Tue, 09 Apr 2024 21:53:59 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ms+12kuh; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1712692438; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=ivW42YB/LOf6DMpRyIxkqRAWu9CXdb7sb5j3xNIZXrk=; b=MCxrZox6t3c437Pfh4Gu5/M2wks/mCx9C+HqYomaqRTqX+Le+a3oNv0XJqdym+5Kx522OV wV9I6/isTvcRTsCVWsOYkDOPpYLm0F1ijHvh/KaqeL1fVHZAd3zJPQrlQ+1W0o4C8tFhIK qP4/ut0QhkRa76IpmTlGqeYY34yhZj3U6E470cYNEe5ThqTBxQzk2+2ZN1EPhNDPNHaFRx qg8I/E3OiZbeHzf4U6UO2R34v0sAbgkhhER+drQ5kbwTwl17FtvHmVK8hw6ObdlM0RIhLP Thwh4YR42Xfz3YdJCyykUeU4/kp6XKQahPky7f7vy+qenh1kcZALFVSW3lHlLA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1712692438; a=rsa-sha256; cv=none; b=lUDyBFouc8HWwLkFbS/gQ1JMRBiN7x+dnih8FLnDGzhMzoxjsFQ5MDe64QOH6lGH+5egoG 0Vgrk/m4/j3gwO0ocVDteMqwRj8+e6zC9XGRUCS3tcz4ZAiQIeLZz6o43/g+jP2Av+X6vL yBTOZMgrWeoOhzOXl+H/9Ht14LQnZNnux2lNPJgLOcaaHhzEI7Pd25TAmh7SvvJj5OVKoy cbuYTppjUs0i+ekdvG9olXKi1iRJGMWeti6TLd+dNGt8f02fAPtuV0BxPCNkxZyqZSbhmX TDaWlXiYyOH6qv3eWf540FP2pARttsXXLctIEIROVOPkZ2xZGE/ImqsuNA5P/Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ms+12kuh; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C3A5639B03 for ; Tue, 9 Apr 2024 21:53:58 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ruHWj-0007m2-6U; Tue, 09 Apr 2024 15:53:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ruHWh-0007lj-5k for emacs-orgmode@gnu.org; Tue, 09 Apr 2024 15:53:03 -0400 Received: from mail-oa1-x2f.google.com ([2001:4860:4864:20::2f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ruHWf-00074F-J2 for emacs-orgmode@gnu.org; Tue, 09 Apr 2024 15:53:02 -0400 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-22ed72b0082so2570900fac.3 for ; Tue, 09 Apr 2024 12:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712692379; x=1713297179; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ivW42YB/LOf6DMpRyIxkqRAWu9CXdb7sb5j3xNIZXrk=; b=ms+12kuhq1BXo4IE3wnMDgk+czyhfrGaD6TkM9RdpOE2+dJfCQYzyEwHHOBqx2Bd+1 +qBWKZbOik00cwjhF9LXSZ7M+lh+HdAdQUIq9DhRoOCUTQpimvpWH+X2hlO0e0QC6jHs ah4DEf3nL63uihz7zoYsIs/uFfRVjeB/7/eyKa5NZg9aqCFZUG0jUtXnsuPn5qlV4Ou8 RadF9Fr5+hkes7qtLc5BCdNZRgcyj6TUx70iTmVTBPz9gVGCj0eSjqLg89ZxGNzrY3M8 ecOxzkSx87Q45lIKtk8dXHzDCM2Tc9R+ellNjCV2TCBf8GMn6vZK864faKBGjnix07at 2+LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712692379; x=1713297179; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ivW42YB/LOf6DMpRyIxkqRAWu9CXdb7sb5j3xNIZXrk=; b=ib7FIGOe7MPrd3ROa7v37nEJ9Fm4mfS/oZDIVu2aN+jd/+gf/jzC/jvGvWClcKP/IH hwU5sO93bA3NcQEHn5e/XUl42huB4lEww3CM5beCg6HOlwOj4dq+m0XZ2vme1DzxvLOY 1tWWjbl436R47/KovsBDgcAYQAyrV9CE4Gc0r3HbLT084jf/qbblTi8a0kovny+k91QL LddEmT8dzD8rw8lxWRheZcZ97T2c7fMskT4VBZ9xW6MRe1FPnZUHBd3WuExwpGdehoBw hGJGo2jTVCbBR34go4kRDerzuMNcJg4R4lJUwpuf0cZUE6JPBH0jMNlCRSlw9X1GvhGd 5ABw== X-Forwarded-Encrypted: i=1; AJvYcCVTHAo0rLA2fUC7w6GfdfiVL6wJUsltKvHnhhycoA2c92sUnRyw0uTw5MvCOF7VbeHjrjBECmxvqH3fX+o6tM3Odv8t4/Q= X-Gm-Message-State: AOJu0YxwU3pQ3u7yby7YSKaxB43l7+F34tkAgel0gvcdr3ZBnXtDcdqL keRHs/ZtibLphyasIm4ZqTeDLExuusE1AyGj9HgGo7TF/PJVPM8UgVfA4Z2Zc8EMx25LMvnSfDI aL0M/3rN3audnyFo0flcm4emOzpM= X-Google-Smtp-Source: AGHT+IEJlb3CoG7QravRGDvy07RFVjjndZdE7hnynHfO+cq6cJaeu4DAS96gGKQk0QvPiQbY9yEjMbYJZzVKcOj8WhM= X-Received: by 2002:a05:6870:a68e:b0:22e:c697:5769 with SMTP id i14-20020a056870a68e00b0022ec6975769mr766923oam.42.1712692379399; Tue, 09 Apr 2024 12:52:59 -0700 (PDT) MIME-Version: 1.0 References: <87msq2397e.fsf@localhost> <875xwq49bd.fsf@gmail.com> In-Reply-To: <875xwq49bd.fsf@gmail.com> From: Rudi C Date: Tue, 9 Apr 2024 23:22:23 +0330 Message-ID: Subject: Re: [Q] startup hook: How do I detect if the current buffer has been opened programmatically? To: Karthik Chikmagalur Cc: Ihor Radchenko , emacs-orgmode@gnu.org Content-Type: multipart/alternative; boundary="000000000000e23c6e0615af44f6" Received-SPF: pass client-ip=2001:4860:4864:20::2f; envelope-from=rudiwillalwaysloveyou@gmail.com; helo=mail-oa1-x2f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -5.99 X-Spam-Score: -5.99 X-Migadu-Queue-Id: C3A5639B03 X-Migadu-Scanner: mx13.migadu.com X-TUID: lCdmWAsADi2f --000000000000e23c6e0615af44f6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable `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=E2=80=AFPM 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 > --000000000000e23c6e0615af44f6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
`find-file-hook` seems not to have false negatives, at lea= st. Using `(window-live-p (get-buffer-window (current-buffer) 'visible)= )` has false negatives for me (i.e., it returns nil for some interactive bu= ffers).

Thanks, everyone.

On Tue, Apr 9,= 2024 at 11:06=E2=80=AFPM Karthik Chikmagalur <karthikchikmagalur@gmail.com> wrote:
>> Org-mode occasi= onally 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 t= he user
>> within the startup hooks. This distinction is important because, e= .g., I
>> want to automatically preview all LaTeX fragments if the buffer wa= s 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-buff= er'
> macro in org-macs.el (on main).

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

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

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

Karthik
--000000000000e23c6e0615af44f6--