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

    greader now manages regions.
---
 greader.el | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/greader.el b/greader.el
index 3bcc0ec880..4f288a3ed1 100644
--- a/greader.el
+++ b/greader.el
@@ -303,6 +303,61 @@ when the buffer is visiting a file."
   (if greader-auto-bookmark-mode
       (add-hook 'greader-after-stop-hook 'set-bookmark-for-greader)
     (remove-hook 'greader-after-stop-hook 'set-bookmark-for-greader)))
+;; greader-region-mode is a non-interactive minor mode that deals with
+;; read the active region instead of the entire buffer.
+;; The current implementation of greader probably dictates that the
+;; buffer needs to be temporarily narrowed when the region is
+;; active, so that the functions that deal with obtaining the sentences
+;; to read and move the point "believe" that that is all the
+;; buffer to read.
+(defvar greader-start-region nil
+  "start of region.")
+(defvar greader-end-region nil
+  "end of region.")
+
+(defun greader--active-region-p ()
+  "Return t if the region in the current buffer is active.
+Active in this context means that the variables
+  `greader-start-region' and `greader-end-region' are set appropriately."
+  (if (and greader-start-region greader-end-region)
+      t
+    nil))
+
+(defun greader-narrow ()
+  "Narrow current buffer if region is active."
+  (unless (buffer-narrowed-p)
+    (narrow-to-region greader-start-region greader-end-region)))
+
+;; This function widens the buffer, and is added to the
+;; `greader-after-stop-hook' hook by `greader-region-mode'.
+(defun greader-widen ()
+  "Widen buffer and set greader-region variables to nil."
+  (setq greader-start-region nil)
+  (setq greader-end-region nil)
+  (greader-region-mode -1)  
+  (widen))
+
+;; This function places the point at the beginning of the active region.
+(defun greader-set-point-to-start-of-region ()
+  "set the point to the beginning of the active region.
+This only happens if the variables `greader-start-region' and
+`greader-end-region' are set."
+  (when (and greader-start-region greader-end-region)
+    (goto-char greader-start-region)))
+
+(define-minor-mode greader-region-mode
+  "This mode activates when the region is active."
+  :interactive nil
+  (if greader-region-mode
+      (progn
+       (setq greader-start-region (region-beginning))
+       (setq greader-end-region (region-end))
+       (greader-narrow)
+       (add-hook 'greader-after-stop-hook 'greader-widen)
+       (add-hook 'greader-before-finish-hook 'greader-widen)
+       (greader-set-point-to-start-of-region))
+    (remove-hook 'greader-before-finish-hook 'greader-widen)
+    (remove-hook 'greader-after-stop-hook 'greader-widen)))
 
 (defun greader-set-register ()
   "Set the `?G' register to the point in current buffer."
@@ -534,6 +589,10 @@ if `GOTO-MARKER' is t and if you pass a prefix to this
   (cond
    ((and (greader-timer-flag-p) (not (timerp greader-stop-timer)))
     (greader-setup-timers)))
+  (when (region-active-p)
+    (cond
+     ((and (not greader-region-mode) (not (greader--active-region-p)))
+      (greader-region-mode 1))))
   (run-hooks greader-before-get-sentence-functions)
   (let ((chunk (funcall greader-read-chunk-of-text)))
     (if chunk

Reply via email to