From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: avoid narrow-to-region (was: Re: replace-regexp) Date: Sun, 9 May 2021 19:27:30 +0700 Message-ID: References: <87tunf1mz5.fsf@zoho.eu> <87v97v6mxg.fsf@gnu.org> <87wnsaz9df.fsf@zoho.eu> <877dkaytwu.fsf@zoho.eu> <87eeegx25w.fsf_-_@zoho.eu> <87fsywtp3f.fsf@zoho.eu> <87bl9ktms2.fsf@zoho.eu> <875yzsrxd9.fsf@zoho.eu> 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="20741"; mail-complaints-to="usenet@ciao.gmane.io" To: Emanuel Berg , help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 09 14:28:29 2021 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 1lfiXt-0005Iq-E1 for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 09 May 2021 14:28:29 +0200 Original-Received: from localhost ([::1]:49818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lfiXs-0002pN-H8 for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 09 May 2021 08:28:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48150) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lfiXC-0002pC-01 for help-gnu-emacs@gnu.org; Sun, 09 May 2021 08:27:46 -0400 Original-Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]:45951) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lfiX9-0004VZ-By for help-gnu-emacs@gnu.org; Sun, 09 May 2021 08:27:45 -0400 Original-Received: by mail-ua1-x936.google.com with SMTP id v8so4332358uau.12 for ; Sun, 09 May 2021 05:27:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=nM5efqrYb2QONnq21YdBWhK4LhFijK72ZrykEPPHOmU=; b=D74k5RgnCq/4m9rTSByjIrth/fkINu6SOE7Ofh5laCQN7i0/6UZy9QbS1L2LETvfbk FJt0ZruyJ3ow//a7/doR3XPRcqdNUahAb5EZue8hyqLUvV6QoH67CUEPvAsyJ2Z7Z/dG qrAoziMn89gkGyDb8BU9hMklxT6cAb+5zovFwO3QwdkaYu1QRm8mPcMWkf4TPfSwS2Rx eUedx2ydpA3TDOKM9XNr0XBv1mc5AL0DI70zITjEdSMU2Sa+ezp859tLuX+gN3DQy5JJ 7rXP9tTd4ds1v0scf3n1ZChZBcdZ+H4UR7WITj8vIWSzV9XctiJZTE56MTY2awh+D11e 1Gow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=nM5efqrYb2QONnq21YdBWhK4LhFijK72ZrykEPPHOmU=; b=fcU1tpvU2I2qVuO77/70hDRmtVaPShJonTXFeD2ubdq/GaMAKd0Kfu2Ss/cjL8ZQcK jDcoQryzIKScYCcslOAmqciMStYFI8X12S0dhrsvXYaYb5NKPHNt+S6mCCZvKnelztAc fnkS3m8zbH+Oi8cEezWX9SmE2o7ipcU29bGqsJ2TzvwuXMRY7baNYvskhYzOhBslU0DA 1KT8s7ihF+2Tu7xRi5tERXJ252bB5EswZ3Z/zqPV0oshIy+M3aeJN6DIhgDGoATsomlr T634pjg2lbi9hnRJsS4Ahi6yCs89C4XLVqR0WzsYOJXVq7BTY1qEq+WT7J7d5VObaiOU hAZQ== X-Gm-Message-State: AOAM533sM0OCvW8/0V8J0plOSRNYiTLQV7lxqZSnAdj5AwkXkhNmTtnV 8kBN9lbUyZ8qo/YyHZW6TGIEC8pdnDtq7GvP5T4= X-Google-Smtp-Source: ABdhPJxH2w+i6Z2xgcpckMwJHKX2Cl4oW1Ht8+/r0d3Scg/rFi3RemmwUq+4y8uPr+MbflHz/G0hGwkQtfIN81jD8q8= X-Received: by 2002:ab0:3891:: with SMTP id z17mr14533794uav.24.1620563262430; Sun, 09 May 2021 05:27:42 -0700 (PDT) In-Reply-To: <875yzsrxd9.fsf@zoho.eu> Received-SPF: pass client-ip=2607:f8b0:4864:20::936; envelope-from=yurivkhan@gmail.com; helo=mail-ua1-x936.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.23 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:129612 Archived-At: On Sun, 9 May 2021 at 17:54, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > Proposal: Instead of quoting computer science theory, why > don't _you_ write some Elisp and show us how you think it > should be done? Mostly because I don=E2=80=99t usually write code to solve tasks I never encounter in my own workflow. (Unless specifically employed to write such code, or doing an interview with a prospective employer.) > It isn't because it is easier and more comfortable to broadly > criticize someone or something else, than to be active and > creative oneself - is it? I=E2=80=99m not criticizing you, I=E2=80=99m advising you about a possible = issue with your code. At the same time, I=E2=80=99m taking an opportunity to raise general awareness of sorting predicate requirements. Now, because you asked nicely, here=E2=80=99s my take on implementing the random shuffle algorithm. (defun yk-shuffle-lines (begin end) "Reorder lines between BEGIN and END randomly. If BEGIN or END is in the middle of a line, that line is included. Any markers inside the region may move to the beginning or end of their respective lines, and won=E2=80=99t follow the text they were associated with." (interactive "*r") (save-excursion (let ((nlines (count-lines begin end))) (goto-char begin) (while (> nlines 1) (let* ((begin1 (line-beginning-position)) (line1 (let ((end1 (line-end-position))) (prog1 (buffer-substring begin1 end1) (delete-region begin1 end1)))) (_ (forward-line (random nlines))) (line2 (let* ((begin2 (line-beginning-position)) (end2 (line-end-position))) (prog1 (buffer-substring begin2 end2) (delete-region begin2 end2))))) (insert line1) (goto-char begin1) (insert line2)) (forward-line) (setq nlines (1- nlines)))))) (The structure of let*/let/let* bindings may seem excessively complex, but I=E2=80=99m taking care here to not expose bindings beyond their validi= ty. E.g. as soon as (delete-region begin1 end1) is executed, end1 no longer refers to anything meaningful, so I drop that binding asap.)