| 1 |
;;; |
| 2 |
;;; text.pager - display text using appropriate pager |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; This module uses some heuristics to present a long text nicely to the user |
| 35 |
|
| 36 |
(define-module text.pager |
| 37 |
(use gauche.process) |
| 38 |
(use gauche.termios) |
| 39 |
(use gauche.vport) |
| 40 |
(use text.console) |
| 41 |
(use file.util) |
| 42 |
(export display/pager |
| 43 |
with-output-to-pager |
| 44 |
pager-program)) |
| 45 |
(select-module text.pager) |
| 46 |
|
| 47 |
(define *default-pager* |
| 48 |
(or (and-let1 s (sys-getenv "PAGER") |
| 49 |
(shell-tokenize-string s)) |
| 50 |
;; NB: We can use more.com on Windows console. On MSYS, however |
| 51 |
;; fork fails for both less.exe and more.exe, so we don't use them. |
| 52 |
(and-let1 cmd (or (find-file-in-paths "less") |
| 53 |
(find-file-in-paths "more" :extensions '("com"))) |
| 54 |
(list cmd)))) |
| 55 |
|
| 56 |
;; Parameter: pager-program |
| 57 |
;; pager program and arguments. |
| 58 |
;; can be #f. |
| 59 |
(define pager-program (make-parameter *default-pager*)) |
| 60 |
|
| 61 |
;; This is to display info document safely on ascii-only output. We can |
| 62 |
;; throw in more heuristics as needed. The goal isn't a perfect solution, |
| 63 |
;; but something that works most of the time. |
| 64 |
(define (unicode->ascii s) |
| 65 |
($ regexp-replace-all* s |
| 66 |
#/\u21d2/ "==>" ; @result{} |
| 67 |
#/\u2026/ "..." ; @dots{} |
| 68 |
#/\u2018/ "`" ; @code{} |
| 69 |
#/\u2019/ "'" ; @code{} |
| 70 |
#/\u201C/ "``" ; `` (e.g. ,i do) |
| 71 |
#/\u201D/ "''" ; '' (e.g. ,i do) |
| 72 |
#/\u2261/ "==" ; @equiv{} (e.g. ,i cut) |
| 73 |
#/\u2212/ "-" ; @minus (e.g. ,i modulo) |
| 74 |
#/\u2022/ "*" ; @bullet (e.g. ,i lambda) |
| 75 |
#/\u2013/ "--" ; -- (e.g. ,i utf8-length) |
| 76 |
#/\u2014/ "---" ; --- (e.g. ,i lambda) |
| 77 |
#/\u00df/ "[Eszett]" ; eszett (e.g. ,i char-upcase) |
| 78 |
)) |
| 79 |
|
| 80 |
(define (mintty?) |
| 81 |
(cond-expand |
| 82 |
[gauche.os.windows |
| 83 |
(and-let* ([t (sys-getenv "TERM")] |
| 84 |
[ (vt100-compatible? t) ]) |
| 85 |
((with-module gauche.internal %sys-mintty?) (current-output-port)))] |
| 86 |
[else #f])) |
| 87 |
|
| 88 |
(define (pager-unavailable?) |
| 89 |
(or (equal? (sys-getenv "TERM") "emacs") |
| 90 |
(equal? (sys-getenv "TERM") "dumb") |
| 91 |
(not (or (sys-isatty (current-output-port)) (mintty?))) |
| 92 |
(not (pager-program)))) |
| 93 |
|
| 94 |
(define (limited-output?) |
| 95 |
(cond-expand |
| 96 |
[gauche.os.windows |
| 97 |
(use os.windows) |
| 98 |
(guard (e [else #f]) |
| 99 |
(and (sys-isatty 1) |
| 100 |
(sys-get-console-mode |
| 101 |
(sys-get-std-handle STD_OUTPUT_HANDLE)) |
| 102 |
(not (= (sys-get-console-output-cp) 65001))))] ;unicode cp |
| 103 |
[else #f])) |
| 104 |
|
| 105 |
(define (pager-filter s) |
| 106 |
(if (limited-output?) |
| 107 |
(unicode->ascii s) |
| 108 |
s)) |
| 109 |
|
| 110 |
(define (pager-filter-port oport) |
| 111 |
(if (limited-output?) |
| 112 |
(make <virtual-output-port> |
| 113 |
:putc (^c (display (unicode->ascii (string c)) oport)) |
| 114 |
:puts (^s (display (unicode->ascii s) oport)) |
| 115 |
:flush (^[] (flush oport)) |
| 116 |
:close (^[] (close-port oport))) |
| 117 |
oport)) |
| 118 |
|
| 119 |
(define (run-pager proc) |
| 120 |
;; We need to ensure the terminal mode is restored even the pager process |
| 121 |
;; died unexpectedly. |
| 122 |
(with-terminal-mode |
| 123 |
(standard-output-port) 'cooked |
| 124 |
(^_ |
| 125 |
;; During running the pager, we block SIGINT and let the pager handle |
| 126 |
;; (or ignore) it, otherwise the terminal would be taken with both the |
| 127 |
;; pager and gosh process and cause a mess. Note that the sent SIGINT |
| 128 |
;; will be handled as soon as we restore the sigmask. |
| 129 |
(let* ([omask (sys-sigmask SIG_BLOCK (sys-sigset SIGINT))] |
| 130 |
[p (run-process (pager-program) :input :pipe :sigmask (sys-sigset))]) |
| 131 |
(unwind-protect |
| 132 |
(proc p) |
| 133 |
(close-output-port (process-input p)) |
| 134 |
(process-wait p) |
| 135 |
(sys-sigmask SIG_SETMASK omask)))))) |
| 136 |
|
| 137 |
;; On MSYS/mintty, calling subprocess somehow doesn't work. We display the |
| 138 |
;; text one page at a time. |
| 139 |
(define (mintty-pager s) |
| 140 |
(call-with-console |
| 141 |
(make <vt100>) ;we know it's vt100 compatible |
| 142 |
(^c |
| 143 |
(receive (h w) (query-screen-size c) |
| 144 |
(let* ((ss (string-split s #\newline)) |
| 145 |
(max-pos (max (- (length ss) (- h 1)) 0))) |
| 146 |
(let loop ((pos 0)) |
| 147 |
(let1 lines (drop* ss pos) |
| 148 |
(clear-screen c) |
| 149 |
(do ([lines lines (cdr lines)] |
| 150 |
[y 0 (+ y 1)]) |
| 151 |
[(or (null? lines) |
| 152 |
(= y (- h 1)))] |
| 153 |
(move-cursor-to c y 0) |
| 154 |
(putstr c (car lines))) |
| 155 |
(move-cursor-to c (- h 1) 0) |
| 156 |
(if (< pos max-pos) |
| 157 |
(putstr c "More ([q] quit) : ") |
| 158 |
(putstr c "End ([q] quit) : "))) |
| 159 |
(let input-loop ([ch (getch c)]) |
| 160 |
(case ch |
| 161 |
[(#\q #\Q)] |
| 162 |
[(KEY_UP #\y) |
| 163 |
(loop (max (- pos 1) 0))] |
| 164 |
[(KEY_DOWN #\e #\return) |
| 165 |
(loop (min (+ pos 1) max-pos))] |
| 166 |
[(KEY_PGUP #\b) |
| 167 |
(loop (max (- pos (- h 1)) 0))] |
| 168 |
[(KEY_PGDN #\f #\space) |
| 169 |
(loop (min (+ pos (- h 1)) max-pos))] |
| 170 |
[(KEY_HOME #\g) |
| 171 |
(loop 0)] |
| 172 |
[(KEY_END #\G) |
| 173 |
(loop max-pos)] |
| 174 |
[else (input-loop (getch c))]))))) |
| 175 |
(clear-screen c) |
| 176 |
(move-cursor-to c 0 0)))) |
| 177 |
|
| 178 |
;; Main Entry Point API |
| 179 |
(define (display/pager s) |
| 180 |
(let1 s (pager-filter s) |
| 181 |
(cond |
| 182 |
[(pager-unavailable?) (display s)] |
| 183 |
[(mintty?) (mintty-pager s)] |
| 184 |
[else (run-pager (^p (display s (process-input p))))]))) |
| 185 |
|
| 186 |
;; Another convenience API |
| 187 |
(define (with-output-to-pager thunk) |
| 188 |
(cond |
| 189 |
[(pager-unavailable?) (display (pager-filter (with-output-to-string thunk)))] |
| 190 |
[(mintty?) (mintty-pager (pager-filter (with-output-to-string thunk)))] |
| 191 |
[else (run-pager |
| 192 |
(^p (with-output-to-port (pager-filter-port (process-input p)) |
| 193 |
thunk)))])) |