Form feed-separated plain text presentations with Emacs
git clone https://www.shemshak.org/~amin/ffs
1 ;;; ffs.el --- Form feed-separated plain text presentations -*- lexical-binding: t; -*-23 ;; Copyright (c) 2022-2026 Free Software Foundation, Inc.45 ;; Author: Amin Bandali <bandali@gnu.org>6 ;; Maintainer: Amin Bandali <bandali@gnu.org>7 ;; URL: https://www.shemshak.org/~amin/ffs8 ;; Version: 0.2.39 ;; Package-Requires: ((emacs "27.1"))10 ;; Keywords: convenience, focus, narrowing, outlines, presentation, text1112 ;; This program is free software; you can redistribute it and/or modify13 ;; it under the terms of the GNU General Public License as published by14 ;; the Free Software Foundation, either version 3 of the License, or15 ;; (at your option) any later version.1617 ;; This program is distributed in the hope that it will be useful,18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20 ;; GNU General Public License for more details.2122 ;; You should have received a copy of the GNU General Public License23 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.2425 ;;; Commentary:2627 ;; A simple mode for simple plain text presentations where the slides28 ;; are separated using the `page-delimiter', by default the form feed29 ;; character (^L).30 ;;31 ;; After installing `ffs', open a text file/buffer containing your32 ;; slides and do `M-x ffs-mode RET'. You can add a key binding for33 ;; `ffs-mode' to your init file for convenience:34 ;;35 ;; (global-set-key (kbd "C-c f s") #'ffs-mode)36 ;;37 ;; You should then see "ffs" appear as one of the currently enabled38 ;; minor modes in your mode line. Once `ffs-mode' is enabled, it puts39 ;; the buffer in read-only mode, and provides convenient key bindings40 ;; for moving between, editing, and inserting slides. To see a list41 ;; of available commands, you can type `?' or `C-h m' to invoke42 ;; `describe-mode' which will include a list of the key bindings in43 ;; the `ffs-mode-map' keymap, or type `M-x ffs- TAB' to get a44 ;; completion of commands starting with the "ffs-" prefix.45 ;;46 ;; There are other packages with similar or related functionality to47 ;; `ffs' that you might also want to look into. The excellent `logos'48 ;; by Proteilaos is the one most similar to `ffs'. Though envisioned49 ;; primarily as a "focus mode" for disctraction-free reading and50 ;; writing, it can also be well-suited for minimalist presentations.51 ;; A potential benefit of `ffs' over `logos' is that it provides52 ;; convenience commands and out-of-the-box key bindings for writing53 ;; and editing slides and presenting them. On the other hand, `logos'54 ;; has a few more user options and may be more flexible than `ffs'.55 ;; For instance, `logos-outlines-are-pages' and its accompanying56 ;; `logos-outline-regexp-alist' allow treating each outline heading57 ;; as a page delimiter, a feature currently not provided by `ffs'.58 ;;59 ;; `org-present', `org-tree-slide', and `dslide' are meant for use60 ;; with Org mode files specifically, in contrast to `ffs' and `logos'61 ;; which may be used with any major mode.62 ;;63 ;; The feature that to my knowledge is unique to `ffs' is built-in64 ;; support for speaker notes: `ffs-find-speaker-notes-file' prompts65 ;; the user for a speaker notes file, and by default displays it in66 ;; a new frame. Then, slide motions (e.g. going to the previous or67 ;; next slide) will affect both the speaker notes buffer and the main68 ;; slides buffer.69 ;;70 ;;71 ;; Refer to the manual for more details on ffs's user options and72 ;; usage, a sample configuration, and other pieces of information:73 ;; <https://www.shemshak.org/~amin/ffs/ffs.html>.7475 ;;; Code:7677 (require 'map)78 (require 'page)79 (require 'format-spec)8081 ␌82 ;;;; User options and variables8384 (defgroup ffs nil85 "Minor mode for form feed-separated plain text presentations."86 :group 'editing)8788 (defcustom ffs-page-delimiter "␌"89 "The default page delimiter for ffs, the form feed character.90 If you use a custom `page-delimiter' regexp, be sure to customize this91 as well."92 :type 'string93 :package-version '(ffs . "0.2.0"))9495 (defcustom ffs-echo-progress nil96 "Whether to display in echo area the progress through the slides.97 When non-nil, changing slides will also display the progress through98 the slides in the echo area."99 :type 'boolean100 :local t101 :package-version '(ffs . "0.2.0"))102103 (defcustom ffs-echo-progress-format "Slide %c of %t"104 "The format spec used for function `ffs-echo-progress-format'.105106 %c Number of current slide.107 %t Total number of slides."108 :type 'string109 :local t110 :package-version '(ffs . "0.2.0"))111112 (defcustom ffs-default-face-height nil113 "Value of `height' property of `default' face during presentations.114 If a natural number (non-negative integer), it will be used as the115 `height' of the `default' face during presentations, useful for having116 a larger font size when presenting.117118 If nil, don't change the `default' face's `height' in presentations."119 :type '(choice (const nil)120 (natnum :value 250))121 :package-version '(ffs . "0.2.0"))122123 (defcustom ffs-hide-cursor nil124 "When non-nil hide the cursor.125 This is only relevant when `ffs-present-mode' is enabled."126 :type 'boolean127 :local t128 :package-version '(ffs . "0.2.0"))129130 (defcustom ffs-hide-mode-line nil131 "When non-nil hide the mode line.132 This is only relevant when `ffs-present-mode' is enabled."133 :type 'boolean134 :local t135 :package-version '(ffs . "0.2.0"))136137 (defcustom ffs-hide-header-line nil138 "When non-nil hide the header line.139 This is only relevant when `ffs-present-mode' is enabled."140 :type 'boolean141 :local t142 :package-version '(ffs . "0.2.0"))143144 (defcustom ffs-edit-display-buffer-alist145 '(display-buffer-same-window146 (inhibit-same-window . nil))147 "Window configuration for the `ffs-edit' buffer.148 By default, it will display the `ffs-edit' buffer in the same window."149 :type display-buffer--action-custom-type150 :package-version '(ffs . "0.2.0"))151152 (defcustom ffs-edit-done-hook nil153 "Hook run after editing a slide (at the end of `ffs-edit-done')."154 :type 'hook)155156 (defvar ffs-edit-buffer-name "*ffs-edit*"157 "The name of the `ffs-edit' buffer used when editing a slide.")158159 (defvar ffs-find-speaker-notes-function #'find-file-other-frame160 "The function to use when finding a speaker's note file.")161162 ;;;;; Internal variables163164 (defvar-local ffs--edit-source-buffer nil165 "The ffs presentation buffer of the slide being edited.")166167 (defvar-local ffs--new-location nil168 "The location where the new slide should be inserted.169 See the docstring for `ffs-edit' for more details.")170171 (defvar-local ffs--slides-buffer nil172 "The main ffs presentation slides buffer.173 When the user enables ffs in a buffer using `\\[ffs]', we store a174 reference to that buffer in this variable.175176 As a special case, in a speaker notes buffer selected by the user177 using `\\[ffs-find-speaker-notes-file]' from the main ffs slides178 buffer, this variable will point to the main ffs slides buffer179 rather than the speaker notes buffer.")180181 (defvar-local ffs--notes-buffer nil182 "The ffs speaker notes buffer (only if selected).183 When the user chooses (and opens) a speaker notes file using184 `\\[ffs-find-speaker-notes-file]', a reference to the file's185 corresponding buffer is stored in this variable, local to the186 main ffs presentation slides buffer (`ffs--slides-buffer').")187188 (defvar-local ffs--default-face-height-cookie nil189 "Cookie returned from `face-remap-add-relative', for later removal.")190191 (defvar ffs--pkg-dir192 (and-let* ((file (or (and load-in-progress load-file-name)193 (eval-when-compile (macroexp-file-name))194 (locate-library "ffs")))195 (dir (file-name-directory file)))196 dir)197 "The directory where `ffs' is located.")198199 ␌200 ;;;; Slide count / presentation progress201202 (declare-function page--what-page "page")203 (defun ffs-current-slide-number ()204 "Return the number of current slide."205 (interactive)206 (car (page--what-page)))207208 (defun ffs-total-slide-count ()209 "Return the total number of slides."210 (interactive)211 (save-restriction212 (save-excursion213 (widen)214 (goto-char (point-max))215 (ffs-current-slide-number))))216217 (defun ffs-echo-progress-format ()218 "Return progress through slides per variable `ffs-echo-progress-format'."219 (format-spec220 ffs-echo-progress-format221 `((?c . ,(ffs-current-slide-number))222 (?t . ,(ffs-total-slide-count)))))223224 ␌225 ;;;; Slide motions226227 (defun ffs--goto-previous (buffer)228 "Go to the previous slide in the given BUFFER."229 (interactive)230 (with-current-buffer buffer231 (let ((n (buffer-narrowed-p)))232 (when n233 (goto-char (point-min))234 (widen)235 (backward-page))236 (backward-page)237 (when n (narrow-to-page)))))238239 (defun ffs--goto-next (buffer)240 "Go to the next slide in the given BUFFER."241 (interactive)242 (with-current-buffer buffer243 (let ((n (buffer-narrowed-p))244 (e (= (- (point-max) (point-min)) 0)))245 (when n246 (goto-char (point-min))247 (widen))248 (unless e (forward-page))249 (when n (narrow-to-page)))))250251 (defun ffs--goto-first (buffer)252 "Go to the first slide in the given BUFFER."253 (interactive)254 (with-current-buffer buffer255 (let ((n (buffer-narrowed-p)))256 (when n (widen))257 (goto-char (point-min))258 (when n (narrow-to-page)))))259260 (defun ffs--goto-last (buffer)261 "Go to the last slide in the given BUFFER."262 (interactive)263 (with-current-buffer buffer264 (let ((n (buffer-narrowed-p)))265 (when n (widen))266 (goto-char (point-max))267 (when n (narrow-to-page)))))268269 (defvar ffs-present-mode)270271 (defmacro ffs--define-goto-slide (name)272 "Define a function for going to a slide.273 Symbol NAME is the name describing the movement."274 (declare (indent 1))275 (let* ((sn (symbol-name name))276 (fname (intern (format "ffs-goto-%s" (downcase sn))))277 (hname (intern (format "ffs--goto-%s" (downcase sn))))278 (doc (format279 "Go to %s slide in ffs presentation and speaker notes buffer."280 sn)))281 `(defun ,fname ()282 ,doc283 (interactive)284 (,hname ffs--slides-buffer)285 (when ffs--notes-buffer286 (,hname ffs--notes-buffer)287 ;; FIXME: Why is this needed?288 (redraw-display))289 (when (and ffs-present-mode ffs-echo-progress)290 (message (ffs-echo-progress-format))))))291292 (ffs--define-goto-slide previous)293 (ffs--define-goto-slide next)294 (ffs--define-goto-slide first)295 (ffs--define-goto-slide last)296297 ␌298 ;;;; Slide insertion and editing299300 (defun ffs-edit (&optional add-above-or-below)301 "Pop to a new buffer to edit a slide.302 If ADD-ABOVE-OR-BELOW is nil or not given, we are editing an303 existing slide. Otherwise, if it is `add-above' then the new304 slide will be added above/before the current slide, and if it is305 `add-below' then the new slide will be added below/after the306 current slide. The logic is implemented in `ffs-edit-done'."307 (interactive)308 (let* ((b (current-buffer))309 (nb (generate-new-buffer ffs-edit-buffer-name))310 (fc fill-column)311 (m major-mode)312 (n (buffer-narrowed-p))313 (s (if add-above-or-below ; if we are adding a new slide314 "\n" ; start with just a newline315 (unless n (narrow-to-page))316 (prog1 (buffer-string)317 (unless n (widen))))))318 (with-current-buffer nb319 (erase-buffer)320 (insert s)321 (goto-char (point-min))322 (set-buffer-modified-p nil)323 (setq-local324 ffs--edit-source-buffer b325 ffs--new-location add-above-or-below326 fill-column fc327 header-line-format328 (substitute-command-keys "Edit, then use `\\[ffs-edit-done]' \329 to apply your changes or `\\[ffs-edit-discard]' to discard them."))330 (funcall m)331 (ffs-edit-mode 1))332 (let ((window (display-buffer nb ffs-edit-display-buffer-alist)))333 (when window334 (select-window window)))))335336 (defun ffs-new-above ()337 "Add a new slide above/before the current slide."338 (interactive)339 (ffs-edit 'add-above))340341 (defun ffs-new-below ()342 "Add a new slide below/after the current slide."343 (interactive)344 (ffs-edit 'add-below))345346 (defun ffs-edit-discard ()347 "Discard current `ffs-edit' buffer and return to the presentation."348 (interactive)349 (let ((b (current-buffer)))350 (quit-windows-on b)351 (kill-buffer b)))352353 (defun ffs-edit-done ()354 "Apply the `ffs-edit' changes and return to the presentation."355 (interactive)356 (let* ((f nil)357 (str (buffer-string))358 (s (if (string-suffix-p "\n" str)359 str360 (concat str "\n")))361 (sn (format "\n%s%s" s ffs-page-delimiter))362 (l ffs--new-location))363 (ffs-edit-mode -1)364 (with-current-buffer ffs--edit-source-buffer365 (let ((inhibit-read-only t))366 (save-excursion367 (cond368 ((eq l 'add-above)369 (backward-page)370 (insert sn)371 (setq f #'ffs-goto-previous))372 ((eq l 'add-below)373 (forward-page)374 (insert sn)375 (setq f #'ffs-goto-next))376 ((null l)377 (narrow-to-page)378 (delete-region (point-min) (point-max))379 (insert s)380 (widen))))))381 (ffs-edit-discard)382 (when (functionp f)383 (funcall f)))384 (run-hooks 'ffs-edit-done-hook))385386 (defvar ffs-edit-mode-map387 (let ((map (make-sparse-keymap)))388 (define-key map (kbd "C-c C-k") #'ffs-edit-discard)389 (define-key map (kbd "C-c C-c") #'ffs-edit-done)390 map)391 "Keymap for `ffs-edit-mode'.")392393 (define-minor-mode ffs-edit-mode394 "Minor mode for editing a single ffs slide.395 When done editing the slide, run \\[ffs-edit-done] to apply your396 changes, or \\[ffs-edit-discard] to discard them."397 :lighter " ffs-edit")398399 ␌400 ;;;; Focus toggles401402 ;; XXX user option for more sophisticated behaviour?403 ;; set-face-attribute for specific frames404 ;; face-list reset to defaults405 (defun ffs-toggle-dark-mode ()406 "Swap the frame background and foreground colours."407 (interactive)408 (let ((bg (frame-parameter nil 'background-color))409 (fg (frame-parameter nil 'foreground-color)))410 (set-background-color fg)411 (set-foreground-color bg)))412413 ;; I learned about the method of using `ffs-set-buffer-local-value'414 ;; from Protesilaos <https://protesilaos.com> and his Logos package,415 ;; who in turn learned about it from Daniel Mendler. Protesilaos's416 ;; version simply uses `push', but I need to retrieve specific values417 ;; later, so I use a plist and `plist-put' instead.418 (defvar-local ffs--restore nil)419420 (defun ffs-set-buffer-local-value (var val)421 "Set VAR to buffer-local VAL."422 (let ((old (and (boundp var) (symbol-value var))))423 (unless (equal old val)424 (set var val)425 (if (local-variable-p var)426 (setq-local427 ffs--restore428 (plist-put ffs--restore var (lambda () (set var old))))429 (make-local-variable var)430 (setq-local431 ffs--restore432 (plist-put433 ffs--restore var (lambda () (kill-local-variable var))))))))434435 (defun ffs-hide-cursor ()436 "Hide cursor if variable `ffs-hide-cursor' is non-nil.437 If we already hid the cursor, restore it. Otherwise, when variable438 `ffs-hide-cursor' is non-nil, set `cursor-type' to nil."439 (interactive)440 (if-let* ((f (plist-get ffs--restore 'cursor-type)))441 (progn442 (funcall f)443 (setq-local444 ffs--restore (map-delete ffs--restore 'cursor-type)))445 (when ffs-hide-cursor446 (ffs-set-buffer-local-value 'cursor-type nil))))447448 (defun ffs-hide-mode-line ()449 "Hide mode line if variable `ffs-hide-mode-line' is non-nil.450 If we already hid the mode line, restore it. Otherwise, when variable451 `ffs-hide-mode-line' is non-nil, set `mode-line-format' to nil."452 (interactive)453 (if-let* ((f (plist-get ffs--restore 'mode-line-format)))454 (progn455 (funcall f)456 (setq-local457 ffs--restore (map-delete ffs--restore 'mode-line-format)))458 (when ffs-hide-mode-line459 (ffs-set-buffer-local-value 'mode-line-format nil)))460 (redraw-display))461462 (defun ffs-hide-header-line ()463 "Hide header line if variable `ffs-hide-header-line' is non-nil.464 If we already hid the header line, restore it. Otherwise, when variable465 `ffs-hide-header-line' is non-nil, set `header-line-format' to nil."466 (interactive)467 (if-let* ((f (plist-get ffs--restore 'header-line-format)))468 (progn469 (funcall f)470 (setq-local471 ffs--restore (map-delete ffs--restore 'header-line-format)))472 (when ffs-hide-header-line473 (ffs-set-buffer-local-value 'header-line-format nil))))474475 (defun ffs-toggle-echo-progress ()476 "Toggle variable `ffs-echo-progress'."477 (interactive)478 (setq-local ffs-echo-progress (not ffs-echo-progress))479 (message480 "%s is now locally set to %s"481 'ffs-echo-progress ffs-echo-progress))482483 (defun ffs-toggle-hide-cursor ()484 "Toggle variable `ffs-hide-cursor'."485 (interactive)486 (setq-local ffs-hide-cursor (not ffs-hide-cursor))487 (message488 "%s is now locally set to %s"489 'ffs-hide-cursor ffs-hide-cursor)490 (when ffs-present-mode491 (ffs-hide-cursor)))492493 (defun ffs-toggle-hide-mode-line ()494 "Toggle variable `ffs-hide-mode-line'."495 (interactive)496 (setq-local ffs-hide-mode-line (not ffs-hide-mode-line))497 (message498 "%s is now locally set to %s"499 'ffs-hide-mode-line ffs-hide-mode-line)500 (when ffs-present-mode501 (ffs-hide-mode-line)))502503 (defun ffs-toggle-hide-header-line ()504 "Toggle variable `ffs-hide-header-line'."505 (interactive)506 (setq-local ffs-hide-header-line (not ffs-hide-header-line))507 (message508 "%s is now locally set to %s"509 'ffs-hide-header-line ffs-hide-header-line)510 (when ffs-present-mode511 (ffs-hide-header-line)))512513 (defvar ffs-toggle-prefix-map514 (let ((map (make-sparse-keymap)))515 (define-key map (kbd "e") #'ffs-toggle-echo-progress)516 (define-key map (kbd "c") #'ffs-toggle-hide-cursor)517 (define-key map (kbd "m") #'ffs-toggle-hide-mode-line)518 (define-key map (kbd "h") #'ffs-toggle-hide-header-line)519 (define-key map (kbd "d") #'ffs-toggle-dark-mode)520 map)521 "Keymap for ffs toggle commands.")522523 ␌524 ;;;; Miscellaneous525526 (defvar ffs-mode)527528 (defun ffs-start ()529 "Start presenting."530 (interactive)531 (unless ffs-mode532 (ffs-mode 1))533 (unless ffs-present-mode534 (ffs-present-mode 1)))535536 (defun ffs-stop-or-quit ()537 "Stop presenting. If not currently presenting, disable `ffs-mode'."538 (interactive)539 (when ffs-mode540 (if ffs-present-mode541 (ffs-present-mode -1)542 (ffs-mode -1))))543544 (defun ffs-undo (&optional arg)545 "Like `undo', but it works even when the buffer is read-only.546 Repeat this command to undo more changes.547 A numeric ARG serves as a repeat count."548 (interactive "P")549 (let ((inhibit-read-only t))550 (undo arg)))551552 (defun ffs-find-speaker-notes-file (file)553 "Prompt user for a speaker notes FILE, open it in a new frame."554 (interactive "Fspeaker's notes buffer: ")555 (let ((b (current-buffer)))556 (save-excursion557 (funcall ffs-find-speaker-notes-function file)558 (ffs-mode 1)559 (setq-local560 ffs--slides-buffer b561 ffs--notes-buffer (current-buffer)))562 (setq-local ffs--notes-buffer (get-file-buffer file))))563564 ;; XXX write all slides to one file?565 (defun ffs-export-slides-to-pdf ()566 "Export slides to PDF (needs Emacs built with Cairo)."567 (interactive)568 (with-current-buffer ffs--slides-buffer569 (ffs-goto-first)570 (let ((c 1)571 (fringe fringe-mode))572 (fringe-mode 0)573 (while (not (eobp))574 (let ((fn (format "%s-%03d.pdf"575 (file-name-sans-extension (buffer-name))576 c))577 (data (x-export-frames nil 'pdf)))578 (with-temp-file fn579 (insert data)))580 (setq c (+ c 1))581 (ffs-goto-next))582 (fringe-mode fringe))))583584 (defun ffs-news ()585 "Show ffs news in a manner similar to `view-emacs-news'."586 (interactive)587 (if ffs--pkg-dir588 (progn589 (find-file (expand-file-name "NEWS" ffs--pkg-dir))590 (when (fboundp 'emacs-news-view-mode)591 (emacs-news-view-mode))592 (goto-address-mode 1))593 (message "Could not find ffs NEWS file")))594595 ␌596 ;;;; Main minor modes597598 (defvar ffs-mode-map599 (let ((map (make-sparse-keymap)))600 (define-key map (kbd "p") #'ffs-goto-previous)601 (define-key map (kbd "n") #'ffs-goto-next)602 (define-key map (kbd "DEL") #'ffs-goto-previous)603 (define-key map (kbd "SPC") #'ffs-goto-next)604 (define-key map (kbd "[") #'ffs-goto-previous)605 (define-key map (kbd "]") #'ffs-goto-next)606 (define-key map (kbd "<") #'ffs-goto-first)607 (define-key map (kbd ">") #'ffs-goto-last)608 (define-key map (kbd "s") #'ffs-start)609 (define-key map (kbd "q") #'ffs-stop-or-quit)610 (define-key map (kbd "e") #'ffs-edit)611 (define-key map (kbd "C-c '") #'ffs-edit)612 (define-key map (kbd "O") #'ffs-new-above)613 (define-key map (kbd "o") #'ffs-new-below)614 (define-key map (kbd "S") #'ffs-find-speaker-notes-file)615 (define-key map (kbd "N") #'narrow-to-page)616 (define-key map (kbd "W") #'widen)617 (define-key map [remap undo] #'ffs-undo) ; `C-_', `C-/', `C-x u'618 (define-key map (kbd "t") ffs-toggle-prefix-map)619 (define-key map (kbd "?") #'describe-mode)620 map)621 "Keymap for `ffs-mode'.")622623 ;;;###autoload624 (define-minor-mode ffs-mode625 "Minor mode for form feed-separated plain text presentations."626 :lighter " ffs"627 (setq-local628 buffer-read-only ffs-mode629 ffs--slides-buffer (current-buffer)))630631 ;;;###autoload632 (defalias 'ffs #'ffs-mode)633634 (defvar ffs-present-mode-map635 (let ((map (make-sparse-keymap)))636 (set-keymap-parent map ffs-mode-map)637 (define-key map (kbd "s") #'ffs-stop-or-quit)638 map)639 "Keymap for `ffs-present-mode'.")640641 (declare-function face-remap-add-relative "face-remap" (face &rest specs))642 (declare-function face-remap-remove-relative "face-remap" (cookie))643 (define-minor-mode ffs-present-mode644 "Minor mode for active ffs presentations."645 :lighter nil646 (map-do (lambda (_var fun) (funcall fun)) ffs--restore)647 (setq-local ffs--restore nil)648 (if ffs-present-mode649 (progn650 (ffs-hide-cursor)651 (ffs-hide-mode-line)652 (ffs-hide-header-line)653 (when (natnump ffs-default-face-height)654 (setq-local655 ffs--default-face-height-cookie656 (face-remap-add-relative657 'default :height ffs-default-face-height)))658 (unless (buffer-narrowed-p)659 (narrow-to-page))660 (when ffs-echo-progress661 (message (ffs-echo-progress-format))))662 (when ffs--default-face-height-cookie663 (face-remap-remove-relative ffs--default-face-height-cookie))664 (when (buffer-narrowed-p)665 (goto-char (point-min))666 (widen))))667668 (provide 'ffs)669 ;;; ffs.el ends here