Subject: 26.1: calc-mode header line [UPDATED PATCH]

From: Boruch Baum <[email-masked]>
Date: Wed, 9 Sep 2020 14:31:27 -0400

This is summary of related mail on emacs-devel (an updated patch is attached):

On 2020-08-31 14:44, Boruch Baum wrote:
> Back in version 21, emacs introduced a static 'header-line' that could
> be inserted at the top of any buffer. Calc mode is one emacs package
> that does not use it and could benefit from it, so the attached patch
> offers that feature. The main benefit is that the 'calc trail' buffer
> (what some greybeards from the mechanical age would remember as the
> 'tape reel') no longer has its title line scroll off the visible
> window. The patch also includes:
>
> 1) Width-sensitive text for the header line, so that it is readable for
>    very narrow windows, and scales to very wide windows.
>
> 2) Display of the 'calc trail' buffer when invoking calc from a frame
>    that is split vertically (C-x 3, M-x split-window-right).
>
> 3) My version of emacs includes a unicode character at 'C-x 8 <return>
>    POCKET CALCULATOR', that I did not include in the header line as the
>    mode's icon, but that could be done.
>
> The patch was diff'ed against the version of emacs that I have: the
> latest-and-greatest that debian is distributing ... v26.1

On 2020-09-07 14:01, Boruch Baum wrote:
> First, congratulations on assuming your new responsibilities.
>
> On 2020-09-07 17:00, Lars Ingebrigtsen wrote:
> > The patch doesn't apply to Emacs 28, so I've respun it (included below).
>
> Oops. I didn't think there would be a difference. I'm using emacs 26.1
> in debian and I didn't download the v28 calc.el
>
> > This is somewhat inscrutable, and is repeated twice (once for the calc
> > buffer and once for the trail buffer).
> >
> > It just centres whatever the string like "--- this ---", so it seems
> > like it should land in a single function for reuse.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
--- calc.el	2020-08-30 15:03:07.563840474 -0400
+++ calc-new.el	2020-09-09 14:12:01.949816446 -0400
@@ -1396,6 +1396,31 @@
     (set-keymap-parent map calc-mode-map)
     map))

+(defun calc--header-line (long short width &optional fudge)
+  "Return a Calc header line appropriate for the buffer width.
+
+This is an internal function for `calc-mode'.
+
+LONG is a desired text for a wide window, SHORT is a desired
+abbreviated text, and width is the buffer width, which will be
+some fraction of the 'parent' window width (At the time of
+writing, 2/3 for calc, 1/3 for trail). The optional FUDGE is a
+trial-and-error adjustment number for the edge-cases at the
+border of the two cases."
+;; TODO: This could be called as part of a 'wiindow-resize' hook.
+  (setq header-line-format
+    (let* ((len-long (length long))
+           (len-short (length short))
+           (fudge (or fudge 0))
+           ; fudge for trail is: -3 (added to len-long)
+           ; (width  ) for trail
+           (factor (if (> width (+ len-long fudge)) len-long len-short))
+           (size   (/ (- width factor) 2))
+           (fill (make-string size ?-))
+           (pre  (replace-regexp-in-string ".$" " " fill))
+           (post (replace-regexp-in-string "^." " " fill)))
+      (concat pre (if (= factor len-long) long short) post))))
+
 (define-derived-mode calc-trail-mode fundamental-mode "Calc Trail"
   "Calc Trail mode.
 This mode is used by the *Calc Trail* buffer, which records all results
@@ -1410,21 +1435,27 @@
   (setq buffer-read-only t)
   (make-local-variable 'overlay-arrow-position)
   (make-local-variable 'overlay-arrow-string)
-  (when (= (buffer-size) 0)
+  (if (and (not calc-show-banner)
+           (= (buffer-size) 0))
     (let ((buffer-read-only nil))
-      (insert (propertize "Emacs Calculator Trail\n" 'face 'italic)))))
+      (insert (propertize "Emacs Calculator Trail\n" 'face 'italic)))
+  (calc--header-line "Emacs Calculator Trail" "Calc Trail"
+                     (/ (window-width) 3) -3)))

 (defun calc-create-buffer ()
   "Create and initialize a buffer for the Calculator."
   (set-buffer (get-buffer-create "*Calculator*"))
   (or (derived-mode-p 'calc-mode)
       (calc-mode))
+  (when calc-show-banner
+    (calc--header-line "Emacs Calculator Mode" "Emacs Calc"
+                       (/ (* (window-width) 2) 3) 1)
   (setq max-lisp-eval-depth (max max-lisp-eval-depth 1000))
   (when calc-always-load-extensions
     (require 'calc-ext))
   (when calc-language
     (require 'calc-ext)
-    (calc-set-language calc-language calc-language-option t)))
+    (calc-set-language calc-language calc-language-option t))))

 ;;;###autoload
 (defun calc (&optional arg full-display interactive)
@@ -1465,7 +1496,6 @@
                 (pop-to-buffer (current-buffer)))))))
       (with-current-buffer (calc-trail-buffer)
         (and calc-display-trail
-             (= (window-width) (frame-width))
              (calc-trail-display 1 t)))
       (message "Welcome to the GNU Emacs Calculator!  Press `?' or `h' for help, `q' to quit")
       (run-hooks 'calc-start-hook)
@@ -2000,13 +2030,8 @@
 	      (calc-any-evaltos nil))
 	 (setq calc-any-selections nil)
 	 (erase-buffer)
-	 (when calc-show-banner
-	   (insert (propertize "--- Emacs Calculator Mode ---\n"
-			       'face 'italic)))
 	 (while thing
 	   (goto-char (point-min))
-	   (when calc-show-banner
-	     (forward-line 1))
 	   (insert (math-format-stack-value (car thing)) "\n")
 	   (setq thing (cdr thing)))
 	 (calc-renumber-stack)
@@ -2091,7 +2116,6 @@
 	   (eq (marker-buffer calc-trail-pointer) calc-trail-buffer))
       (with-current-buffer calc-trail-buffer
 	(goto-char (point-min))
-	(forward-line 1)
 	(setq calc-trail-pointer (point-marker))))
   calc-trail-buffer)

@@ -2159,10 +2183,8 @@
   (if (derived-mode-p 'calc-trail-mode)
       (progn
 	(beginning-of-line)
-	(if (bobp)
-	    (forward-line 1)
 	  (if (eobp)
-	      (forward-line -1)))
+          (forward-line -1))
 	(if (or (bobp) (eobp))
 	    (setq overlay-arrow-position nil)   ; trail is empty
 	  (set-marker calc-trail-pointer (point) (current-buffer))
@@ -2176,7 +2198,7 @@
 	    (if win
 		(save-excursion
 		  (forward-line (/ (window-height win) 2))
-		  (forward-line (- 1 (window-height win)))
+		  (forward-line (- 2 (window-height win)))
 		  (set-window-start win (point))
 		  (set-window-point win (+ calc-trail-pointer 4))
 		  (set-buffer calc-main-buffer)
--- NEWS	2020-09-05 22:28:48.607653342 -0400
+++ NEWS.new	2020-09-06 14:37:38.286349012 -0400
@@ -921,6 +921,14 @@
 Likewise, point isn't moved to the end of the string before inserting
 digits.

+---
+*** Buffer headings have been changed.
+The *Calc* and *Calc Trail* buffers now use 'header-line-format' to
+present a persistent buffer header line, and they are sized based upon
+the window width upon launch. The header-line for the *Calc Trail*
+buffer is no longer part of the buffer's text and no longer scrolls
+off the visible display, unless on sets 'calc-show-banner' to NIL.
+
 ** term-mode

 ---