From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Felix Dietrich Newsgroups: gmane.emacs.help Subject: Re: buffer substring of only visible text Date: Wed, 21 Sep 2022 13:34:24 +0200 Message-ID: <8735clm05b.fsf@sperrhaken.name> References: 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="26755"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) Cc: help-gnu-emacs@gnu.org To: Samuel Wales Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 21 13:36:43 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 1oay1z-0006jc-26 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 21 Sep 2022 13:36:43 +0200 Original-Received: from localhost ([::1]:58240 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oay1v-000577-E1 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 21 Sep 2022 07:36:41 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oaxzv-00054m-Pp for help-gnu-emacs@gnu.org; Wed, 21 Sep 2022 07:34:37 -0400 Original-Received: from mout.kundenserver.de ([212.227.126.187]:41313) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oaxzs-0007sx-Vy for help-gnu-emacs@gnu.org; Wed, 21 Sep 2022 07:34:35 -0400 Original-Received: from localhost ([89.14.46.249]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MAflI-1oPwS428s4-00B5Y6; Wed, 21 Sep 2022 13:34:28 +0200 In-Reply-To: (Samuel Wales's message of "Tue, 20 Sep 2022 18:08:03 -0700") X-Provags-ID: V03:K1:W1dTzMQC7RKIXtGh6hi0RluIYlho+b6EtFKmQMo2J6I8/wEaA/L X0FCo/asbrpeCdHBqYu6OWxVv+NqrLw1oxi5QXLb2QQXAM0/kb5MS2VQESfjfccZJ9zFh8P Hu7OJWYzkYRtSlDfdRsAhxqxZAIOpcd9s4eKu9f4munmS7GVge6xjyIgcuWw41OUUnfO0ME 4OE2htjYil6yc7EHlJkOw== X-UI-Out-Filterresults: notjunk:1;V03:K0:L3JmoMsRwUY=:xVKZPvuvXOVIQ0hhlrhXhu QLFCNx2IanJo/XXLLShYWp5OyKyZwYmT6G8xtafVEIYvSD7ASwDB2OQg2AaBzVOCZSaos77Dp N0XtzlXUTVwl43eEbKEeTS2u8WfwCWG2NlXHWJyK/PQbFugHRy/+zVj5dJDmPOO8gYcKYHAsO iAp3ZbFsXM4wBa97/A5jx1Zn5Y3AN+mwZm/K/Ch76SQpB45O4akQ2+gHmyji0lXNMM0+SN8es T0zcB9rOwX3Mj7nfj/NMuErqxoH2znX8V+botNCFuKIT72uFCgeFD4KWeHyZLOtrixEWUwbad XjKgC7RhY0/4BTDcfdPpEewh2Ga5X4TFOZ/yhbb5cbk4unCyIZyGinS4NIQAn7hsBoynQpORF XJ2b3DPE1Pcjc48nWCE7cHueGLkyG4XXacm6+0HonH7vaozEVORwi/b2DSRTOI4nZLEar1Yu0 TbeIqJdv/1zCqlOjrZZTyk6U1AUu3/1vsMqBH9QHpt+fbXTT2MtkW1JOYOAZLkCSrZ/RdpJG8 PxBX7ihTBwRvBq3j+luvtCcHI2zgksL7E86rEijSCfevdcLhlLAvYIrb2Ukt5nUXjwvF5DlZa wrOjmVSZIiAiYUcTWy4RSxLJ/ATEKDkSE1Lv+SIBxvhQhlvjWXZLo1u1jWRjJKtiehoqoO9Ii OUtaG5OXNyPlT5lMPNbCc3nyHXeL4p6bX8YkOfLsPDTytwK3a2N8/47Eo4TJcMli9cWumjTrb 0TXeORivngDjg6g4wUA76uCMqxw7sX4h2LNvn2B0XI3cWsf1AubjXC3+cqhFLHTQ9qU+iSCH Received-SPF: none client-ip=212.227.126.187; envelope-from=felix.dietrich@sperrhaken.name; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=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:139593 Archived-At: 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)) =20=20 =20=20 (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 a= n 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 to =E2=80=98my/buffer-substring-with-properties=E2=80=99 that takes the PROPS = 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 specifying = the desired properties to copy when calling the function. Text that has the =E2=80=98invisible=E2=80=99 property may be replaced with= an ellipsis (depending on =E2=80=98buffer-invisibility-spec=E2=80=99). If you preferre= d there rather be no ellipsis, adjust the cond-form or bind =E2=80=98htmlize-ellips= is=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") --=20 Felix Dietrich