From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id CIAaK+QrnF9iZgAA0tVLHw (envelope-from ) for ; Fri, 30 Oct 2020 15:06:12 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id 0DcIJ+QrnF/2NgAA1q6Kng (envelope-from ) for ; Fri, 30 Oct 2020 15:06:12 +0000 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 157969407E9 for ; Fri, 30 Oct 2020 15:06:11 +0000 (UTC) Received: from localhost ([::1]:37880 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kYVyk-0000Za-Ry for larch@yhetil.org; Fri, 30 Oct 2020 11:06:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58462) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kYVxS-0000WW-5N for emacs-orgmode@gnu.org; Fri, 30 Oct 2020 11:04:50 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]:39435) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kYVxO-0005Ow-Tg for emacs-orgmode@gnu.org; Fri, 30 Oct 2020 11:04:49 -0400 Received: by mail-pg1-x52c.google.com with SMTP id i7so3512594pgh.6 for ; Fri, 30 Oct 2020 08:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:user-agent:from:to:subject:message-id:date:mime-version; bh=gq5SqKWZGltPoufEBju/qHkyBN8We1TyS3bTCcM+UyY=; b=mi9kXIYaRKZMRplWNW0M8VdpymWy3/f0Z58BnhqnqNX2xDkOGXQ75+OKsz5YLNyEtU 9Ka7gVgJsYgGcuZQzddppp4wkRA3Bs/ocizETDakCid8jAUO35h3ljppfBY4fxdLOBYE nhLwV9a/e7wgAgi6AqMCvC65x1+nc21jRCS/C77Cho9V1+OK02Dqk0giD7rHlJ+gJQX2 42dFHn5WHSDTBgUMbAGmChKPahaQtj7vo0yOb5348vqVkzNPKJf+8RQF49k/3l39NPD0 WGdVhEX3XOsRRKiajtjsPi8QlN3b4af5MxLgv75LDc5kZ3s5Xqn6v9stad5mJEpYt3eT tfgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:user-agent:from:to:subject:message-id :date:mime-version; bh=gq5SqKWZGltPoufEBju/qHkyBN8We1TyS3bTCcM+UyY=; b=MdjoAuleemJIxgvQOhId9zT+k0poHGfxGShEdMD1HAOQquXpf/jUCQ42R6FOPQEWmf PUu8f+z1/k/LNzL5iLAkJRw0v8XVchP39ZEA/cNQJ82u/LTPPxtY3zISPJue3Je6pWlm 5tOamuo9A/UzLH4BaxTcP7pruFTMdULiSgFZqvd3WU2v2M6X6gZ981G2qB9/tgL38OW1 DnGDpfTixiifokKrS2BIgcTMHDg4PJEBbfjk/1QWWAB+gwKk3tjyPJMkSJ6sPRBAoslf O6V2IlqPwxsX3giRNaYx9Dvg9JhZ3DbGVeS1vH68KBEP2sFaUMC+bGU1kWeAKXDghC7g 8eEg== X-Gm-Message-State: AOAM531OWy1Nj0V/ZedktsKdGMoHHgrLREyX+wBCTr+bFWgNDtxtL7ep oaiY8QKlLNrHhMG7gbpDtqPDVmhWHP8= X-Google-Smtp-Source: ABdhPJw0oh8knEASaXJ9cduoRr/G7Zag+fnFzGzy3110ToXuTCjbQgMQmjM//zLzI8/xUFauq9Hq3Q== X-Received: by 2002:a63:1924:: with SMTP id z36mr2523607pgl.354.1604070283428; Fri, 30 Oct 2020 08:04:43 -0700 (PDT) Received: from gusbrs-laptop ([199.116.118.169]) by smtp.gmail.com with ESMTPSA id v12sm5661705pgr.4.2020.10.30.08.04.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Oct 2020 08:04:42 -0700 (PDT) User-agent: mu4e 1.4.13; emacs 27.1 From: Gustavo Barros To: emacs-orgmode@gnu.org Subject: org-sort-entries sorting by top-level with first entry at bob Message-ID: <87pn4zbvqy.fsf@gmail.com> Date: Fri, 30 Oct 2020 12:04:37 -0300 MIME-Version: 1.0 Content-Type: text/plain; format=flowed Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=gusbrs.2016@gmail.com; helo=mail-pg1-x52c.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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_ENVFROM_END_DIGIT=0.25, 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: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 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" X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=fail (headers rsa verify failed) header.d=gmail.com header.s=20161025 header.b=mi9kXIYa; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: 0.09 X-TUID: mbwOK/m+usde Hi All, `org-sort-entries' provides no easy way to sort by top-level when the first entry is at the beginning of buffer. This is true for both interactive and non-interactive uses of the function, but a little more inconvenient in the latter case. Indeed, `org-sort-entries', when deciding what to sort, first tests for `org-region-active-p', then `org-at-heading-p' (or if after a heading), failing those tests, it falls back to top-level sorting. However, if the first heading is at the beginning of buffer and we want to sort by top-level, we'll never get to the fall back case, because `org-at-heading-p' will return non-nil, and the children of the first entry will be sorted instead. Not an ECM, just an use case with the situation at hand. Consider a buffer with contents: #+begin_src org ,* B Foo ,** B Baz ,** A Foo ,* A Bar #+end_src How to sort by top-level? The currently existing alternative is to `mark-whole-buffer', and let `org-sort-entries' sort by region. While this is reasonable in the interactive case, it is less so if `org-sort-entries' is being called in code. Using `mark-whole-buffer' in your code will grant you a nice compiler warning and pretending you don't use it by doing the same thing yourself is explicitly advised against in its docstring: "it is usually a mistake for a Lisp function to use any subroutine that uses or sets the mark". Behind the scenes, Org is using `use-region-p', which means the region must not only exist but transient-mark-mode must be on and the mark must be active. It can be made to work, of course, but it is clearly less than ideal. Either way, currently the only way to ensure that sorting is done by top-level when you don't know whether there is something before the first heading (including possible narrowing) is to rely on the region case. What to do with it is somewhat tricky, though. My first thought was to test if we are actually looking at a heading regexp, and sort on the heading's level in this case. But, on second thought, I believe this is not a good idea, because it will conflict with current and expected behavior for speed-keys, in particular. Perhaps test if point is at beginning of buffer, and handle this case specially? Best regards, Gustavo.