From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Francesco =?UTF-8?Q?Potort=C3=AC?= Newsgroups: gmane.emacs.bugs Subject: bug#44597: 26.3; bibtex should allow reverse sorting Date: Sun, 06 Dec 2020 17:16:42 +0100 Message-ID: References: <87ima7yjp7.fsf@gnus.org> <87sg99vviz.fsf@gnus.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19247"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 44597@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 06 17:18:29 2020 Return-path: Envelope-to: geb-bug-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 1klwk0-0004uL-St for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 06 Dec 2020 17:18:28 +0100 Original-Received: from localhost ([::1]:34662 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1klwjz-0004P3-Th for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 06 Dec 2020 11:18:27 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48652) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1klwie-0004OH-NR for bug-gnu-emacs@gnu.org; Sun, 06 Dec 2020 11:17:06 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:39456) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1klwic-0005oQ-W0 for bug-gnu-emacs@gnu.org; Sun, 06 Dec 2020 11:17:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1klwic-0003xA-Rp for bug-gnu-emacs@gnu.org; Sun, 06 Dec 2020 11:17:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Francesco =?UTF-8?Q?Potort=C3=AC?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 06 Dec 2020 16:17:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44597 X-GNU-PR-Package: emacs Original-Received: via spool by 44597-submit@debbugs.gnu.org id=B44597.160727140715143 (code B ref 44597); Sun, 06 Dec 2020 16:17:02 +0000 Original-Received: (at 44597) by debbugs.gnu.org; 6 Dec 2020 16:16:47 +0000 Original-Received: from localhost ([127.0.0.1]:50995 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1klwiN-0003wB-0B for submit@debbugs.gnu.org; Sun, 06 Dec 2020 11:16:47 -0500 Original-Received: from smtp-clients2.isti.cnr.it ([146.48.28.37]:45044) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1klwiK-0003vx-Lx for 44597@debbugs.gnu.org; Sun, 06 Dec 2020 11:16:45 -0500 Original-Received: from tucano.isti.cnr.it (tucano.isti.cnr.it [146.48.81.102]) (Authenticated sender: pot) by smtp-clients2.isti.cnr.it (Postfix) with ESMTPSA id F10CBB0838; Sun, 6 Dec 2020 17:16:42 +0100 (CET) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.4 at smtp-out.isti.cnr.it Original-Received: from pot by tucano.isti.cnr.it with local (Exim 4.94) (envelope-from ) id 1klwiI-004kvG-Le; Sun, 06 Dec 2020 17:16:42 +0100 In-Reply-To: <87sg99vviz.fsf@gnus.org> (larsi@gnus.org) X-fingerprint: 4B02 6187 5C03 D6B1 2E31 7666 09DF 2DC9 BE21 6115 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:195133 Archived-At: >> Ideally, bibtex.el should provide a hook where one can install any >> function for sorting. The hook should get some arguments (the type of >> entry, the name of entry, the date, a bibtex custom field and maybe >> others). That would make bibtex.el more generally flexible. > >Yes, I think introducing a variable to control the sorting would be a >good idea. It'd default to `bibtex-lessp' (or rather, something that >does the same), but (as you say) have an input that makes implementing >other sorting functions easier. > >Patches welcome. :-) This is a patch that improves things, in my opinion. This is not the ideal situation I mentioned above, but "il meglio รจ il nemico del bene" (best is enemy of good). So here you are, this one satisfies my needs and is hoefully a step towards a more general solution. diff -pub /home/pot/elisp/bibtex-trunk.el /home/pot/elisp/bibtex.el --- /home/pot/elisp/bibtex-trunk.el 2020-12-03 18:52:11.000000000 +0100 +++ /home/pot/elisp/bibtex.el 2020-12-06 17:02:06.000000000 +0100 @@ -202,7 +202,10 @@ narrowed to just the entry." (defcustom bibtex-maintain-sorted-entries nil "If non-nil, BibTeX mode maintains all entries in sorted order. Allowed non-nil values are: -plain or t All entries are sorted alphabetically. +plain or t Entries are sorted alphabetically. +reverse Entries are sorted in reverse alphabetic order. +date Entries are sorted by date. +reversedate Entries are reverse sorted by date, starting with newest. crossref All entries are sorted alphabetically unless an entry has a crossref field. These crossrefed entries are placed in alphabetical order immediately preceding the main entry. @@ -213,10 +216,13 @@ See also `bibtex-sort-ignore-string-entr :group 'bibtex :type '(choice (const nil) (const plain) + (const reverse) + (const date) + (const reversedate) (const crossref) (const entry-class) (const t)) - :safe (lambda (a) (memq a '(nil t plain crossref entry-class)))) + :safe (lambda (a) (memq a '(nil t plain reverse date reversedate crossref entry-class)))) (defcustom bibtex-sort-entry-class '(("String") @@ -3993,23 +3999,25 @@ If mark is active count entries in regio (defun bibtex-entry-index () "Return index of BibTeX entry head at or past position of point. -The index is a list (KEY CROSSREF-KEY ENTRY-TYPE) that is used for sorting -the entries of the BibTeX buffer. CROSSREF-KEY is nil unless the value -of `bibtex-maintain-sorted-entries' is `crossref'. Move point to the end -of the head of the entry found. Return nil if no entry found." +The index is a list (KEY CROSSREF-KEY ENTRY-TYPE DATE) that is +used for sorting the entries of the BibTeX buffer. CROSSREF-KEY +is nil unless the value of `bibtex-maintain-sorted-entries' is +`crossref'. Move point to the end of the head of the entry +found. Return nil if no entry found." (let ((case-fold-search t)) (if (re-search-forward bibtex-entry-maybe-empty-head nil t) (let ((key (bibtex-key-in-head)) ;; all entry types should be downcase (for ease of comparison) - (entry-type (downcase (bibtex-type-in-head)))) + (crossref-key nil) + (entry-type (downcase (bibtex-type-in-head))) + (date (bibtex-get-date))) ;; Don't search CROSSREF-KEY if we don't need it. - (if (eq bibtex-maintain-sorted-entries 'crossref) + (when (eq bibtex-maintain-sorted-entries 'crossref) (let ((bounds (bibtex-search-forward-field "\\(OPT\\)?crossref" t))) - (list key - (if bounds (bibtex-text-in-field-bounds bounds t)) - entry-type)) - (list key nil entry-type)))))) + (when bounds + (setq crossref-key (bibtex-text-in-field-bounds bounds t))))) + (list key crossref-key entry-type date))))) (defun bibtex-init-sort-entry-class-alist () "Initialize `bibtex-sort-entry-class-alist' (buffer-local)." @@ -4024,6 +4032,28 @@ of the head of the entry found. Return alist))) alist)))) +(defun bibtex-get-date (&optional latest) + "Return date of current entry as a string in yyyy-mm-dd format. +If any part of date is missing, use the earliest possible missing +value, unless LATEST is not nil, in which case use the latest possible +missing value." + (let ((date (bibtex-autokey-get-field "date"))) + (if (string> date "") + date + (let ((year (bibtex-autokey-get-field "year")) + (month (bibtex-autokey-get-field + "month" '(("^jan.*". "01") ("^feb.*". "02") ("^mar.*". "03") + ("^apr.*". "04") ("^may.*". "05") ("^jun.*". "06") + ("^jul.*". "07") ("^aug.*". "08") ("^sep.*". "09") + ("^oct.*". "10") ("^nov.*". "11") ("^dec.*". "12")))) + (day (bibtex-autokey-get-field "days" '(("-.*" . ""))))) + (when (string= year "") (setq year (if latest "9999" "1900"))) + (when (string= month "") (setq month (if latest "12" "01"))) + (when (string= day "") (setq day (if latest "31" "01"))) + (concat year "-" month "-" (if (= (string-width day) 1) + (concat "0" day) + (substring day 0 2))))))) + (defun bibtex-lessp (index1 index2) "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2. Each index is a list (KEY CROSSREF-KEY ENTRY-TYPE). @@ -4056,6 +4086,12 @@ If its value is nil use plain sorting." (or (< n1 n2) (and (= n1 n2) (string-lessp (car index1) (car index2)))))) + ((eq bibtex-maintain-sorted-entries 'reversedate) + (string-lessp (nth 3 index2) (nth 3 index1))) + ((eq bibtex-maintain-sorted-entries 'date) + (string-lessp (nth 3 index1) (nth 3 index2))) + ((eq bibtex-maintain-sorted-entries 'reverse) + (string-lessp (car index2) (car index1))) (t ; (eq bibtex-maintain-sorted-entries 'plain) (string-lessp (car index1) (car index2)))))