Experimental: C-u M-x elpher-menu with history
authorAlex Schroeder <alex@gnu.org>
Fri, 3 Jul 2020 16:20:32 +0000 (18:20 +0200)
committerAlex Schroeder <alex@gnu.org>
Fri, 3 Jul 2020 16:20:32 +0000 (18:20 +0200)
When called with a prefix argument, the elpher-menu now displays
history items as well. That is, instead of showing each buffer in
Elpher or Eww mode, it shows those buffers, and their history (based
on elpher-history and eww-history).

Sadly, there are no buffers for these, therefore operating on them
does nothing meaningful: visiting them gets you the current
buffer (the last one of every history branch).

Ideally, we'd rework all the buffer commands so that the know what to
do with history entries: visiting the current buffer and hitting
"back" a few times, for example. Or cloning the current buffer and
doing that.

Then again, perhaps we should limit ourself to a much smaller set of
commands for elpher-menu-mode-map (and the doc string of
elpher-menu-mode).

elpher.el

index 4a141b7..943b569 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -2005,12 +2005,13 @@ functions which initialize the gopher client, namely
 ;;; Menu
 ;;
 
-(defun elpher-menu ()
-  "Show a list of all your `elpher' buffers."
+(defun elpher-menu (&optional arg)
+  "Show a list of all your `elpher' buffers.
+With an optional argument, add all the history items, too."
   (interactive)
   (switch-to-buffer (get-buffer-create "*Elpher Menu*"))
   (elpher-menu-mode)
-  (elpher-menu-refresh)
+  (elpher-menu-refresh arg)
   (tabulated-list-print))
 
 (defvar elpher-menu-mode-map
@@ -2137,45 +2138,86 @@ In Elpher Menu mode, the following commands are defined:
     (let ((start (text-property-any
                  (point-min) (point-max)
                  'face 'elpher-gemini-heading1)))
-      (if start
-         (save-excursion
-           (goto-char start)
-           (setq-local elpher-title
-                       (buffer-substring-no-properties
-                        start (line-end-position))))
-       "none"))))
-
-(defun elpher-menu-refresh ()
-  "Refresh the list of buffers."
+      (when start
+       (save-excursion
+         (goto-char start)
+         (setq-local elpher-title
+                     (buffer-substring-no-properties
+                      start (line-end-position))))))))
+
+(defun elpher-menu-refresh (&optional arg)
+  "Refresh the list of buffers.
+With an optional argument, add all the history items, too."
     ;; Set up `tabulated-list-format'.
     (setq tabulated-list-format
          (vector '("T" 1 t)
                  '("URL" 35 t)
                  '("Name" 35 t))
-         tabulated-list-sort-key '("Name"))
+         tabulated-list-sort-key nil)
     ;; Collect info for each buffer we're interested in.
     (let (entries)
       (dolist (buf (buffer-list))
        (with-current-buffer buf
          (when (memq major-mode '(elpher-mode eww-mode))
-           (push (list buf
-                       (vector
-                        (cond ((eq major-mode 'elpher-mode) "E")
-                              ((eq major-mode 'eww-mode) "W"))
-                        (cond ((eq major-mode 'elpher-mode)
-                               (or (elpher-address-to-url
-                                    (elpher-page-address elpher-current-page))
-                                   "none"))
-                              ((eq major-mode 'eww-mode)
-                               (eww-current-url)))
-                        (cond ((eq major-mode 'elpher-mode)
-                               (elpher-find-title))
-                              ((eq major-mode 'eww-mode)
-                               (plist-get eww-data :title)))))
-                 entries))))
+           (setq entries
+                 (nconc (if arg
+                            (elpher-menu-refresh-current)
+                          (elpher-menu-refresh-history))
+                        entries)))))
       (setq tabulated-list-entries (nreverse entries)))
     (tabulated-list-init-header))
 
+(defun elpher-menu-refresh-current ()
+  "Return current entries for `elpher-menu-refresh'.
+If we're only interested in the current entries, then this
+function can only return a list of a single item per buffer."
+  (list buf
+       (vector
+        (cond ((eq major-mode 'elpher-mode) "E")
+              ((eq major-mode 'eww-mode) "W"))
+        (cond ((eq major-mode 'elpher-mode)
+               (or (elpher-address-to-url
+                    (elpher-page-address elpher-current-page))
+                   "none"))
+              ((eq major-mode 'eww-mode)
+               (eww-current-url)))
+        (cond ((eq major-mode 'elpher-mode)
+               (or (elpher-find-title)
+                   (elpher-page-display-string elpher-current-page)))
+              ((eq major-mode 'eww-mode)
+               (plist-get eww-data :title))))))
+
+(defun elpher-menu-refresh-history ()
+  "Return current entries for `elpher-menu-refresh'.
+If we're only interested in the current entries, then this
+function can only return a list of a single item per buffer."
+  (let ((separator (list (current-buffer)
+                        (vector
+                         "E"
+                         (make-string 25 ?-)
+                         (make-string 25 ?-)))))
+    (if (eq major-mode 'elpher-mode)
+       ;; every section starts with the current page and ends with
+       ;; the separator
+       (mapcar (lambda (page)
+                 (if page
+                     (list (current-buffer)
+                           (vector
+                            "E"
+                            (or (elpher-address-to-url
+                                 (elpher-page-address page)) "none")
+                            (or (elpher-page-display-string page) "?")))
+                   separator))
+               (cons elpher-current-page elpher-history))
+      (nconc (mapcar (lambda (data)
+                      (list (current-buffer)
+                            (vector
+                             "W"
+                             (plist-get data :url)
+                             (plist-get data :title))))
+                    (cons eww-data eww-history))
+            (list separator)))))
+
 ;;; Main start procedure
 ;;