emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/greader b8269b6b13 6/6: greader-queue-mode implemented.


From: ELPA Syncer
Subject: [elpa] externals/greader b8269b6b13 6/6: greader-queue-mode implemented.
Date: Wed, 27 Sep 2023 03:58:15 -0400 (EDT)

branch: externals/greader
commit b8269b6b1358162c25df95e1f8839d64ca3a9376
Author: Michelangelo Rodriguez <michelangelo.rodriguez@gmail.com>
Commit: Michelangelo Rodriguez <michelangelo.rodriguez@gmail.com>

    greader-queue-mode implemented.
---
 greader.el | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 155 insertions(+), 12 deletions(-)

diff --git a/greader.el b/greader.el
index 2694cdb602..e7f5d5f2fb 100644
--- a/greader.el
+++ b/greader.el
@@ -1230,12 +1230,15 @@ So you can use this command like a player, if you press 
<left> you
 ;; greader-queue-mode
 ;; This minor mode enables an alternative reading mode, which
 ;; works with blocks of text instead of the buffer in a linear fashion.
-(defvar greader-queue nil
+
+(defvar-local greader-queue nil
   "This is the variable that contains the queue.
 each element is formed by a cons, whose form is 'start-position
 . end-position'.")
-(defvar greader-queue-current-element nil
-"Represent the current item in the queue (as index).")
+
+(defvar-local greader-queue-current-element nil
+  "Represent the current item in the queue (as index).")
+
 ;; Questa funzione aggiunge un elemento alla coda.
 (defun greader-queue-add-element (&optional start end)
   "Add an item to the queue.
@@ -1244,16 +1247,63 @@ If the region is not active either, then it throws an 
error."
   (interactive)
   (unless (and start end)
     (if (region-active-p)
-       (setq start (region-beginning) end (region-end))
+       (progn
+         (setq start (region-beginning) end (region-end))
+         (deactivate-mark))
       (user-error "Nothing to add")))
-  (add-to-list 'greader-queue (cons start end) t)
+  (let ((result (greader-queue-region-overlap start end)))
+    (if result
+       (greader-queue-modify-element result start end)
+      (add-to-list 'greader-queue (cons start end) t)))
   (unless greader-queue-current-element
     (setq greader-queue-current-element 0)))
 
+;; This function determines whether the region overlaps with one of the
+;; items in the queue.
+(defun greader-queue-region-overlap (&optional start end)
+  "Return the index of the element that overlaps the region.
+If the region does not overlap any of the items in the queue,
+then return nil."
+  (unless (and start end)
+    (if (region-active-p)
+       (setq start (region-beginning) end (region-end))
+      (user-error "Please specify a region")))
+  (if greader-queue
+      (let ((result nil) (counter 0))
+       (dolist (item greader-queue)
+         (when (or
+                (and (>= start (car item)) (<= start (cdr item)))
+                (and (>= end (car item)) (<= end (cdr item))))
+           (setq result counter))
+         (setq counter (1+ counter)))
+       result)
+    nil))
+
+;; This function modifies an item in the queue.
+(defun greader-queue-modify-element (index start end)
+  "Modify the item specified in INDEX with the new START and END.
+This function modifies the `greader-queue' variable."
+  (let ((result nil) (counter 0))
+    (dolist (item greader-queue)
+      (if (equal counter index)
+         (progn
+           (push (cons start end) result)
+           (setq counter (1+ counter)))
+       (push item result)
+       (setq counter (1+ counter))))
+    (setq greader-queue (reverse result))))
+
+;;this function resets the queue.
+(defun greader-queue-reset-queue ()
+  "Reset the queue and relative variables."
+  (interactive)
+  (setq greader-queue nil)
+  (setq greader-queue-current-element nil))
+
 ;; This function removes an item from the queue.
 (defun greader-queue-remove-element (&optional index)
   "Remove the item in INDEX.
-Se INDEX è nil, usa la variabile `greader-queue-current-element'."
+If INDEX is nil, use the `greader-queue-current-element' variable."
   (interactive)
   (unless index
     (setq index greader-queue-current-element))
@@ -1264,15 +1314,19 @@ Se INDEX è nil, usa la variabile 
`greader-queue-current-element'."
 ;; This function gets the text linked to an element of the
 ;; queue.
 (defun greader-queue-get-element (&optional index)
-  "return the text associated with a queue item.
+  "return the text associated with a queue item or nil if INDEX is beyond
+the limit.
 If INDEX is nil, use `greader-queue-current-element'."
   (unless index
     (setq index greader-queue-current-element))
-  (when (>= index (length greader-queue))
-    (user-error "Index out of range"))
-  (buffer-substring (car (elt greader-queue index)) (cdr (elt
-                                                         greader-queue
-                                                         index))))
+  (when (equal (length greader-queue) 0)
+    (user-error "Queue is emty"))
+  (let ((result nil))
+    (when (< greader-queue-current-element (length greader-queue))
+      (setq result (buffer-substring (car (elt greader-queue index)) (cdr (elt
+                                                                          
greader-queue
+                                                                          
index)))))
+    result))
 
 ;; This function returns the next item in the queue. If
 ;; the current element is the last one, then it returns nil.
@@ -1298,5 +1352,94 @@ If the current element is the first, it returns it."
       (setq greader-queue-current-element (1-
                                           greader-queue-current-element)))
   (greader-queue-get-element))
+
+;; This function will act as a sentinel for the tts when invoked
+;; by the `greader-queue-read' function.
+(defun greader-queue-next-action (&optional process string)
+  "Sentinel process for greader-queue-mode."
+  (setq greader-queue-current-element (1+
+                                      greader-queue-current-element))
+  (greader-queue-read))
+
+;; This function is a reimplementation in salsa queue's
+;; reader-read. The idea is to map it right in place of
+;; greader-read in the `greader-queue-mode' keymap.
+(defun greader-queue-read ()
+  "Entry point for `greader-queue-mode'."
+  (interactive)
+  (let ((text (greader-queue-get-element)))
+    (if text
+       (progn
+         (setq-local greader-backend-action
+                     #'greader-queue-next-action)
+         (greader-read-asynchronous text))
+      (setq-local greader-backend-action #'greader--default-action)
+      (greader-read-asynchronous "end of queue."))))
+
+;; This function stops reading of queue.
+(defun greader-queue-stop ()
+  "stop reading of queue."
+  (interactive)
+  (setq greader-backend-action #'greader--default-action)
+  (greader-tts-stop))
+;; This function moves the point to the beginning of the previous element
+;; in the queue and start reading.
+(defun greader-queue-backward ()
+  "Move the point to the beginning of the item in the queue and start the
+reading.
+If we are already at the first element, it makes a ding and does
+nothing else."
+  (interactive)
+  (if (equal greader-queue-current-element 0)
+      (ding)
+    (greader-tts-stop)
+    (setq greader-queue-current-element (1-
+                                        greader-queue-current-element))
+    (greader-queue-read)))
+
+;; This function moves the point to the next item in the queue and
+;; reads it.
+(defun greader-queue-forward ()
+  "Move the point to the next item in the queue and read it.
+If it is already the last item in the queue it beeps and does nothing
+else."
+  (if (equal greader-queue-current-element (- 1 (length
+                                                greader-queue)))
+      (ding)
+    (greader-tts-stop)
+    (setq greader-queue-current-element (1+
+                                        greader-queue-current-element))
+    (greader-queue-read)))
+
+;; this is the keymap for greader-queue-mode.
+(defvar-keymap greader-queue-mode-map
+  :doc "greader-queue-mode map."
+  "C-r RET" #'greader-queue-add-element
+  "C-r SPC" #'greader-queue-read
+  "C-r <left>" #'greader-queue-backward
+  "C-r <right>" #'greader-queue-forward
+  "C-r ." #'greader-queue-stop)
+(defvar-local greader-greader-mode-was-active nil
+  "This variable becomes t if `greader-mode' is active when
+`greader-queue-mode' is enabled.
+This is to avoid reactivating `greader-mode' if it wasn't active
+before.")
+
+;;;###autoload
+(define-minor-mode greader-queue-mode
+  "In this mode, text reading occurs via blocks.
+normally greader reads the text of a buffer sequentially; in queue-mode you 
add blocks of text to a queue, so you can choose only certain parts of the 
buffer.
+As reading progresses, further blocks can be added,
+or you can add the blocks and then start reading."
+  :lighter "greader-q"
+  (cond
+   (greader-queue-mode
+    (when greader-mode
+      (setq greader-greader-mode-was-active t)
+      (greader-mode -1)))
+   ((not greader-queue-mode)
+    (when greader-greader-mode-was-active
+      (greader-mode 1)))))
+
 (provide 'greader)
 ;;; greader.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]