From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id INKyMih52F5kDAAA0tVLHw (envelope-from ) for ; Thu, 04 Jun 2020 04:31:36 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id 2BaDLih52F55AwAAB5/wlQ (envelope-from ) for ; Thu, 04 Jun 2020 04:31:36 +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 BEF6B94014C for ; Thu, 4 Jun 2020 04:31:35 +0000 (UTC) Received: from localhost ([::1]:57204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jghXR-0008TH-2O for larch@yhetil.org; Thu, 04 Jun 2020 00:31:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55902) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jghX0-0008So-67 for emacs-orgmode@gnu.org; Thu, 04 Jun 2020 00:31:06 -0400 Received: from pb-smtp1.pobox.com ([64.147.108.70]:57453) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jghWy-0007B0-3h for emacs-orgmode@gnu.org; Thu, 04 Jun 2020 00:31:05 -0400 Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 029326F443; Thu, 4 Jun 2020 00:31:01 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to :subject:in-reply-to:references:date:message-id:mime-version :content-type; s=sasl; bh=/87GsoGYs7xQV4wfjGOHRZDcU/0=; b=dQsYZi MnqvQG+NGtobu1qZ2glIigxECW7sqkQSMyvOZhHSnDoMxIXo2Yq9koN+Y/Fuz9ZK OfP1Q+STatheFGWiEBOSDPLBC6AvlRFUim25oya/SdD91juQv2fEmXPrzm3B9WCD C7dj5mjiexoWfndPLy8/hrecchoWPbl02t3PA= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id B59456F442; Thu, 4 Jun 2020 00:31:00 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=kyleam.com; h=from:to:subject:in-reply-to:references:date:message-id:mime-version:content-type; s=mesmtp; bh=47e3DBrzcBzrS54bCs7WgOE7lCec5keOTL3epTdPd2o=; b=KiBJlmatmYFCZk/KiSKwE7t3sVy6B7DMJFRLYvXlfqav9GvOSqrrEm8XJSvPDDweF4X5uPuxceImJBooicKOWmKMNSqu3rQnEEsQut+DGG3SaqMQRmYkgfjYcm8icQFSvTh3nI/stFsAKBCtiIgSrDlgadtlQQu78lNgl1l2p0o= Received: from localhost (unknown [45.33.91.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id 085466F441; Thu, 4 Jun 2020 00:31:00 -0400 (EDT) (envelope-from kyle@kyleam.com) From: Kyle Meyer To: Budiman Snowman , emacs-orgmode@gnu.org Subject: Re: agenda-view: match tags: Sorting by number of tags matched? In-Reply-To: References: Date: Thu, 04 Jun 2020 04:30:58 +0000 Message-ID: <87a71jwiyl.fsf@kyleam.com> MIME-Version: 1.0 Content-Type: text/plain X-Pobox-Relay-ID: 310DD51A-A61C-11EA-95FD-C28CBED8090B-24757444!pb-smtp1.pobox.com Received-SPF: pass client-ip=64.147.108.70; envelope-from=kyle@kyleam.com; helo=pb-smtp1.pobox.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/04 00:31:01 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.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, RCVD_IN_DNSWL_LOW=-0.7, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN 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: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=pobox.com header.s=sasl header.b=dQsYZi M; dkim=pass header.d=kyleam.com header.s=mesmtp header.b=KiBJlmat; dmarc=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.21 X-TUID: ukribbB7EwxX Budiman Snowman writes: > Suppose I have this document: > > * topic1 :tag1:tag2:tag3: > * topic2 :tag1:tag2:tag4: > * topic3 :tag1:tag3:tag4: > * topic4 :tag2:tag3:tag4: > * topic5 :tag1:tag2: > * topic6 :tag1:tag4: > * topic7 :tag3:tag4: > * topic8 :tag1: > > And I turn on org-agenda m, then search for tag1|tag3|tag4. Emacs will then > show the results (all entries match) in the above order. org-agenda-sorting-strategy is the main option that controls sorting in the agenda. Going through the values there, I don't see anything fitting your description. But there is a user-defined-{up,down} option that lets you implement your own logic via org-agenda-cmp-user-defined. Here's an example. It's probably brittle and inefficient in a number of ways (and maybe completely wrong), but hopefully it gives you something to start with. (add-to-list 'org-agenda-sorting-strategy '(tags user-defined-down)) (defun my/org-agenda-cmp-num-tags-matched (a b) (when (and (eq (car org-agenda-redo-command) 'org-tags-view) org-agenda-query-string (string-match-p "|" org-agenda-query-string)) (let* ((query-tags (split-string org-agenda-query-string "|")) (count (lambda (entry) (length (cl-intersection query-tags (get-text-property 0 'tags entry) :test #'equal)))) (a-nmatched (funcall count a)) (b-nmatched (funcall count b))) (cond ((> a-nmatched b-nmatched) 1) ((< a-nmatched b-nmatched) -1))))) (setq org-agenda-cmp-user-defined #'my/org-agenda-cmp-num-tags-matched) > Is there a way for Emacs to show based on the number of tags matched > ("sort based on relevance"), e.g.: > > * topic2 :tag1:tag2:tag4: > * topic1 :tag1:tag2:tag3: > * topic3 :tag1:tag3:tag4: > * topic4 :tag2:tag3:tag4: > * topic6 :tag1:tag4: > * topic5 :tag1:tag2: > * topic7 :tag3:tag4: > * topic8 :tag1: Hmm, your example query is "tag1|tag3|tag4", so a few of those lines seem off. For example, shouldn't topic 3 be at the top? Anyway, perhaps I'm misunderstanding your goal, but you should be able to adapt the above approach to what you want. Here are the results with the above custom sorting strategy and your example query: topic3 :tag1:tag3:tag4: topic1 :tag1:tag2:tag3: topic2 :tag1:tag2:tag4: topic4 :tag2:tag3:tag4: topic6 :tag1:tag4: topic7 :tag3:tag4: topic5 :tag1:tag2: topic8 :tag1: