From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Samuel Wales Newsgroups: gmane.emacs.help Subject: Re: buffer substring of only visible text Date: Wed, 21 Sep 2022 15:57:57 -0700 Message-ID: References: <8735clm05b.fsf@sperrhaken.name> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34098"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: Felix Dietrich Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Sep 22 00:58:44 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ob8g0-0008ku-Fk for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 22 Sep 2022 00:58:44 +0200 Original-Received: from localhost ([::1]:51714 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ob8fy-0003kF-SF for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 21 Sep 2022 18:58:42 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36716) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ob8fL-0003js-RD for help-gnu-emacs@gnu.org; Wed, 21 Sep 2022 18:58:03 -0400 Original-Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]:40668) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ob8fJ-0005ZX-EL for help-gnu-emacs@gnu.org; Wed, 21 Sep 2022 18:58:03 -0400 Original-Received: by mail-lj1-x22e.google.com with SMTP id g20so7626326ljg.7 for ; Wed, 21 Sep 2022 15:58:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:from:to:cc:subject:date; bh=W2IlpXnazs1t3ZjRuEWD1kX1IsL+5ew+fxNbXN6bCYo=; b=NGZ7kYred5o0PBnnktXyrOoHLUp3JqYfgvjR5O+6UwcDj7274WpFzLUhjhN7y+FwhD meCOA1Ofq2HLWhbMrgvkxOVv33xXJB2uOBpReQzKfip2N98U+zqaO8VDNrrg3lyGWkHH FWhBNRcKMPsNBQZlZLK2u597fwVo5Htj8chOwAV9jDsbcXkSbAPsPFnwVm6Pzb0mKAG6 yBWnzXWZbwLoAn/ywzNwdbJRxExw2rFGYTQSRsIMN8N3MyfL9fQgA1adwzmgKlxK21+K IM+2/n6qZvNwmuYVVaK/mqZ1r+ZOEB9J1p1Luxuq4Ho18d+SkJW62Jg6ctQIEp4Vl/MA hSKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=W2IlpXnazs1t3ZjRuEWD1kX1IsL+5ew+fxNbXN6bCYo=; b=uWdgiSZLSde6bVMdLZROGathSFkCOSHxA+KweKS3VO3lR6dDYPYFTmEBS7WZYtQU1s 8876nQV3+iv8YkM6Qb4+pXHAFCjIIUfUiJqLBF5wgBDySAudhUEhyIxjNUbAGhsJx2wt MVuwPibib54X7lUCqxaJKivYt3OW0WTk1vcr+CLpsOHUogpYvhyN4FPWDwuyMB21XdE1 DRnWWVF+cBM8D/e1WoWP0atzNhrPtHRflr2zeU+9B3KjNptkVhFi11C/YcbE0B7a0Q1O SSGMzS6nCNjqFEHxaXYCZ0Eg42BETklZImcIhYvYW66hasZe4uWp7VgaNRhhe7nvRTHo 626A== X-Gm-Message-State: ACrzQf1Hu4S5RyZUnNa/SpoKCSU1qanvCCwZaw+sUEk+/LkdJ08OJ3ox 2sHmUxgRjgM56NXaeuqZ1vuGRJxlEFlV25yygPI= X-Google-Smtp-Source: AMsMyM6qqPwTQUz2TgPDcZJmShSXhmw8Mf+D9wddVeYNO/18lbJszwclT1z9nkjAN7vzPog3ZlFZcodLRvEZf2dfpT8= X-Received: by 2002:a2e:b74b:0:b0:26c:5cf3:cc87 with SMTP id k11-20020a2eb74b000000b0026c5cf3cc87mr128600ljo.75.1663801079236; Wed, 21 Sep 2022 15:57:59 -0700 (PDT) Original-Received: by 2002:aa6:c2eb:0:b0:21e:73da:40af with HTTP; Wed, 21 Sep 2022 15:57:57 -0700 (PDT) In-Reply-To: <8735clm05b.fsf@sperrhaken.name> Received-SPF: pass client-ip=2a00:1450:4864:20::22e; envelope-from=samologist@gmail.com; helo=mail-lj1-x22e.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, 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: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:139595 Archived-At: thank you for your response. very interesting and thorough. i was hoping there would be just a built in function; thank you for suggesting there is not. it is surely a useful function regardless of what it is used for so i will hang on to it. maybe can use. this problem with or without manuals has made my head swim ever since text properties arrived. i'm sure the manuals are good but my broken brain is not really getting text properties. results are sometimes off, i don't know the special ones, etc. for me it would be better if thre were a tool more powerful than c-u c-x =3D to inspect them. for example, if there were a tool to show all text properties and their intervals in a region or buffer or string. and also links to where any special text properties are documented. but that's a wish list i guess. it would help me understand emacs anyway. i forgot to mention more explicitly that i was hoping to not depend on htmlize as it is not in core, but that's a side point, as i will use htmlize if necessary, and also i think now, maybe, it /might/ be better to go line by line? but i am not sure. more of the x for which this is possibly an xy problem: as noted, i am trying to copy visible text from buffer a to b. and i want buffer b to have text properties that include markers to jump to a for specific lines. kinda like magit jumps to files. and so, those text properties in buffer b would include, in b, the markers in a. or... something. you can probably tell i am a little lost here. but with such a funciton like yours, i would put the markers in a, put the text properties redundantly in a, then copy over using the function. the redundant text properties seems wrong however. it seems efficient. so idk. one alternative seems to be to go line by line through a, insert markers in a, then create a string for each line with a text property containing the marker, then insert that string into b, then continue with the next matching visible line in a. which seems inefficient. idk if this is a common problem: copy parts of buffer a to buffer b such that you can RET to get to a. On 9/21/22, Felix Dietrich wrote: > Hello Samuel, > > Samuel Wales writes: > >> i want to copy visible text from a region in a buffer, into a string. >> if any invisible regions are in the region, i want them to not be in >> the string. that is, i want the visible intervals in the copied >> region to be merely appended. when i INSERT the resulting string into >> the new buffer, i want any text properties that i specify to still >> exist. htmlize.el has a function, buffer-substring-no-invisible, >> which does not include any text properties at all. it works, but i'd >> like to include at least one text property, which i want to specify. >> >> i am looking for a know a cookbook formula to do this really >> straightforwardly. > > I do not know any cookbook solution for this problem, but you have > already found a (fairly short and straightforward) function that almost > does what you described; with a few minor modifications you should be > able to get what you want (almost untested): > > #+begin_src emacs-lisp > (defun my/buffer-substring-with-properties (start end props) > ;; Copy the specified property from the specified region of the > ;; buffer to the target string. We cannot rely on Emacs to copy the > ;; property because we want to handle properties coming from both > ;; text properties and overlays. > (let ((text (buffer-substring-no-properties start end))) > (dolist (p props) > (htmlize-copy-prop p start end text)) > text)) > > > (defun my/buffer-substring-no-invisible (beg end &optional props) > ;; Like buffer-substring-no-properties, but don't copy invisible > ;; parts of the region. Where buffer-substring-no-properties > ;; mandates an ellipsis to be shown, htmlize-ellipsis is inserted. > (let ((pos beg) > visible-list invisible show last-show next-change) > ;; Iterate over the changes in the `invisible' property and filter > ;; out the portions where it's non-nil, i.e. where the text is > ;; invisible. > (while (< pos end) > (setq invisible (get-char-property pos 'invisible) > next-change (htmlize-next-change pos 'invisible end) > show (htmlize-decode-invisibility-spec invisible)) > (cond ((eq show t) > (push (my/buffer-substring-with-properties pos next-change > props) > visible-list)) > ((and (eq show 'ellipsis) > (not (eq last-show 'ellipsis)) > ;; Conflate successive ellipses. > (push htmlize-ellipsis visible-list)))) > (setq pos next-change last-show show)) > (htmlize-concat (nreverse visible-list)))) > #+end_src > > The function =E2=80=98my/buffer-substring-no-invisible=E2=80=99 only adds= an additional > parameter PROPS to =E2=80=98htmlize-buffer-substring-no-invisible=E2=80= =99 and replaces > the call to =E2=80=98htmlize-get-text-with-display=E2=80=99 with a call t= o > =E2=80=98my/buffer-substring-with-properties=E2=80=99 that takes the PROP= S parameter to > copy not only the two fixed properties =E2=80=98display=E2=80=99 and =E2= =80=98htmlize-link=E2=80=99, as > does =E2=80=98htmlize-get-text-with-display=E2=80=99, but allow specifyin= g the desired > properties to copy when calling the function. > > Text that has the =E2=80=98invisible=E2=80=99 property may be replaced wi= th an ellipsis > (depending on =E2=80=98buffer-invisibility-spec=E2=80=99). If you prefer= red there > rather be no ellipsis, adjust the cond-form or bind =E2=80=98htmlize-elli= psis=E2=80=99 > to the empty string "". > > As the comment at the top of =E2=80=98htmlize-copy-prop=E2=80=99 suggests= , Emacs may > provide more efficient means to copy text properties if you do not need > the overlay properties. I cannot help you with the details of that, > though, but maybe the sections on text and overlay properties in the > Emacs Lisp handbook can provide you with the necessary clues [1][2]. > > Footnotes: > [1] (info "(elisp) Text Properties") > > > > [2] (info "(elisp) Overlay Properties") > > > > -- > Felix Dietrich > --=20 The Kafka Pandemic A blog about science, health, human rights, and misopathy: https://thekafkapandemic.blogspot.com