Fixes for elpher-menu
[elpher.git] / elpher.el
index f68a247..6513ee7 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -2010,12 +2010,13 @@ functions which initialize the gopher client, namely
 ;;; Menu
 ;;
 
-(defun elpher-menu ()
-  "Show a list of all your `elpher' buffers."
-  (interactive)
+(defun elpher-menu (&optional arg)
+  "Show a list of all your `elpher' buffers.
+With an optional argument, add all the history items, too."
+  (interactive "P")
   (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
@@ -2131,7 +2132,7 @@ In Elpher Menu mode, the following commands are defined:
 \\[Buffer-menu-backup-unmark]  Back up a line and remove marks.
 \\[revert-buffer]    Update the list of buffers.
 \\[Buffer-menu-bury]    Bury the buffer listed on this line."
-  (add-hook 'tabulated-list-revert-hook 'elpher-menu-refresh))
+  (add-hook 'tabulated-list-revert-hook 'elpher-menu-refresh nil t))
 
 (defvar elpher-title nil)
 
@@ -2142,45 +2143,107 @@ 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"))
+                 '("URL" 40 t)
+                 '("Name" 30 t))
+         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))))
+         (when (memq major-mode '(elpher-mode eww-mode gemini-mode))
+           (if arg
+               (setq entries (nconc (elpher-menu-refresh-history) entries))
+             (push (elpher-menu-refresh-current) entries)))))
       (setq tabulated-list-entries (nreverse entries)))
     (tabulated-list-init-header))
 
+(defun elpher-menu-refresh-current ()
+  "Returns an item for `elpher-menu-refresh'
+based on the current buffer.
+
+An item is a list (BUFFER VECTOR) where BUFFER is the buffer this
+item refers to and VECTOR is what to display in the tabulated
+list established by `elpher-menu-refresh'. See
+`tabulated-list-format'."
+  (list (current-buffer)
+       (cond ((eq major-mode 'elpher-mode)
+              (vector "G"
+                      (or (elpher-address-to-url
+                           (elpher-page-address elpher-current-page))
+                          "none")
+                      (or (elpher-find-title)
+                          (elpher-page-display-string elpher-current-page)
+                          (buffer-name))))
+             ((eq major-mode 'gemini-mode)
+              (vector "E"
+                      (or (elpher-address-to-url
+                           (elpher-page-address elpher-current-page))
+                          "none")
+                      (or (elpher-page-display-string elpher-current-page)
+                          (buffer-name))))
+             ((eq major-mode 'eww-mode)
+              (vector "W"
+                      (or (eww-current-url)
+                          "none")
+                      (or (plist-get eww-data :title)
+                          (buffer-name)))))))
+
+(defun elpher-menu-refresh-history ()
+  "Return current entries for `elpher-menu-refresh'.
+This returns a list of items for the current buffer, based on the
+buffer's history.
+
+An item is a list (BUFFER VECTOR) where BUFFER is the buffer this
+item refers to and VECTOR is what to display in the tabulated
+list established by `elpher-menu-refresh'. See
+`tabulated-list-format'."
+  (let ((separator (list (current-buffer)
+                        (vector
+                         "-"
+                         (make-string 25 ?-)
+                         (make-string 25 ?-)))))
+    (cond ((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 "G"
+                                      (or (elpher-address-to-url
+                                           (elpher-page-address page)) "none")
+                                      (or (elpher-page-display-string page) "?")))
+                      separator))
+                  (cons elpher-current-page elpher-history)))
+         ((eq major-mode 'gemini-mode)
+          ;; no history means a list of one item
+          (list (list (current-buffer)
+                      (vector "E"
+                              (or (elpher-address-to-url
+                                   (elpher-page-address elpher-current-page)))
+                              (or (elpher-page-display-string elpher-current-page)
+                                  (buffer-name))))))
+          ((eq major-mode 'eww-mode)
+           (nconc (mapcar (lambda (data)
+                            (list (current-buffer)
+                                  (vector "W"
+                                          (or (plist-get data :url) "none")
+                                          (or (plist-get data :title) "none"))))
+                          (cons eww-data eww-history))
+                  (list separator))))))
+
 ;;; Main start procedure
 ;;