unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Manuel Giraud <manuel@ledu-giraud.fr>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Lars Ingebrigtsen <larsi@gnus.org>,
	 Karl Fogel <kfogel@red-bean.com>,
	Drew Adams <drew.adams@oracle.com>,
	 emacs-devel <emacs-devel@gnu.org>
Subject: Re: [External] : [emacs bookmark.el] Sorting by last set
Date: Thu, 26 May 2022 22:09:22 +0200	[thread overview]
Message-ID: <87leuo12fh.fsf@elite.giraud> (raw)
In-Reply-To: <jwvsfow9qnr.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 26 May 2022 12:59:37 -0400")

[-- Attachment #1: Type: text/plain, Size: 518 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Manuel Giraud [2022-05-26 18:42:19] wrote:
>> This time I add a timestamp field to each bookmark upon setting it. I
>> also add the possibility to set `bookmark-sort-flag' to 'timestamp which
>> means sort the displayed bookmark list from most recently set to the
>> least recently set.
>
> "timestamp" doesn't say which time it's referring to.
> I think we should call it "last-modified" or something like that, so
> there's no ambiguity.

Yes, that makes sense.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-add-a-last-modified-field-when-a-bookmark-is-set.patch --]
[-- Type: text/x-patch, Size: 8596 bytes --]

From cd0e1165c4eb5a45382317f73109cc2b46fd4d54 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <manuel@ledu-giraud.fr>
Date: Thu, 26 May 2022 18:25:52 +0200
Subject: [PATCH] add a last-modified field when a bookmark is set.

add the 'last-modified option to `bookmark-sort-flag' to display
bookmark list sorted by those timestamp.
---
 etc/NEWS         |  5 +++
 lisp/bookmark.el | 92 ++++++++++++++++++++++++++++--------------------
 2 files changed, 58 insertions(+), 39 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 4ebaf6e07a..fb5fafe5bd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1673,6 +1673,11 @@ manual for more details.
 Types are registered via a 'bookmark-handler-type' symbol property on
 the jumping function.
 
++++
+*** 'bookmark-sort-flag' can now be set to 'last-modified.
+This will display bookmark list from most recently set to least
+recently set.
+
 ---
 *** New minor mode 'elide-head-mode'.
 Enabling this minor mode turns on hiding header material, like
diff --git a/lisp/bookmark.el b/lisp/bookmark.el
index c604395dd7..5460a9a297 100644
--- a/lisp/bookmark.el
+++ b/lisp/bookmark.el
@@ -115,10 +115,14 @@ bookmark-completion-ignore-case
 
 
 (defcustom bookmark-sort-flag t
-  "Non-nil means that bookmarks will be displayed sorted by bookmark name.
+  "T means that bookmarks will be displayed sorted by bookmark name.
+LAST-MODIFIED means that bookmarks will be displayed sorted from most
+recently set to last recently set.
 Otherwise they will be displayed in LIFO order (that is, most
-recently set ones come first, oldest ones come last)."
-  :type 'boolean)
+recently created ones come first, oldest ones come last)."
+  :type '(choice (const :tag "By name" t)
+                 (const :tag "By modified time" last-modified)
+                 (const :tag "By creation time" nil)))
 
 
 (defcustom bookmark-menu-confirm-deletion nil
@@ -460,6 +464,10 @@ bookmark-get-handler
   "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
   (bookmark-prop-get bookmark-name-or-record 'handler))
 
+(defun bookmark-get-last-modified (bookmark-name-or-record)
+  "Return the last-modified for BOOKMARK-NAME-OR-RECORD, or nil if none."
+  (bookmark-prop-get bookmark-name-or-record 'last-modified))
+
 (defvar bookmark-history nil
   "The history list for bookmark functions.")
 
@@ -497,6 +505,21 @@ bookmark--remove-fringe-mark
               (when (eq 'bookmark (overlay-get temp 'category))
                 (delete-overlay (setq found temp))))))))))
 
+(defun bookmark-maybe-sort-alist ()
+  "Return `bookmark-alist' for display.
+If `bookmark-sort-flag' is T, then return a sorted by name copy of the alist.
+If `bookmark-sort-flag' is LAST-MODIFIED, then return a sorted by last modified
+copy of the alist.  Otherwise, just return `bookmark-alist', which by default
+is ordered from most recently created to least recently created bookmark."
+  (let ((copy (copy-alist bookmark-alist)))
+    (cond ((eq bookmark-sort-flag t)
+           (sort copy (lambda (x y) (string-lessp (car x) (car y)))))
+          ((eq bookmark-sort-flag 'last-modified)
+           (sort copy (lambda (x y)
+                        (time-less-p (bookmark-get-last-modified y)
+                                     (bookmark-get-last-modified x)))))
+          (t copy))))
+
 (defun bookmark-completing-read (prompt &optional default)
   "Prompting with PROMPT, read a bookmark name in completion.
 PROMPT will get a \": \" stuck on the end no matter what, so you
@@ -506,10 +529,8 @@ bookmark-completing-read
   (bookmark-maybe-load-default-file) ; paranoia
   (if (listp last-nonmenu-event)
       (bookmark-menu-popup-paned-menu t prompt
-				      (if bookmark-sort-flag
-					  (sort (bookmark-all-names)
-						'string-lessp)
-					(bookmark-all-names)))
+                                      (mapcar 'bookmark-name-from-full-record
+                                              (bookmark-maybe-sort-alist)))
     (let* ((completion-ignore-case bookmark-completion-ignore-case)
            (default (unless (equal "" default) default)))
       (completing-read (format-prompt prompt default)
@@ -630,7 +651,8 @@ bookmark-make-record-default
                                    (point)
                                    (- (point) bookmark-search-size))
                                   nil))))
-    (position . ,(or posn (point)))))
+    (position . ,(or posn (point)))
+    (last-modified . ,(current-time))))
 
 \f
 ;;; File format stuff
@@ -1140,15 +1162,6 @@ bookmark-maybe-load-default-file
                                   (car bookmark-bookmarks-timestamp)))))))
          (bookmark-load (car bookmark-bookmarks-timestamp) t t))))
 
-(defun bookmark-maybe-sort-alist ()
-  "Return `bookmark-alist' for display.
-If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist.
-Otherwise, just return `bookmark-alist', which by default is ordered
-from most recently created to least recently created bookmark."
-  (if bookmark-sort-flag
-      (sort (copy-alist bookmark-alist)
-            (lambda (x y) (string-lessp (car x) (car y))))
-    bookmark-alist))
 
 
 (defvar bookmark-after-jump-hook nil
@@ -1825,27 +1838,28 @@ bookmark-bmenu--revert
               entries)))
     ;; The value of `bookmark-sort-flag' might have changed since the
     ;; last time the buffer contents were generated, so re-check it.
-    (if bookmark-sort-flag
-        (progn
-          (setq tabulated-list-sort-key '("Bookmark Name" . nil))
-          (setq tabulated-list-entries entries))
-      (setq tabulated-list-sort-key nil)
-      ;; And since we're not sorting by bookmark name, show bookmarks
-      ;; according to order of creation, with the most recently
-      ;; created bookmarks at the top and the least recently created
-      ;; at the bottom.
-      ;;
-      ;; Note that clicking the column sort toggle for the bookmark
-      ;; name column will invoke the `tabulated-list-mode' sort, which
-      ;; uses `bookmark-bmenu--name-predicate' to sort lexically by
-      ;; bookmark name instead of by (reverse) creation order.
-      ;; Clicking the toggle again will reverse the lexical sort, but
-      ;; the sort will still be lexical not creation-order.  However,
-      ;; if the user reverts the buffer, then the above check of
-      ;; `bookmark-sort-flag' will happen again and the buffer will
-      ;; go back to a creation-order sort.  This is all expected
-      ;; behavior, as documented in `bookmark-bmenu-mode'.
-      (setq tabulated-list-entries (reverse entries)))
+    (cond ((eq bookmark-sort-flag t)
+           (setq tabulated-list-sort-key '("Bookmark Name" . nil)
+                 tabulated-list-entries entries))
+          ((or (null bookmark-sort-flag)
+               (eq bookmark-sort-flag 'last-modified))
+           (setq tabulated-list-sort-key nil)
+           ;; And since we're not sorting by bookmark name, show bookmarks
+           ;; according to order of creation, with the most recently
+           ;; created bookmarks at the top and the least recently created
+           ;; at the bottom.
+           ;;
+           ;; Note that clicking the column sort toggle for the bookmark
+           ;; name column will invoke the `tabulated-list-mode' sort, which
+           ;; uses `bookmark-bmenu--name-predicate' to sort lexically by
+           ;; bookmark name instead of by (reverse) creation order.
+           ;; Clicking the toggle again will reverse the lexical sort, but
+           ;; the sort will still be lexical not creation-order.  However,
+           ;; if the user reverts the buffer, then the above check of
+           ;; `bookmark-sort-flag' will happen again and the buffer will
+           ;; go back to a creation-order sort.  This is all expected
+           ;; behavior, as documented in `bookmark-bmenu-mode'.
+           (setq tabulated-list-entries (reverse entries))))
     ;; Generate the header only after `tabulated-list-sort-key' is
     ;; settled, because if that's non-nil then the sort-direction
     ;; indicator will be shown in the named column, but if it's
@@ -1953,7 +1967,7 @@ bookmark-bmenu-mode
           ,@(if bookmark-bmenu-toggle-filenames
                 '(("File" 0 bookmark-bmenu--file-predicate)))])
   (setq tabulated-list-padding bookmark-bmenu-marks-width)
-  (when bookmark-sort-flag
+  (when (eq bookmark-sort-flag t)
     (setq tabulated-list-sort-key '("Bookmark Name" . nil)))
   (add-hook 'tabulated-list-revert-hook #'bookmark-bmenu--revert nil t)'
   (setq revert-buffer-function 'bookmark-bmenu--revert)
-- 
2.36.0


[-- Attachment #3: Type: text/plain, Size: 18 bytes --]

-- 
Manuel Giraud

  reply	other threads:[~2022-05-26 20:09 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-24 11:34 [emacs bookmark.el] Sorting by last set Manuel Giraud
2022-05-24 14:41 ` [External] : " Drew Adams
2022-05-24 15:32   ` Manuel Giraud
2022-05-24 15:46     ` Lars Ingebrigtsen
2022-05-25  2:25       ` Karl Fogel
2022-05-25  5:05         ` Drew Adams
2022-05-25  8:04           ` Manuel Giraud
2022-05-25 14:01             ` Drew Adams
2022-05-25 13:18       ` Manuel Giraud
2022-05-25 14:01         ` Drew Adams
2022-05-25 15:25           ` Manuel Giraud
2022-05-25 20:17             ` Drew Adams
2022-05-26 19:41               ` Manuel Giraud
2022-05-26  4:09             ` Karl Fogel
2022-05-26 10:58               ` Lars Ingebrigtsen
2022-05-26 16:42                 ` Manuel Giraud
2022-05-26 16:59                   ` Stefan Monnier
2022-05-26 20:09                     ` Manuel Giraud [this message]
2022-05-27 10:34                       ` Lars Ingebrigtsen
2022-05-27 13:11                         ` Manuel Giraud
2022-05-27 13:20                           ` Lars Ingebrigtsen
2022-05-27 13:39                             ` Manuel Giraud
2022-05-28 10:34                               ` Lars Ingebrigtsen
2022-05-30 14:59                                 ` Manuel Giraud
2022-05-31 18:36                                   ` Lars Ingebrigtsen
2022-06-01  6:16                                     ` Juri Linkov
2022-06-01  8:04                                       ` Manuel Giraud
2022-06-01 12:18                                         ` Lars Ingebrigtsen
2022-06-01 12:38                                           ` Manuel Giraud
2022-06-01 12:08                                       ` Stefan Monnier
2022-06-01 14:24                                       ` Drew Adams
2022-06-01 13:45                                     ` Manuel Giraud
2022-06-01 15:32                                       ` Lars Ingebrigtsen
2022-06-01 15:56                                         ` Manuel Giraud
2022-06-01 15:58                                           ` Lars Ingebrigtsen
2022-06-04  5:33                                     ` Karl Fogel
2022-05-24 16:03     ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2022-06-04  6:07 Karl Fogel
2022-06-04 14:36 ` Stefan Monnier
2022-06-04 16:25 ` Drew Adams
2022-06-05 16:16 ` Manuel Giraud
2022-06-05 17:33   ` Drew Adams
2022-06-05 20:53     ` Manuel Giraud
2022-06-05 21:12       ` Stefan Monnier
2022-06-06  0:39         ` Drew Adams
2022-06-07 15:55           ` Manuel Giraud
2022-06-08 11:51             ` Lars Ingebrigtsen
2022-06-06  0:39       ` Drew Adams
2022-06-05 17:53   ` Stefan Monnier
2022-06-07 19:49   ` Karl Fogel
2022-06-08  1:14     ` Drew Adams
2022-06-08  7:57       ` Manuel Giraud
2022-06-08 14:23         ` Drew Adams
2022-06-14 15:34         ` Manuel Giraud
2022-06-14 16:36           ` Drew Adams
2022-06-15 12:08           ` Lars Ingebrigtsen
2022-06-15 12:32             ` Manuel Giraud
2022-08-18 18:19           ` Karl Fogel
2022-08-31 11:39             ` Manuel Giraud
2022-09-01  1:45               ` Karl Fogel
2022-09-01  2:08                 ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87leuo12fh.fsf@elite.giraud \
    --to=manuel@ledu-giraud.fr \
    --cc=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    --cc=kfogel@red-bean.com \
    --cc=larsi@gnus.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).