newst-treeview.el 90 KB
Newer Older
1
;;; newst-treeview.el --- Treeview frontend for newsticker.
Ulf Jasper's avatar
Ulf Jasper committed
2

3
;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
Ulf Jasper's avatar
Ulf Jasper committed
4 5

;; Author:      Ulf Jasper <ulf.jasper@web.de>
6
;; Filename:    newst-treeview.el
Ulf Jasper's avatar
Ulf Jasper committed
7 8 9
;; URL:         http://www.nongnu.org/newsticker
;; Created:     2007
;; Keywords:    News, RSS, Atom
10
;; Package:     newsticker
Ulf Jasper's avatar
Ulf Jasper committed
11 12 13

;; ======================================================================

Glenn Morris's avatar
Glenn Morris committed
14 15
;; This file is part of GNU Emacs.

Ulf Jasper's avatar
Ulf Jasper committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;; ======================================================================
;;; Commentary:

;; See newsticker.el

;; ======================================================================
;;; History:
Glenn Morris's avatar
Glenn Morris committed
36
;;
Ulf Jasper's avatar
Ulf Jasper committed
37 38 39

;; ======================================================================
;;; Code:
Ulf Jasper's avatar
Ulf Jasper committed
40
(require 'newst-reader)
Ulf Jasper's avatar
Ulf Jasper committed
41 42 43 44 45 46 47 48 49 50 51 52
(require 'widget)
(require 'tree-widget)
(require 'wid-edit)

;; ======================================================================
;;; Customization
;; ======================================================================
(defgroup newsticker-treeview nil
  "Settings for the tree view reader."
  :group 'newsticker-reader)

(defface newsticker-treeview-face
53 54
  '((((class color) (background dark))  :foreground "white")
    (((class color) (background light)) :foreground "black"))
Ulf Jasper's avatar
Ulf Jasper committed
55 56 57 58
  "Face for newsticker tree."
  :group 'newsticker-treeview)

(defface newsticker-treeview-new-face
59
  '((t :inherit newsticker-treeview-face :weight bold))
Ulf Jasper's avatar
Ulf Jasper committed
60 61 62 63
  "Face for newsticker tree."
  :group 'newsticker-treeview)

(defface newsticker-treeview-old-face
64
  '((t :inherit newsticker-treeview-face))
Ulf Jasper's avatar
Ulf Jasper committed
65 66 67 68
  "Face for newsticker tree."
  :group 'newsticker-treeview)

(defface newsticker-treeview-immortal-face
69 70 71
  '((default :inherit newsticker-treeview-face :slant italic)
    (((class color) (background dark))  :foreground "orange")
    (((class color) (background light)) :foreground "blue"))
Ulf Jasper's avatar
Ulf Jasper committed
72 73 74 75
  "Face for newsticker tree."
  :group 'newsticker-treeview)

(defface newsticker-treeview-obsolete-face
76
  '((t :inherit newsticker-treeview-face :strike-through t))
Ulf Jasper's avatar
Ulf Jasper committed
77 78 79 80
  "Face for newsticker tree."
  :group 'newsticker-treeview)

(defface newsticker-treeview-selection-face
81 82
  '((((class color) (background dark))  :background "#bbbbff")
    (((class color) (background light)) :background "#bbbbff"))
Ulf Jasper's avatar
Ulf Jasper committed
83 84 85
  "Face for newsticker selection."
  :group 'newsticker-treeview)

86 87 88 89
(defcustom newsticker-treeview-date-format
  "%d.%m.%y, %H:%M"
  "Format for the date column in the treeview list buffer.
See `format-time-string' for a list of valid specifiers."
Stefan Monnier's avatar
Stefan Monnier committed
90
  :version "25.1"
91 92 93
  :type 'string
  :group 'newsticker-treeview)

Ulf Jasper's avatar
Ulf Jasper committed
94
(defcustom newsticker-treeview-own-frame
95
  nil
96
  "Decides whether newsticker treeview creates and uses its own frame."
Ulf Jasper's avatar
Ulf Jasper committed
97 98 99
  :type 'boolean
  :group 'newsticker-treeview)

100 101 102 103
(defcustom newsticker-treeview-treewindow-width
  30
  "Width of tree window in treeview layout.
See also `newsticker-treeview-listwindow-height'."
104
  :type 'integer
105 106 107 108 109 110
  :group 'newsticker-treeview)

(defcustom newsticker-treeview-listwindow-height
  10
  "Height of list window in treeview layout.
See also `newsticker-treeview-treewindow-width'."
111
  :type 'integer
112 113
  :group 'newsticker-treeview)

Ulf Jasper's avatar
Ulf Jasper committed
114 115 116 117 118 119 120 121 122 123 124
(defcustom newsticker-treeview-automatically-mark-displayed-items-as-old
  t
  "Decides whether to automatically mark displayed items as old.
If t an item is marked as old as soon as it is displayed.  This
applies to newsticker only."
  :type 'boolean
  :group 'newsticker-treeview)

(defvar newsticker-groups
  '("Feeds")
  "List of feed groups, used in the treeview frontend.
125 126 127 128 129 130 131 132
First element is a string giving the group name.  Remaining
elements are either strings giving a feed name or lists having
the same structure as `newsticker-groups'. (newsticker-groups :=
groupdefinition, groupdefinition := groupname groupcontent*,
groupcontent := feedname | groupdefinition)

Example: (\"Topmost group\" \"feed1\" (\"subgroup1\" \"feed 2\")
\"feed3\")")
Ulf Jasper's avatar
Ulf Jasper committed
133 134 135 136 137 138

(defcustom newsticker-groups-filename
  "~/.newsticker-groups"
  "Name of the newsticker groups settings file."
  :type 'string
  :group 'newsticker-treeview)
139
(make-obsolete-variable 'newsticker-groups-filename 'newsticker-dir "23.1")
Ulf Jasper's avatar
Ulf Jasper committed
140 141 142 143 144 145

;; ======================================================================
;;; internal variables
;; ======================================================================
(defvar newsticker--treeview-windows nil)
(defvar newsticker--treeview-buffers nil)
Ulf Jasper's avatar
Ulf Jasper committed
146 147
(defvar newsticker--treeview-current-feed nil
  "Feed name of currently shown item.")
Ulf Jasper's avatar
Ulf Jasper committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
(defvar newsticker--treeview-current-vfeed nil)
(defvar newsticker--treeview-list-show-feed nil)
(defvar newsticker--saved-window-config nil)
(defvar newsticker--selection-overlay nil
  "Highlight the selected tree node.")
(defvar newsticker--tree-selection-overlay nil
  "Highlight the selected list item.")
(defvar newsticker--frame nil "Special frame for newsticker windows.")
(defvar newsticker--treeview-list-sort-order 'sort-by-time)
(defvar newsticker--treeview-current-node-id nil)
(defvar newsticker--treeview-current-tree nil)
(defvar newsticker--treeview-feed-tree nil)
(defvar newsticker--treeview-vfeed-tree nil)

;; maps for the clickable portions
(defvar newsticker--treeview-url-keymap
  (let ((map (make-sparse-keymap 'newsticker--treeview-url-keymap)))
    (define-key map [mouse-1] 'newsticker-treeview-mouse-browse-url)
    (define-key map [mouse-2] 'newsticker-treeview-mouse-browse-url)
    (define-key map "\n" 'newsticker-treeview-browse-url)
    (define-key map "\C-m" 'newsticker-treeview-browse-url)
    (define-key map [(control return)] 'newsticker-handle-url)
    map)
  "Key map for click-able headings in the newsticker treeview buffers.")


;; ======================================================================
;;; short cuts
;; ======================================================================
(defsubst newsticker--treeview-tree-buffer ()
  "Return the tree buffer of the newsticker treeview."
  (nth 0 newsticker--treeview-buffers))
(defsubst newsticker--treeview-list-buffer ()
  "Return the list buffer of the newsticker treeview."
  (nth 1 newsticker--treeview-buffers))
(defsubst newsticker--treeview-item-buffer ()
  "Return the item buffer of the newsticker treeview."
  (nth 2 newsticker--treeview-buffers))
(defsubst newsticker--treeview-tree-window ()
  "Return the tree window of the newsticker treeview."
  (nth 0 newsticker--treeview-windows))
(defsubst newsticker--treeview-list-window ()
  "Return the list window of the newsticker treeview."
  (nth 1 newsticker--treeview-windows))
(defsubst newsticker--treeview-item-window ()
  "Return the item window of the newsticker treeview."
  (nth 2 newsticker--treeview-windows))

;; ======================================================================
;;; utility functions
;; ======================================================================
(defun newsticker--treeview-get-id (parent i)
  "Create an id for a newsticker treeview node.
PARENT is the node's parent, I is an integer."
  ;;(message "newsticker--treeview-get-id %s"
  ;;       (format "%s-%d" (widget-get parent :nt-id) i))
  (format "%s-%d" (widget-get parent :nt-id) i))

(defun newsticker--treeview-ids-eq (id1 id2)
  "Return non-nil if ids ID1 and ID2 are equal."
  ;;(message "%s/%s" (or id1 -1) (or id2 -1))
  (and id1 id2 (string= id1 id2)))

(defun newsticker--treeview-nodes-eq (node1 node2)
  "Compare treeview nodes NODE1 and NODE2 for equality.
Nodes are equal if the have the same newsticker-id.  Note that
during re-tagging and collapsing/expanding nodes change, while
their id stays constant."
  (let ((id1 (widget-get node1 :nt-id))
        (id2 (widget-get node2 :nt-id)))
    ;;(message "%s/%s %s/%s" (widget-get node1 :tag) (widget-get node2 :tag)
    ;;       (or id1 -1) (or id2 -1))
    (or (newsticker--treeview-ids-eq id1 id2)
        (string= (widget-get node1 :tag) (widget-get node2 :tag)))))

(defun newsticker--treeview-do-get-node-of-feed (feed-name startnode)
Juanma Barranquero's avatar
Juanma Barranquero committed
224
   "Recursively search node for feed FEED-NAME starting from STARTNODE."
Ulf Jasper's avatar
Ulf Jasper committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
   ;;(message "%s/%s" feed-name (widget-get startnode :nt-feed))
   (if (string= feed-name (or (widget-get startnode :nt-feed)
                              (widget-get startnode :nt-vfeed)))
       (throw 'found startnode)
     (let ((children (widget-get startnode :children)))
       (dolist (w children)
         (newsticker--treeview-do-get-node-of-feed feed-name w)))))

(defun newsticker--treeview-get-node-of-feed (feed-name)
  "Return node for feed FEED-NAME in newsticker treeview tree."
  (catch 'found
    (newsticker--treeview-do-get-node-of-feed feed-name
                                              newsticker--treeview-feed-tree)
    (newsticker--treeview-do-get-node-of-feed feed-name
                                              newsticker--treeview-vfeed-tree)))

241
(defun newsticker--treeview-do-get-node-by-id (id startnode)
Juanma Barranquero's avatar
Juanma Barranquero committed
242
   "Recursively search node with ID starting from STARTNODE."
Ulf Jasper's avatar
Ulf Jasper committed
243 244 245 246
   (if (newsticker--treeview-ids-eq id (widget-get startnode :nt-id))
       (throw 'found startnode)
     (let ((children (widget-get startnode :children)))
       (dolist (w children)
247
         (newsticker--treeview-do-get-node-by-id id w)))))
Ulf Jasper's avatar
Ulf Jasper committed
248

249
(defun newsticker--treeview-get-node-by-id (id)
Ulf Jasper's avatar
Ulf Jasper committed
250 251
  "Return node with ID in newsticker treeview tree."
  (catch 'found
252 253
    (newsticker--treeview-do-get-node-by-id id newsticker--treeview-feed-tree)
    (newsticker--treeview-do-get-node-by-id id newsticker--treeview-vfeed-tree)))
Ulf Jasper's avatar
Ulf Jasper committed
254 255 256

(defun newsticker--treeview-get-current-node ()
  "Return current node in newsticker treeview tree."
257
  (newsticker--treeview-get-node-by-id newsticker--treeview-current-node-id))
Ulf Jasper's avatar
Ulf Jasper committed
258 259 260

;; ======================================================================

261
(unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
262 263
(declare-function w3m-toggle-inline-images "ext:w3m" (&optional force no-cache))

Ulf Jasper's avatar
Ulf Jasper committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
(defun newsticker--treeview-render-text (start end)
  "Render text between markers START and END."
  (if newsticker-html-renderer
      (condition-case error-data
          (save-excursion
            (set-marker-insertion-type end t)
            ;; check whether it is necessary to call html renderer
            ;; (regexp inspired by htmlr.el)
            (goto-char start)
            (when (re-search-forward
                   "</?[A-Za-z1-6]*\\|&#?[A-Za-z0-9]+;" end t)
              ;; (message "%s" (newsticker--title item))
              (let ((w3m-fill-column (if newsticker-use-full-width
                                         -1 fill-column))
                    (w3-maximum-line-length
                     (if newsticker-use-full-width nil fill-column)))
                (save-excursion
                  (funcall newsticker-html-renderer start end)))
              ;;(cond ((eq newsticker-html-renderer 'w3m-region)
              ;;     (add-text-properties start end (list 'keymap
              ;;                                        w3m-minor-mode-map)))
              ;;((eq newsticker-html-renderer 'w3-region)
              ;;(add-text-properties start end (list 'keymap w3-mode-map))))
              (if (eq newsticker-html-renderer 'w3m-region)
                  (w3m-toggle-inline-images t))
              t))
        (error
         (message "Error: HTML rendering failed: %s, %s"
                  (car error-data) (cdr error-data))
         nil))
    nil))

;; ======================================================================
;;; List window
;; ======================================================================
(defun newsticker--treeview-list-add-item (item feed &optional show-feed)
  "Add news ITEM for FEED to newsticker treeview list window.
If string SHOW-FEED is non-nil it is shown in the item string."
  (setq newsticker--treeview-list-show-feed show-feed)
303
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317
    (let* ((inhibit-read-only t)
           pos1 pos2)
      (goto-char (point-max))
      (setq pos1 (point-marker))
      (insert " ")
      (insert (propertize " " 'display '(space :align-to 2)))
      (insert (if show-feed
                  (concat
                   (substring
                    (format "%-10s" (newsticker--real-feed-name
                                     feed))
                    0 10)
                   (propertize " " 'display '(space :align-to 12)))
                ""))
318
      (insert (format-time-string newsticker-treeview-date-format
Ulf Jasper's avatar
Ulf Jasper committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
                                  (newsticker--time item)))
      (insert (propertize " " 'display
                          (list 'space :align-to (if show-feed 28 18))))
      (setq pos2 (point-marker))
      (insert (newsticker--title item))
      (insert "\n")
      (newsticker--treeview-render-text pos2 (point-marker))
      (goto-char pos2)
      (while (search-forward "\n" nil t)
        (replace-match " "))
      (let ((map (make-sparse-keymap)))
        (define-key map [mouse-1] 'newsticker-treeview-tree-click)
        (define-key map "\n" 'newsticker-treeview-show-item)
        (define-key map "\C-m" 'newsticker-treeview-show-item)
        (add-text-properties pos1 (point-max)
                             (list :nt-item item
                                   :nt-feed feed
                                   :nt-link (newsticker--link item)
                                   'mouse-face 'highlight
                                   'keymap map
339 340
                                   'help-echo (buffer-substring pos2
                                                                (point-max)))))
Ulf Jasper's avatar
Ulf Jasper committed
341 342 343 344
      (insert "\n"))))

(defun newsticker--treeview-list-clear ()
  "Clear the newsticker treeview list window."
345
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    (let ((inhibit-read-only t))
      (erase-buffer)
      (kill-all-local-variables)
      (remove-overlays))))

(defun newsticker--treeview-list-items-with-age-callback (widget
                                                          changed-widget
                                                          &rest ages)
  "Fill newsticker treeview list window with items of certain age.
This is a callback function for the treeview nodes.
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument AGES is the list of ages that are to be shown."
  (newsticker--treeview-list-clear)
  (widget-put widget :nt-selected t)
  (apply 'newsticker--treeview-list-items-with-age ages))

(defun newsticker--treeview-list-items-with-age (&rest ages)
  "Actually fill newsticker treeview list window with items of certain age.
AGES is the list of ages that are to be shown."
  (mapc (lambda (feed)
          (let ((feed-name-symbol (intern (car feed))))
            (mapc (lambda (item)
                    (when (memq (newsticker--age item) ages)
                      (newsticker--treeview-list-add-item
                       item feed-name-symbol t)))
                  (newsticker--treeview-list-sort-items
                   (cdr (newsticker--cache-get-feed feed-name-symbol))))))
        (append newsticker-url-list-defaults newsticker-url-list))
  (newsticker--treeview-list-update nil))

(defun newsticker--treeview-list-new-items (widget changed-widget
                                                   &optional event)
  "Fill newsticker treeview list window with new items.
This is a callback function for the treeview nodes.
381 382 383
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
384 385 386 387 388 389 390 391 392 393
  (newsticker--treeview-list-items-with-age-callback widget changed-widget
                                                     'new)
  (newsticker--treeview-item-show-text
   "New items"
   "This is a virtual feed containing all new items"))

(defun newsticker--treeview-list-immortal-items (widget changed-widget
                                                        &optional event)
  "Fill newsticker treeview list window with immortal items.
This is a callback function for the treeview nodes.
394 395 396
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
397 398 399 400 401 402 403 404 405 406
  (newsticker--treeview-list-items-with-age-callback widget changed-widget
                                                     'immortal)
  (newsticker--treeview-item-show-text
   "Immortal items"
   "This is a virtual feed containing all immortal items."))

(defun newsticker--treeview-list-obsolete-items (widget changed-widget
                                                        &optional event)
  "Fill newsticker treeview list window with obsolete items.
This is a callback function for the treeview nodes.
407 408 409
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
410 411 412 413 414 415 416 417 418 419
  (newsticker--treeview-list-items-with-age-callback widget changed-widget
                                                     'obsolete)
  (newsticker--treeview-item-show-text
   "Obsolete items"
   "This is a virtual feed containing all obsolete items."))

(defun newsticker--treeview-list-all-items (widget changed-widget
                                                   &optional event)
  "Fill newsticker treeview list window with all items.
This is a callback function for the treeview nodes.
420 421 422
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  (newsticker--treeview-list-items-with-age-callback widget changed-widget
                                                     event 'new 'old
                                                     'obsolete 'immortal)
  (newsticker--treeview-item-show-text
   "All items"
   "This is a virtual feed containing all items."))

(defun newsticker--treeview-list-items-v (vfeed-name)
  "List items for virtual feed VFEED-NAME."
  (when vfeed-name
    (cond ((string-match "\\*new\\*" vfeed-name)
           (newsticker--treeview-list-items-with-age 'new))
          ((string-match "\\*immortal\\*" vfeed-name)
           (newsticker--treeview-list-items-with-age 'immortal))
          ((string-match "\\*old\\*" vfeed-name)
           (newsticker--treeview-list-items-with-age 'old nil)))
    (newsticker--treeview-list-update nil)
    ))

(defun newsticker--treeview-list-items (feed-name)
  "List items for feed FEED-NAME."
  (when feed-name
    (if (newsticker--treeview-virtual-feed-p feed-name)
        (newsticker--treeview-list-items-v feed-name)
      (mapc (lambda (item)
              (if (eq (newsticker--age item) 'feed)
                  (newsticker--treeview-item-show item (intern feed-name))
                (newsticker--treeview-list-add-item item
                                                    (intern feed-name))))
            (newsticker--treeview-list-sort-items
             (cdr (newsticker--cache-get-feed (intern feed-name)))))
      (newsticker--treeview-list-update nil))))

(defun newsticker--treeview-list-feed-items (widget changed-widget
                                                    &optional event)
  "Callback function for listing feed items.
459 460 461
Argument WIDGET is the calling treeview widget.
Argument CHANGED-WIDGET is the widget that actually has changed.
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  (newsticker--treeview-list-clear)
  (widget-put widget :nt-selected t)
  (let ((feed-name (widget-get widget :nt-feed))
        (vfeed-name (widget-get widget :nt-vfeed)))
    (if feed-name
        (newsticker--treeview-list-items feed-name)
      (newsticker--treeview-list-items-v vfeed-name))))

(defun newsticker--treeview-list-compare-item-by-age (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt age."
  (catch 'result
    (let ((age1 (newsticker--age item1))
          (age2 (newsticker--age item2)))
      (cond ((eq age1 'new)
             t)
            ((eq age1 'immortal)
             (cond ((eq age2 'new)
                    t)
                   ((eq age2 'immortal)
                    t)
                   (t
                    nil)))
            ((eq age1 'old)
             (cond ((eq age2 'new)
                    nil)
                   ((eq age2 'immortal)
                    nil)
                   ((eq age2 'old)
                    nil)
                   (t
                    t)))
            (t
             nil)))))

(defun newsticker--treeview-list-compare-item-by-age-reverse (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt age in reverse order."
  (newsticker--treeview-list-compare-item-by-age item2 item1))

(defun newsticker--treeview-list-compare-item-by-time (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt time values."
  (newsticker--cache-item-compare-by-time item1 item2))

(defun newsticker--treeview-list-compare-item-by-time-reverse (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt time values in reverse order."
  (newsticker--cache-item-compare-by-time item2 item1))

(defun newsticker--treeview-list-compare-item-by-title (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt title."
  (newsticker--cache-item-compare-by-title item1 item2))

(defun newsticker--treeview-list-compare-item-by-title-reverse (item1 item2)
  "Compare two news items ITEM1 and ITEM2 wrt title in reverse order."
  (newsticker--cache-item-compare-by-title item2 item1))

(defun newsticker--treeview-list-sort-items (items)
  "Return sorted copy of list ITEMS.
The sort function is chosen according to the value of
`newsticker--treeview-list-sort-order'."
  (let ((sort-fun
         (cond ((eq newsticker--treeview-list-sort-order 'sort-by-age)
                'newsticker--treeview-list-compare-item-by-age)
               ((eq newsticker--treeview-list-sort-order
                    'sort-by-age-reverse)
                'newsticker--treeview-list-compare-item-by-age-reverse)
               ((eq newsticker--treeview-list-sort-order 'sort-by-time)
                'newsticker--treeview-list-compare-item-by-time)
               ((eq newsticker--treeview-list-sort-order
                    'sort-by-time-reverse)
                'newsticker--treeview-list-compare-item-by-time-reverse)
               ((eq newsticker--treeview-list-sort-order 'sort-by-title)
                'newsticker--treeview-list-compare-item-by-title)
               ((eq newsticker--treeview-list-sort-order
                    'sort-by-title-reverse)
                'newsticker--treeview-list-compare-item-by-title-reverse)
               (t
                'newsticker--treeview-list-compare-item-by-title))))
    (sort (copy-sequence items) sort-fun)))

(defun newsticker--treeview-list-update-faces ()
  "Update faces in the treeview list buffer."
  (let (pos-sel)
543
    (with-current-buffer (newsticker--treeview-list-buffer)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
      (save-excursion
        (let ((inhibit-read-only t))
          (goto-char (point-min))
          (while (not (eobp))
            (let* ((pos (point-at-eol))
                   (item (get-text-property (point) :nt-item))
                   (age (newsticker--age item))
                   (selected (get-text-property (point) :nt-selected))
                   (face (cond ((eq age 'new)
                                'newsticker-treeview-new-face)
                               ((eq age 'old)
                                'newsticker-treeview-old-face)
                               ((eq age 'immortal)
                                'newsticker-treeview-immortal-face)
                               ((eq age 'obsolete)
                                'newsticker-treeview-obsolete-face)
                               (t
                                'bold))))
              (put-text-property (point) pos 'face face)
              (if selected
                  (move-overlay newsticker--selection-overlay (point)
                                (1+ pos) ;include newline
                                (current-buffer)))
              (if selected (setq pos-sel (point)))
              (forward-line 1)
              (beginning-of-line)))))) ;; FIXME!?
Ulf Jasper's avatar
Ulf Jasper committed
570
    (when pos-sel
571 572
      (if (window-live-p (newsticker--treeview-list-window))
          (set-window-point (newsticker--treeview-list-window) pos-sel)))))
Ulf Jasper's avatar
Ulf Jasper committed
573 574 575

(defun newsticker--treeview-list-clear-highlight ()
  "Clear the highlight in the treeview list buffer."
576
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
577 578 579 580 581 582 583 584
    (let ((inhibit-read-only t))
      (put-text-property (point-min) (point-max) :nt-selected nil))
    (newsticker--treeview-list-update-faces)))

(defun newsticker--treeview-list-update-highlight ()
  "Update the highlight in the treeview list buffer."
  (newsticker--treeview-list-clear-highlight)
    (let (pos num-lines)
585
      (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
586
        (let ((inhibit-read-only t))
587
          (put-text-property (point-at-bol) (point-at-eol) :nt-selected t))
Ulf Jasper's avatar
Ulf Jasper committed
588 589 590 591
        (newsticker--treeview-list-update-faces))))

(defun newsticker--treeview-list-highlight-start ()
  "Return position of selection in treeview list buffer."
592
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
593 594 595
    (save-excursion
      (goto-char (point-min))
      (next-single-property-change (point) :nt-selected))))
Ulf Jasper's avatar
Ulf Jasper committed
596 597 598 599 600

(defun newsticker--treeview-list-update (clear-buffer)
  "Update the faces and highlight in the treeview list buffer.
If CLEAR-BUFFER is non-nil the list buffer is completely erased."
  (save-excursion
601 602 603
    (if (window-live-p (newsticker--treeview-list-window))
        (set-window-buffer (newsticker--treeview-list-window)
                           (newsticker--treeview-list-buffer)))
Ulf Jasper's avatar
Ulf Jasper committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    (set-buffer (newsticker--treeview-list-buffer))
    (if clear-buffer
        (let ((inhibit-read-only t))
          (erase-buffer)))
    (newsticker-treeview-list-mode)
    (newsticker--treeview-list-update-faces)
    (goto-char (point-min))))

(defvar newsticker-treeview-list-sort-button-map
  (let ((map (make-sparse-keymap)))
    (define-key map [header-line mouse-1]
      'newsticker--treeview-list-sort-by-column)
    (define-key map [header-line mouse-2]
      'newsticker--treeview-list-sort-by-column)
    map)
  "Local keymap for newsticker treeview list window sort buttons.")

621
(defun newsticker--treeview-list-sort-by-column (&optional event)
Ulf Jasper's avatar
Ulf Jasper committed
622
  "Sort the newsticker list window buffer by the column clicked on.
623
Optional argument EVENT is the mouse event that triggered this action."
Ulf Jasper's avatar
Ulf Jasper committed
624
  (interactive (list last-input-event))
625 626
  (if event (mouse-select-window event))
  (let* ((pos (event-start event))
Ulf Jasper's avatar
Ulf Jasper committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
	 (obj (posn-object pos))
	 (sort-order (if obj
                      (get-text-property (cdr obj) 'sort-order (car obj))
                    (get-text-property (posn-point pos) 'sort-order))))
    (setq newsticker--treeview-list-sort-order
          (cond ((eq sort-order 'sort-by-age)
                 (if (eq newsticker--treeview-list-sort-order 'sort-by-age)
                     'sort-by-age-reverse
                   'sort-by-age))
                ((eq sort-order 'sort-by-time)
                 (if (eq newsticker--treeview-list-sort-order 'sort-by-time)
                     'sort-by-time-reverse
                   'sort-by-time))
                ((eq sort-order 'sort-by-title)
                 (if (eq newsticker--treeview-list-sort-order 'sort-by-title)
                     'sort-by-title-reverse
                   'sort-by-title))))
    (newsticker-treeview-update)))

(defun newsticker-treeview-list-make-sort-button (name sort-order)
  "Create propertized string for headerline button.
NAME is the button text, SORT-ORDER is the associated sort order
for the button."
  (let ((face (if (string-match (symbol-name sort-order)
                                (symbol-name
                                 newsticker--treeview-list-sort-order))
                  'bold
                'header-line)))
    (propertize name
                'sort-order sort-order
                'help-echo (concat "Sort by " name)
                'mouse-face 'highlight
                'face face
                'keymap newsticker-treeview-list-sort-button-map)))

Ulf Jasper's avatar
Ulf Jasper committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
(defun newsticker--treeview-list-select (item)
  "Select ITEM in treeview's list buffer."
  (newsticker--treeview-list-clear-highlight)
    (let (pos num-lines)
      (save-current-buffer
        (set-buffer (newsticker--treeview-list-buffer))
        (goto-char (point-min))
        (catch 'found
          (while t
            (let ((it (get-text-property (point) :nt-item)))
              (when (eq it item)
                (newsticker--treeview-list-update-highlight)
                (newsticker--treeview-list-update-faces)
                (newsticker--treeview-item-show
                 item (get-text-property (point) :nt-feed))
                (throw 'found t)))
            (forward-line 1)
            (when (eobp)
              (goto-char (point-min))
              (throw 'found nil)))))))

Ulf Jasper's avatar
Ulf Jasper committed
683 684 685 686 687
;; ======================================================================
;;; item window
;; ======================================================================
(defun newsticker--treeview-item-show-text (title description)
  "Show text in treeview item buffer consisting of TITLE and DESCRIPTION."
688
  (with-current-buffer (newsticker--treeview-item-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
689 690 691 692 693 694 695 696 697 698 699
    (when (fboundp 'w3m-process-stop)
      (w3m-process-stop (current-buffer)))
    (let ((inhibit-read-only t))
      (erase-buffer)
      (kill-all-local-variables)
      (remove-overlays)
      (insert title)
      (put-text-property (point-min) (point) 'face 'newsticker-feed-face)
      (insert "\n\n" description)
      (when newsticker-justification
        (fill-region (point-min) (point-max) newsticker-justification))
Ulf Jasper's avatar
Ulf Jasper committed
700
      (newsticker-treeview-item-mode)
Ulf Jasper's avatar
Ulf Jasper committed
701 702
      (goto-char (point-min)))))

Ulf Jasper's avatar
Ulf Jasper committed
703 704 705
(defun newsticker--treeview-item-show (item feed-name-symbol)
  "Show news ITEM coming from FEED-NAME-SYMBOL in treeview item buffer."
  (setq newsticker--treeview-current-feed (symbol-name feed-name-symbol))
706
  (with-current-buffer (newsticker--treeview-item-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
707 708 709 710 711 712 713 714 715 716 717
    (when (fboundp 'w3m-process-stop)
      (w3m-process-stop (current-buffer)))
    (let ((inhibit-read-only t)
          (is-rendered-HTML nil)
          pos
          (marker1 (make-marker))
          (marker2 (make-marker)))
      (erase-buffer)
      (kill-all-local-variables)
      (remove-overlays)

Ulf Jasper's avatar
Ulf Jasper committed
718
      (when (and item feed-name-symbol)
Ulf Jasper's avatar
Ulf Jasper committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732
        (let ((wwidth (1- (window-width (newsticker--treeview-item-window)))))
          (if newsticker-use-full-width
              (set (make-local-variable 'fill-column) wwidth))
          (set (make-local-variable 'fill-column) (min fill-column
                                                       wwidth)))
        (let ((desc (newsticker--desc item)))
          (insert "\n" (or desc "[No Description]")))
        (set-marker marker1 (1+ (point-min)))
        (set-marker marker2 (point-max))
        (setq is-rendered-HTML (newsticker--treeview-render-text marker1
                                                                 marker2))
        (when (and newsticker-justification
                   (not is-rendered-HTML))
          (fill-region marker1 marker2 newsticker-justification))
Glenn Morris's avatar
Glenn Morris committed
733

Ulf Jasper's avatar
Ulf Jasper committed
734
        (newsticker-treeview-item-mode)
Ulf Jasper's avatar
Ulf Jasper committed
735 736 737
        (goto-char (point-min))
        ;; insert logo at top
        (let* ((newsticker-enable-logo-manipulations nil)
Ulf Jasper's avatar
Ulf Jasper committed
738
               (img (newsticker--image-read feed-name-symbol nil)))
Ulf Jasper's avatar
Ulf Jasper committed
739 740
          (if (and (display-images-p) img)
              (newsticker--insert-image img (car item))
Ulf Jasper's avatar
Ulf Jasper committed
741
            (insert (newsticker--real-feed-name feed-name-symbol))))
Ulf Jasper's avatar
Ulf Jasper committed
742 743 744 745 746 747 748
        (add-text-properties (point-min) (point)
                             (list 'face 'newsticker-feed-face
                                   'mouse-face 'highlight
                                   'help-echo "Visit in web browser."
                                   :nt-link (newsticker--link item)
                                   'keymap newsticker--treeview-url-keymap))
        (setq pos (point))
Glenn Morris's avatar
Glenn Morris committed
749

Ulf Jasper's avatar
Ulf Jasper committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
        (insert "\n\n")
        ;; insert title
        (setq pos (point))
        (insert (newsticker--title item) "\n")
        (set-marker marker1 pos)
        (set-marker marker2 (point))
        (newsticker--treeview-render-text marker1 marker2)
        (put-text-property pos (point) 'face 'newsticker-treeview-new-face)
        (goto-char marker2)
        (delete-char -1)
        (insert "\n")
        (put-text-property marker2 (point) 'face 'newsticker-treeview-face)
        (set-marker marker2 (point))
        (when newsticker-justification
          (fill-region marker1 marker2 newsticker-justification))
        (goto-char marker2)
        (add-text-properties marker1 (1- (point))
                             (list 'mouse-face 'highlight
                                   'help-echo "Visit in web browser."
                                   :nt-link (newsticker--link item)
                                   'keymap newsticker--treeview-url-keymap))
        (insert (format-time-string newsticker-date-format
                                    (newsticker--time item)))
        (insert "\n")
        (setq pos (point))
        (insert "\n")
        ;; insert enclosures and rest at bottom
        (goto-char (point-max))
        (insert "\n\n")
        (setq pos (point))
        (newsticker--insert-enclosure item newsticker--treeview-url-keymap)
        (put-text-property pos (point) 'face 'newsticker-enclosure-face)
        (setq pos (point))
        (insert "\n")
        (newsticker--print-extra-elements item newsticker--treeview-url-keymap)
        (put-text-property pos (point) 'face 'newsticker-extra-face)
        (goto-char (point-min)))))
  (if (and newsticker-treeview-automatically-mark-displayed-items-as-old
           item
           (memq (newsticker--age item) '(new obsolete)))
      (let ((newsticker-treeview-automatically-mark-displayed-items-as-old nil))
        (newsticker-treeview-mark-item-old t)
        (newsticker--treeview-list-update-faces)))
793 794
  (if (window-live-p (newsticker--treeview-item-window))
      (set-window-point (newsticker--treeview-item-window) 1)))
Ulf Jasper's avatar
Ulf Jasper committed
795 796 797 798

(defun newsticker--treeview-item-update ()
  "Update the treeview item buffer and window."
  (save-excursion
799 800 801
    (if (window-live-p (newsticker--treeview-item-window))
        (set-window-buffer (newsticker--treeview-item-window)
                           (newsticker--treeview-item-buffer)))
Ulf Jasper's avatar
Ulf Jasper committed
802 803 804
    (set-buffer (newsticker--treeview-item-buffer))
    (let ((inhibit-read-only t))
      (erase-buffer))
Ulf Jasper's avatar
Ulf Jasper committed
805
    (newsticker-treeview-item-mode)))
Ulf Jasper's avatar
Ulf Jasper committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

;; ======================================================================
;;; Tree window
;; ======================================================================
(defun newsticker--treeview-tree-expand (tree)
  "Expand TREE.
Callback function for tree widget that adds nodes for feeds and subgroups."
  (tree-widget-set-theme "folder")
  (let ((group (widget-get tree :nt-group))
        (i 0)
        (nt-id ""))
    (mapcar (lambda (g)
              (setq nt-id (newsticker--treeview-get-id tree i))
              (setq i (1+ i))
              (if (listp g)
                  (let* ((g-name (car g)))
                    `(tree-widget
                      :tag ,(newsticker--treeview-tree-get-tag g-name nil nt-id)
                      :expander newsticker--treeview-tree-expand
                      :expander-p (lambda (&rest ignore) t)
                      :nt-group ,(cdr g)
                      :nt-feed ,g-name
                      :nt-id ,nt-id
                      :keep (:nt-feed :num-new :nt-id :open);;  :nt-group
                      :open nil))
                (let ((tag (newsticker--treeview-tree-get-tag g nil nt-id)))
                `(item :tag ,tag
                       :leaf-icon newsticker--tree-widget-leaf-icon
                       :nt-feed ,g
                       :action newsticker--treeview-list-feed-items
                       :nt-id ,nt-id
                       :keep (:nt-id)
                       :open t))))
            group)))

(defun newsticker--treeview-tree-expand-status (tree &optional changed-widget
                                                     event)
  "Expand the vfeed TREE.
Optional arguments CHANGED-WIDGET and EVENT are ignored."
  (tree-widget-set-theme "folder")
  (list `(item :tag ,(newsticker--treeview-tree-get-tag nil "new")
               :nt-vfeed "new"
               :action newsticker--treeview-list-new-items
               :nt-id ,(newsticker--treeview-get-id tree 0)
               :keep (:nt-id))
        `(item :tag ,(newsticker--treeview-tree-get-tag nil "immortal")
               :nt-vfeed "immortal"
               :action newsticker--treeview-list-immortal-items
               :nt-id ,(newsticker--treeview-get-id tree 1)
               :keep (:nt-id))
        `(item :tag  ,(newsticker--treeview-tree-get-tag nil "obsolete")
               :nt-vfeed "obsolete"
               :action newsticker--treeview-list-obsolete-items
               :nt-id ,(newsticker--treeview-get-id tree 2)
               :keep (:nt-id))
        `(item :tag ,(newsticker--treeview-tree-get-tag nil "all")
               :nt-vfeed "all"
               :action newsticker--treeview-list-all-items
               :nt-id ,(newsticker--treeview-get-id tree 3)
               :keep (:nt-id))))

(defun newsticker--treeview-virtual-feed-p (feed-name)
  "Return non-nil if FEED-NAME is a virtual feed."
  (string-match "\\*.*\\*" feed-name))

(define-widget 'newsticker--tree-widget-leaf-icon 'tree-widget-icon
  "Icon for a tree-widget leaf node."
  :tag        "O"
  :glyph-name "leaf"
  :button-face 'default)

(defun newsticker--treeview-tree-update ()
  "Update treeview tree buffer and window."
  (save-excursion
880 881 882
    (if (window-live-p (newsticker--treeview-tree-window))
        (set-window-buffer (newsticker--treeview-tree-window)
                           (newsticker--treeview-tree-buffer)))
Ulf Jasper's avatar
Ulf Jasper committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    (set-buffer (newsticker--treeview-tree-buffer))
    (kill-all-local-variables)
    (let ((inhibit-read-only t))
      (erase-buffer)
      (tree-widget-set-theme "folder")
      (setq newsticker--treeview-feed-tree
            (widget-create 'tree-widget
                           :tag (newsticker--treeview-propertize-tag
                                 "Feeds" 0 "feeds")
                           :expander 'newsticker--treeview-tree-expand
                           :expander-p (lambda (&rest ignore) t)
                           :leaf-icon 'newsticker--tree-widget-leaf-icon
                           :nt-group (cdr newsticker-groups)
                           :nt-id "feeds"
                           :keep '(:nt-id)
                           :open t))
      (setq newsticker--treeview-vfeed-tree
            (widget-create 'tree-widget
                           :tag (newsticker--treeview-propertize-tag
                                 "Virtual Feeds" 0 "vfeeds")
                           :expander 'newsticker--treeview-tree-expand-status
                           :expander-p (lambda (&rest ignore) t)
                           :leaf-icon 'newsticker--tree-widget-leaf-icon
                           :nt-id "vfeeds"
                           :keep '(:nt-id)
                           :open t))
      (use-local-map widget-keymap)
      (widget-setup))
    (newsticker-treeview-mode)))

(defun newsticker--treeview-propertize-tag (tag &optional num-new nt-id feed
                                                vfeed)
  "Return propertized copy of string TAG.
Optional argument NUM-NEW is used for choosing face, other
arguments NT-ID, FEED, and VFEED are added as properties."
  ;;(message "newsticker--treeview-propertize-tag '%s' %s" feed nt-id)
  (let ((face 'newsticker-treeview-face)
        (map (make-sparse-keymap)))
    (if (and num-new (> num-new 0))
        (setq face 'newsticker-treeview-new-face))
    (define-key map [mouse-1] 'newsticker-treeview-tree-click)
    (define-key map "\n" 'newsticker-treeview-tree-do-click)
    (define-key map "\C-m" 'newsticker-treeview-tree-do-click)
    (propertize tag 'face face 'keymap map
                :nt-id nt-id
                :nt-feed feed
                :nt-vfeed vfeed
930
                'help-echo tag
Ulf Jasper's avatar
Ulf Jasper committed
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                'mouse-face 'highlight)))

(defun newsticker--treeview-tree-get-tag (feed-name vfeed-name
                                                    &optional nt-id)
  "Return a tag string for either FEED-NAME or, if it is nil, for VFEED-NAME.
Optional argument NT-ID is added to the tag's properties."
  (let (tag (num-new 0))
    (cond (vfeed-name
           (cond ((string= vfeed-name "new")
                  (setq num-new (newsticker--stat-num-items-total 'new))
                  (setq tag (format "New items (%d)" num-new)))
                 ((string= vfeed-name "immortal")
                  (setq num-new (newsticker--stat-num-items-total 'immortal))
                  (setq tag (format "Immortal items (%d)" num-new)))
                 ((string= vfeed-name "obsolete")
                  (setq num-new (newsticker--stat-num-items-total 'obsolete))
                  (setq tag (format "Obsolete items (%d)" num-new)))
                 ((string= vfeed-name "all")
                  (setq num-new (newsticker--stat-num-items-total))
                  (setq tag (format "All items (%d)" num-new)))))
          (feed-name
           (setq num-new (newsticker--stat-num-items-for-group
                          (intern feed-name) 'new 'immortal))
           (setq tag
                 (format "%s (%d)"
                         (newsticker--real-feed-name (intern feed-name))
                         num-new))))
    (if tag
        (newsticker--treeview-propertize-tag tag num-new
                                             nt-id
                                             feed-name vfeed-name))))

(defun newsticker--stat-num-items-for-group (feed-name-symbol &rest ages)
  "Count number of items in feed FEED-NAME-SYMBOL that have an age matching AGES."
  ;;(message "newsticker--stat-num-items-for-group %s %s" feed-name-symbol ages)
  (let ((result (apply 'newsticker--stat-num-items feed-name-symbol ages)))
    (mapc (lambda (f-n)
            (setq result (+ result
                            (apply 'newsticker--stat-num-items (intern f-n)
                                   ages))))
          (newsticker--group-get-feeds
           (newsticker--group-get-group (symbol-name feed-name-symbol)) t))
    result))

(defun newsticker--treeview-count-node-items (feed &optional isvirtual)
  "Count number of relevant items for a treeview node.
FEED gives the name of the feed or group.  If ISVIRTUAL is non-nil
the feed is a virtual feed."
  (let* ((num-new 0))
    (if feed
        (if isvirtual
            (cond ((string= feed "new")
                   (setq num-new (newsticker--stat-num-items-total 'new)))
                  ((string= feed "immortal")
                   (setq num-new (newsticker--stat-num-items-total 'immortal)))
                  ((string= feed "obsolete")
                   (setq num-new (newsticker--stat-num-items-total 'obsolete)))
                  ((string= feed "all")
                   (setq num-new (newsticker--stat-num-items-total))))
          (setq num-new (newsticker--stat-num-items-for-group
                         (intern feed) 'new 'immortal))))
    num-new))

(defun newsticker--treeview-tree-update-tag (w &optional recursive
                                               &rest ignore)
  "Update tag for tree widget W.
If RECURSIVE is non-nil recursively update parent widgets as
well.  Argument IGNORE is ignored.  Note that this function, if
called recursively, makes w invalid.  You should keep w's nt-id in
that case."
  (let* ((parent (widget-get w :parent))
         (feed (or (widget-get w :nt-feed) (widget-get parent :nt-feed)))
         (vfeed (or (widget-get w :nt-vfeed) (widget-get parent :nt-vfeed)))
         (nt-id (or (widget-get w :nt-id) (widget-get parent :nt-id)))
         (num-new (newsticker--treeview-count-node-items (or feed vfeed)
                                                         vfeed))
         (tag (newsticker--treeview-tree-get-tag feed vfeed nt-id))
         (n (widget-get w :node)))
    (if parent
        (if recursive
            (newsticker--treeview-tree-update-tag parent)))
    (when tag
      (when n
        (widget-put n :tag tag))
      (widget-put w :num-new num-new)
      (widget-put w :tag tag)
      (when (marker-position (widget-get w :from))
        (let ((p (point))
              (notify (widget-get w :notify)))
          ;; FIXME: This moves point!!!!
1021
          (with-current-buffer (newsticker--treeview-tree-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1022 1023
            (widget-value-set w (widget-value w)))
          (goto-char p))))))
1024

Ulf Jasper's avatar
Ulf Jasper committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
(defun newsticker--treeview-tree-do-update-tags (widget)
  "Actually recursively update tags for WIDGET."
  (save-excursion
    (let ((children (widget-get widget :children)))
      (dolist (w children)
        (newsticker--treeview-tree-do-update-tags w))
      (newsticker--treeview-tree-update-tag widget))))

(defun newsticker--treeview-tree-update-tags (&rest ignore)
  "Update all tags of all trees.
Arguments IGNORE are ignored."
  (save-current-buffer
    (set-buffer (newsticker--treeview-tree-buffer))
    (let ((inhibit-read-only t))
      (newsticker--treeview-tree-do-update-tags
       newsticker--treeview-feed-tree)
      (newsticker--treeview-tree-do-update-tags
       newsticker--treeview-vfeed-tree))
      (tree-widget-set-theme "folder")))

(defun newsticker--treeview-tree-update-highlight ()
  "Update highlight in tree buffer."
  (let ((pos (widget-get (newsticker--treeview-get-current-node) :from)))
    (unless (or (integerp pos) (and (markerp pos) (marker-position pos)))
      (setq pos (widget-get (widget-get
                             (newsticker--treeview-get-current-node)
                                        :parent) :from)))
    (when (or (integerp pos) (and (markerp pos) (marker-position pos)))
1053
      (with-current-buffer (newsticker--treeview-tree-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1054 1055
        (goto-char pos)
        (move-overlay newsticker--tree-selection-overlay
1056
                      (point-at-bol) (1+ (point-at-eol))
Ulf Jasper's avatar
Ulf Jasper committed
1057
                      (current-buffer)))
1058 1059
      (if (window-live-p (newsticker--treeview-tree-window))
          (set-window-point (newsticker--treeview-tree-window) pos)))))
Ulf Jasper's avatar
Ulf Jasper committed
1060 1061 1062 1063 1064 1065 1066

;; ======================================================================
;;; Toolbar
;; ======================================================================
(defvar newsticker-treeview-tool-bar-map
  (if (featurep 'xemacs)
      nil
1067 1068
    (if (boundp 'tool-bar-map)
        (let ((tool-bar-map (make-sparse-keymap)))
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
          (tool-bar-add-item "newsticker/prev-feed"
                             'newsticker-treeview-prev-feed
                             'newsticker-treeview-prev-feed
                             :help "Go to previous feed"
                             ;;:enable '(newsticker-previous-feed-available-p) FIXME
                             )
          (tool-bar-add-item "newsticker/prev-item"
                             'newsticker-treeview-prev-item
                             'newsticker-treeview-prev-item
                             :help "Go to previous item"
                             ;;:enable '(newsticker-previous-item-available-p) FIXME
                             )
          (tool-bar-add-item "newsticker/next-item"
                             'newsticker-treeview-next-item
                             'newsticker-treeview-next-item
                             :visible t
                             :help "Go to next item"
                             ;;:enable '(newsticker-next-item-available-p) FIXME
                             )
          (tool-bar-add-item "newsticker/next-feed"
                             'newsticker-treeview-next-feed
                             'newsticker-treeview-next-feed
                             :help "Go to next feed"
                             ;;:enable '(newsticker-next-feed-available-p) FIXME
                             )
          (tool-bar-add-item "newsticker/mark-immortal"
                             'newsticker-treeview-toggle-item-immortal
                             'newsticker-treeview-toggle-item-immortal
                             :help "Toggle current item as immortal"
                             ;;:enable '(newsticker-item-not-immortal-p) FIXME
                             )
          (tool-bar-add-item "newsticker/mark-read"
                             'newsticker-treeview-mark-item-old
                             'newsticker-treeview-mark-item-old
                             :help "Mark current item as read"
                             ;;:enable '(newsticker-item-not-old-p) FIXME
                             )
          (tool-bar-add-item "newsticker/get-all"
                             'newsticker-get-all-news
                             'newsticker-get-all-news
                             :help "Get news for all feeds")
          (tool-bar-add-item "newsticker/update"
                             'newsticker-treeview-update
                             'newsticker-treeview-update
                             :help "Update newsticker buffer")
          (tool-bar-add-item "newsticker/browse-url"
                             'newsticker-browse-url
                             'newsticker-browse-url
                             :help "Browse URL for item at point")
          ;; standard icons / actions
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
          (define-key tool-bar-map [newsticker-sep-1]
            (list 'menu-item "--double-line"))
          (tool-bar-add-item "close"
                             'newsticker-treeview-quit
                             'newsticker-treeview-quit
                             :help "Close newsticker")
          (tool-bar-add-item "preferences"
                             'newsticker-customize
                             'newsticker-customize
                             :help "Customize newsticker")
          tool-bar-map))))
Ulf Jasper's avatar
Ulf Jasper committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149

;; ======================================================================
;;; actions
;; ======================================================================

(defun newsticker-treeview-mouse-browse-url (event)
  "Call `browse-url' for the link of the item at which the EVENT occurred."
  (interactive "e")
  (save-excursion
    (switch-to-buffer (window-buffer (posn-window (event-end event))))
    (let ((url (get-text-property (posn-point (event-end event))
                                  :nt-link)))
      (when url
        (browse-url url)
        (if newsticker-automatically-mark-visited-items-as-old
            (newsticker-treeview-mark-item-old))))))

(defun newsticker-treeview-browse-url ()
  "Call `browse-url' for the link of the item at point."
  (interactive)
1150
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    (let ((url (get-text-property (point) :nt-link)))
      (when url
        (browse-url url)
        (if newsticker-automatically-mark-visited-items-as-old
            (newsticker-treeview-mark-item-old))))))

(defun newsticker--treeview-buffer-init ()
  "Initialize all treeview buffers."
  (setq newsticker--treeview-buffers nil)
  (add-to-list 'newsticker--treeview-buffers
               (get-buffer-create "*Newsticker Tree*") t)
  (add-to-list 'newsticker--treeview-buffers
               (get-buffer-create "*Newsticker List*") t)
  (add-to-list 'newsticker--treeview-buffers
               (get-buffer-create "*Newsticker Item*") t)

  (unless newsticker--selection-overlay
1168
    (with-current-buffer (newsticker--treeview-list-buffer)
1169
      (setq buffer-undo-list t)
Ulf Jasper's avatar
Ulf Jasper committed
1170 1171 1172 1173 1174
      (setq newsticker--selection-overlay (make-overlay (point-min)
                                                        (point-max)))
      (overlay-put newsticker--selection-overlay 'face
                   'newsticker-treeview-selection-face)))
  (unless newsticker--tree-selection-overlay
1175
    (with-current-buffer (newsticker--treeview-tree-buffer)
1176
      (setq buffer-undo-list t)
Ulf Jasper's avatar
Ulf Jasper committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
      (setq newsticker--tree-selection-overlay (make-overlay (point-min)
                                                             (point-max)))
      (overlay-put newsticker--tree-selection-overlay 'face
                   'newsticker-treeview-selection-face)))

  (newsticker--treeview-tree-update)
  (newsticker--treeview-list-update t)
  (newsticker--treeview-item-update))

(defun newsticker-treeview-update ()
1187 1188
  "Update all treeview buffers and windows.
Note: does not update the layout."
Ulf Jasper's avatar
Ulf Jasper committed
1189
  (interactive)
Ulf Jasper's avatar
Ulf Jasper committed
1190
  (let ((cur-item (newsticker--treeview-get-selected-item)))
1191 1192
    (if (newsticker--group-manage-orphan-feeds)
      (newsticker--treeview-tree-update))
Ulf Jasper's avatar
Ulf Jasper committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    (newsticker--treeview-list-update t)
    (newsticker--treeview-item-update)
    (newsticker--treeview-tree-update-tags)
    (cond (newsticker--treeview-current-feed
           (newsticker--treeview-list-items newsticker--treeview-current-feed))
          (newsticker--treeview-current-vfeed
           (newsticker--treeview-list-items-with-age
            (intern newsticker--treeview-current-vfeed))))
    (newsticker--treeview-tree-update-highlight)
    (newsticker--treeview-list-update-highlight)
    (let ((cur-feed (or newsticker--treeview-current-feed
                        newsticker--treeview-current-vfeed)))
      (if (and cur-feed cur-item)
          (newsticker--treeview-list-select cur-item)))))
Ulf Jasper's avatar
Ulf Jasper committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

(defun newsticker-treeview-quit ()
  "Quit newsticker treeview."
  (interactive)
  (setq newsticker--sentinel-callback nil)
  (bury-buffer "*Newsticker Tree*")
  (bury-buffer "*Newsticker List*")
  (bury-buffer "*Newsticker Item*")
  (set-window-configuration newsticker--saved-window-config)
  (when newsticker--frame
    (if (frame-live-p newsticker--frame)
      (delete-frame newsticker--frame))
1219 1220
    (setq newsticker--frame nil))
  (newsticker-treeview-save))
Ulf Jasper's avatar
Ulf Jasper committed
1221 1222

(defun newsticker-treeview-save ()
1223
  "Save treeview group settings."
Ulf Jasper's avatar
Ulf Jasper committed
1224
  (interactive)
1225 1226 1227 1228
  (let ((coding-system-for-write 'utf-8)
        (buf (find-file-noselect (concat newsticker-dir "/groups"))))
    (when buf
      (with-current-buffer buf
Ulf Jasper's avatar
Ulf Jasper committed
1229 1230 1231 1232
        (setq buffer-undo-list t)
        (erase-buffer)
        (insert ";; -*- coding: utf-8 -*-\n")
        (insert (prin1-to-string newsticker-groups))
1233 1234
        (save-buffer)
        (kill-buffer)))))
Ulf Jasper's avatar
Ulf Jasper committed
1235 1236 1237 1238

(defun newsticker--treeview-load ()
  "Load treeview settings."
  (let* ((coding-system-for-read 'utf-8)
Ulf Jasper's avatar
Ulf Jasper committed
1239 1240 1241 1242 1243 1244 1245
         (filename
          (or (and (file-exists-p newsticker-groups-filename)
                   (y-or-n-p
                    (format "Old newsticker groups (%s) file exists.  Read it? "
                            newsticker-groups-filename))
                   newsticker-groups-filename)
              (concat newsticker-dir "/groups")))
1246 1247
         (buf (and (file-exists-p filename)
                   (find-file-noselect filename))))
1248 1249 1250
    (and (file-exists-p newsticker-groups-filename)
	 (y-or-n-p (format "Delete old newsticker groups file? "))
	 (delete-file newsticker-groups-filename))
Ulf Jasper's avatar
Ulf Jasper committed
1251 1252 1253 1254 1255 1256 1257
    (when buf
      (set-buffer buf)
      (goto-char (point-min))
      (condition-case nil
          (setq newsticker-groups (read buf))
        (error
         (message "Error while reading newsticker groups file!")
1258 1259
         (setq newsticker-groups nil)))
      (kill-buffer buf))))
Ulf Jasper's avatar
Ulf Jasper committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271


(defun newsticker-treeview-scroll-item ()
  "Scroll current item."
  (interactive)
  (save-selected-window
    (select-window (newsticker--treeview-item-window) t)
    (scroll-up 1)))

(defun newsticker-treeview-show-item ()
  "Show current item."
  (interactive)
1272
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1273
  (newsticker--treeview-list-update-highlight)
1274
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    (beginning-of-line)
    (let ((item (get-text-property (point) :nt-item))
          (feed (get-text-property (point) :nt-feed)))
      (newsticker--treeview-item-show item feed)))
  (newsticker--treeview-tree-update-tag
   (newsticker--treeview-get-current-node) t)
  (newsticker--treeview-tree-update-highlight))

(defun newsticker-treeview-next-item ()
  "Move to next item."
  (interactive)
1286
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
  (save-current-buffer
    (set-buffer (newsticker--treeview-list-buffer))
    (if (newsticker--treeview-list-highlight-start)
        (forward-line 1))
    (if (eobp)
        (forward-line -1)))
  (newsticker-treeview-show-item))

(defun newsticker-treeview-prev-item ()
  "Move to previous item."
  (interactive)
1298
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1299 1300 1301 1302 1303
  (save-current-buffer
    (set-buffer (newsticker--treeview-list-buffer))
    (forward-line -1))
  (newsticker-treeview-show-item))

Ulf Jasper's avatar
Ulf Jasper committed
1304
(defun newsticker-treeview-next-new-or-immortal-item (&optional
1305 1306
                                                      current-item-counts
                                                      dont-wrap-trees)
Ulf Jasper's avatar
Ulf Jasper committed
1307 1308 1309
  "Move to next new or immortal item.
Will move to next feed until an item is found.  Will not move if
optional argument CURRENT-ITEM-COUNTS is t and current item is
Ulf Jasper's avatar
Ulf Jasper committed
1310 1311
new or immortal.  Will not move from virtual to ordinary feed
tree or vice versa if optional argument DONT-WRAP-TREES is non-nil."
Ulf Jasper's avatar
Ulf Jasper committed
1312
  (interactive)
1313
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1314
  (newsticker--treeview-list-clear-highlight)
Ulf Jasper's avatar
Ulf Jasper committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
  (unless (catch 'found
            (let ((move (not current-item-counts)))
              (while t
                (save-current-buffer
                  (set-buffer (newsticker--treeview-list-buffer))
                  (when move (forward-line 1)
                        (when (eobp)
                          (forward-line -1)
                          (throw 'found nil))))
                (when (memq (newsticker--age
                             (newsticker--treeview-get-selected-item))
                            '(new immortal))
                  (newsticker-treeview-show-item)
                  (throw 'found t))
                (setq move t))))
1330 1331 1332 1333
    (let ((wrap-trees (not dont-wrap-trees)))
      (when (or (newsticker-treeview-next-feed t)
                (and wrap-trees (newsticker--treeview-first-feed)))
        (newsticker-treeview-next-new-or-immortal-item t t)))))
Ulf Jasper's avatar
Ulf Jasper committed
1334 1335

(defun newsticker-treeview-prev-new-or-immortal-item ()
Ulf Jasper's avatar
Ulf Jasper committed
1336 1337
  "Move to previous new or immortal item.
Will move to previous feed until an item is found."
Ulf Jasper's avatar
Ulf Jasper committed
1338
  (interactive)
1339
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1340
  (newsticker--treeview-list-clear-highlight)
Ulf Jasper's avatar
Ulf Jasper committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
  (unless (catch 'found
            (while t
              (save-current-buffer
                (set-buffer (newsticker--treeview-list-buffer))
                (when (bobp)
                  (throw 'found nil))
                (forward-line -1))
              (when (memq (newsticker--age
                           (newsticker--treeview-get-selected-item))
                          '(new immortal))
                (newsticker-treeview-show-item)
                (throw 'found t))
                (when (bobp)
                  (throw 'found nil))))
    (when (newsticker-treeview-prev-feed t)
      (set-buffer (newsticker--treeview-list-buffer))
      (goto-char (point-max))
      (newsticker-treeview-prev-new-or-immortal-item))))
Ulf Jasper's avatar
Ulf Jasper committed
1359 1360 1361

(defun newsticker--treeview-get-selected-item ()
  "Return item that is currently selected in list buffer."
1362
  (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
    (beginning-of-line)
    (get-text-property (point) :nt-item)))

(defun newsticker-treeview-mark-item-old (&optional dont-proceed)
  "Mark current item as old unless it is obsolete.
Move to next item unless DONT-PROCEED is non-nil."
  (interactive)
  (let ((item (newsticker--treeview-get-selected-item)))
    (unless (eq (newsticker--age item) 'obsolete)
      (newsticker--treeview-mark-item item 'old)))
  (unless dont-proceed
    (newsticker-treeview-next-item)))

(defun newsticker-treeview-toggle-item-immortal ()
  "Toggle immortality of current item."
  (interactive)
  (let* ((item (newsticker--treeview-get-selected-item))
         (new-age (if (eq (newsticker--age item) 'immortal)
                      'old
                    'immortal)))
    (newsticker--treeview-mark-item item new-age)
    (newsticker-treeview-next-item)))

(defun newsticker--treeview-mark-item (item new-age)
  "Mark ITEM with NEW-AGE."
  (when item
    (setcar (nthcdr 4 item) new-age)
    ;; clean up ticker FIXME
1391
    )
Ulf Jasper's avatar
Ulf Jasper committed
1392 1393 1394
  (newsticker--cache-save-feed
   (newsticker--cache-get-feed (intern newsticker--treeview-current-feed)))
  (newsticker--treeview-tree-do-update-tags newsticker--treeview-vfeed-tree))
Ulf Jasper's avatar
Ulf Jasper committed
1395 1396 1397 1398 1399 1400

(defun newsticker-treeview-mark-list-items-old ()
  "Mark all listed items as old."
  (interactive)
  (let ((current-feed (or newsticker--treeview-current-feed
                          newsticker--treeview-current-vfeed)))
1401
    (with-current-buffer (newsticker--treeview-list-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
      (goto-char (point-min))
      (while (not (eobp))
        (let ((item (get-text-property (point) :nt-item)))
          (unless (memq (newsticker--age item) '(immortal obsolete))
            (newsticker--treeview-mark-item item 'old)))
        (forward-line 1)))
    (newsticker--treeview-tree-update-tags)
    (if current-feed
        (newsticker-treeview-jump current-feed))))

(defun newsticker-treeview-save-item ()
  "Save current item."
  (interactive)
  (newsticker-save-item (or newsticker--treeview-current-feed
                            newsticker--treeview-current-vfeed)
                        (newsticker--treeview-get-selected-item)))

1419 1420 1421 1422 1423 1424 1425
(defun newsticker-treeview-browse-url-item ()
  "Convert current item to HTML and call `browse-url' on result."
  (interactive)
  (newsticker-browse-url-item (or newsticker--treeview-current-feed
                                  newsticker--treeview-current-vfeed)
                              (newsticker--treeview-get-selected-item)))

Ulf Jasper's avatar
Ulf Jasper committed
1426 1427
(defun newsticker--treeview-set-current-node (node)
  "Make NODE the current node."
1428
  (with-current-buffer (newsticker--treeview-tree-buffer)
Ulf Jasper's avatar
Ulf Jasper committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
    (setq newsticker--treeview-current-node-id
          (widget-get node :nt-id))
    (setq newsticker--treeview-current-feed (widget-get node :nt-feed))
    (setq newsticker--treeview-current-vfeed (widget-get node :nt-vfeed))
    (newsticker--treeview-tree-update-highlight)))

(defun newsticker--treeview-get-first-child (node)
  "Get first child of NODE."
  (let ((children (widget-get node :children)))
    (if children
        (car children)
      nil)))

(defun newsticker--treeview-get-second-child (node)
  "Get scond child of NODE."
  (let ((children (widget-get node :children)))
    (if children
        (car (cdr children))
      nil)))

(defun newsticker--treeview-get-last-child (node)
  "Get last child of NODE."
  ;;(message "newsticker--treeview-get-last-child %s" (widget-get node :tag))
  (let ((children (widget-get node :children)))
    (if children
        (car (reverse children))
      nil)))

(defun newsticker--treeview-get-feed-vfeed (node)
  "Get (virtual) feed of NODE."
  (or (widget-get node :nt-feed) (widget-get node :nt-vfeed)))

(defun newsticker--treeview-get-next-sibling (node)
  "Get next sibling of NODE."
  (let ((parent (widget-get node :parent)))
    (catch 'found
      (let ((children (widget-get parent :children)))
        (while children
          (if (newsticker--treeview-nodes-eq (car children) node)
              (throw 'found (car (cdr children))))
          (setq children (cdr children)))))))

(defun newsticker--treeview-get-prev-sibling (node)
  "Get previous sibling of NODE."
  (let ((parent (widget-get node :parent)))
    (catch 'found
      (let ((children (widget-get parent :children))
            (prev nil))
        (while children
          (if (and (newsticker--treeview-nodes-eq (car children) node)
                   (widget-get prev :nt-id))
              (throw 'found prev))
          (setq prev (car children))
          (setq children (cdr children)))))))

(defun newsticker--treeview-get-next-uncle (node)
  "Get next uncle of NODE, i.e. parent's next sibling."
  (let* ((parent (widget-get node :parent))
         (grand-parent (widget-get parent :parent)))
    (catch 'found
      (let ((uncles (widget-get grand-parent :children)))
        (while uncles
          (if (newsticker--treeview-nodes-eq (car uncles) parent)
              (throw 'found (car (cdr uncles))))
          (setq uncles (cdr uncles)))))))

(defun newsticker--treeview-get-prev-uncle (node)
  "Get previous uncle of NODE, i.e. parent's previous sibling."
  (let* ((parent (widget-get node :parent))
         (grand-parent (widget-get parent :parent)))
    (catch 'found
      (let ((uncles (widget-get grand-parent :children))
            (prev nil))
        (while uncles
          (if (newsticker--treeview-nodes-eq (car uncles) parent)
              (throw 'found prev))
          (setq prev (car uncles))
          (setq uncles (cdr uncles)))))))

(defun newsticker--treeview-get-other-tree ()
  "Get other tree."
  (if (and (newsticker--treeview-get-current-node)
           (widget-get (newsticker--treeview-get-current-node) :nt-feed))
      newsticker--treeview-vfeed-tree
    newsticker--treeview-feed-tree))

(defun newsticker--treeview-activate-node (node &optional backward)
  "Activate NODE.
If NODE is a tree widget the node's first subnode is activated.
If BACKWARD is non-nil the last subnode of the previous sibling
is activated."
  (newsticker--treeview-set-current-node node)
  (save-current-buffer
    (set-buffer (newsticker--treeview-tree-buffer))
    (cond ((eq (widget-type node) 'tree-widget)
           (unless (widget-get node :open)
             (widget-put node :open nil)
             (widget-apply-action node))
           (newsticker--treeview-activate-node
            (if backward
                (newsticker--treeview-get-last-child node)
              (newsticker--treeview-get-second-child node))))
          (node
           (widget-apply-action node)))))

1534
(defun newsticker--treeview-first-feed ()
Ulf Jasper's avatar
Ulf Jasper committed
1535
  "Jump to the depth-first feed in the `newsticker-groups' tree."
1536 1537 1538
  (newsticker-treeview-jump
   (car (reverse (newsticker--group-get-feeds newsticker-groups t)))))

Ulf Jasper's avatar
Ulf Jasper committed
1539 1540 1541 1542 1543
(defun newsticker-treeview-next-feed (&optional stay-in-tree)
  "Move to next feed.
Optional argument STAY-IN-TREE prevents moving from real feed
tree to virtual feed tree or vice versa.
Return t if a new feed was activated, nil otherwise."
Ulf Jasper's avatar
Ulf Jasper committed
1544
  (interactive)
1545
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1546 1547
  (let ((cur (newsticker--treeview-get-current-node))
        (new nil))
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    (setq new
          (if cur
              (or (newsticker--treeview-get-next-sibling cur)
                  (newsticker--treeview-get-next-uncle cur)
                  (and (not stay-in-tree)
                       (newsticker--treeview-get-other-tree)))
            (car (widget-get newsticker--treeview-feed-tree :children))))
    (if new
        (progn
          (newsticker--treeview-activate-node new)
          (newsticker--treeview-tree-update-highlight)
          (not (eq new cur)))
Ulf Jasper's avatar
Ulf Jasper committed
1560
      nil)))
Ulf Jasper's avatar
Ulf Jasper committed
1561

Ulf Jasper's avatar
Ulf Jasper committed
1562 1563 1564 1565 1566
(defun newsticker-treeview-prev-feed (&optional stay-in-tree)
  "Move to previous feed.
Optional argument STAY-IN-TREE prevents moving from real feed
tree to virtual feed tree or vice versa.
Return t if a new feed was activated, nil otherwise."
Ulf Jasper's avatar
Ulf Jasper committed
1567
  (interactive)
1568
  (newsticker--treeview-restore-layout)
Ulf Jasper's avatar
Ulf Jasper committed
1569 1570
  (let ((cur (newsticker--treeview-get-current-node))
        (new nil))
Ulf Jasper's avatar
Ulf Jasper committed
1571
    (if cur
Ulf Jasper's avatar
Ulf Jasper committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
      (progn
        (setq new
              (if cur
                  (or (newsticker--treeview-get-prev-sibling cur)
                      (newsticker--treeview-get-prev-uncle cur)
                      (and (not stay-in-tree)
                           (newsticker--treeview-get-other-tree)))
                (car (widget-get newsticker--treeview-feed-tree :children))))
        (if new
            (progn
              (newsticker--treeview-activate-node new t)
              (newsticker--treeview-tree-update-highlight)
              (not (eq new cur)))
          nil))
      nil)))
1587

Ulf Jasper's avatar
Ulf Jasper committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
(defun newsticker-treeview-next-page ()