|
| 1 | +;;; pygmy-mode.el --- major mode for Forth pseudo block files |
| 2 | + |
| 3 | +;; Copyright 2017 Frank Sergeant |
| 4 | + |
| 5 | +;; Version: 17.10 |
| 6 | +;; Author: Frank Sergeant <[email protected]> |
| 7 | +;; Maintainer: Frank Sergeant |
| 8 | +;; URL: http://pygmy.utoh.org |
| 9 | +;; First release: October 2017 |
| 10 | +;; License: GNU General Public License 2 |
| 11 | +;; Distribution: This file is not part of Emacs |
| 12 | + |
| 13 | +;;; Commentary: |
| 14 | + |
| 15 | +;;; pygmy-mode.el is free software distributed under the terms of the |
| 16 | +;;; GNU General Public License, version 2. For details, see the file |
| 17 | +;;; gpl-2.0.txt. |
| 18 | + |
| 19 | +;;; For help with this mode, see the comment below in the definition |
| 20 | +;;; of pygmy-mode or, start pygmy mode with |
| 21 | +;;; |
| 22 | +;;; M-x pygmy-mode |
| 23 | +;;; |
| 24 | +;;; then press |
| 25 | +;;; |
| 26 | +;;; C-h m |
| 27 | + |
| 28 | + |
| 29 | +;;; Code: |
| 30 | + |
| 31 | +;;; To do: |
| 32 | +;;; open a Pygmy Forth process and communicate with it using comint. |
| 33 | + |
| 34 | + |
| 35 | +(require 'org) |
| 36 | + |
| 37 | +;;---- VARS -------------------------------------------------------------------- |
| 38 | + |
| 39 | +(defvar pygmy-mode-map |
| 40 | + (let ((map (make-sparse-keymap))) |
| 41 | + (define-key map "\C-v" 'forward-block-narrow) |
| 42 | + (define-key map "\M-v" 'backward-block-narrow) |
| 43 | + (define-key map [next] 'forward-block-narrow) |
| 44 | + (define-key map [prior] 'backward-block-narrow) |
| 45 | + (define-key map [backtab] 'pygmy-global-cycle) |
| 46 | + (define-key map [tab] 'org-cycle) |
| 47 | + (define-key map (kbd "M-<up>") 'outline-move-subtree-up) |
| 48 | + (define-key map (kbd "M-<down>") 'outline-move-subtree-down) |
| 49 | + map) |
| 50 | + "Keymap for `pygmy-mode'.") |
| 51 | + |
| 52 | +(defvar pygmy-font-lock-keywords |
| 53 | + '((":\\|;\\|VARIABLE" . font-lock-function-name-face) |
| 54 | + ("TRUE\\|FALSE" . font-lock-constant-face)) |
| 55 | + "This is not used at this time.") |
| 56 | + |
| 57 | +(defvar pygmy-outline-regexp "( +\\(block\\|shado\\)" |
| 58 | + "Regexp for identifying a heading and its level. |
| 59 | +
|
| 60 | + A Forth block must start with a left parenthesis at the |
| 61 | + beginning of the line, followed by one or more spaces, |
| 62 | + followed by either 'block' or 'shado'. The 'w' is chopped off |
| 63 | + to make source and shadow blocks the same level. This can be |
| 64 | + adjusted by inserting additional spaces after the opening |
| 65 | + parenthesis. |
| 66 | +
|
| 67 | + If this is changed, pygmy-heading-" ) |
| 68 | + |
| 69 | +(defvar any-block-regexp pygmy-outline-regexp) |
| 70 | +(defvar source-block-regexp "( +\\(block\\) ") |
| 71 | +(defvar shadow-block-regexp "( +\\(shadow\\) ") |
| 72 | + |
| 73 | +;; (defvar pygmy-block-number-regexp |
| 74 | +;; (concat pygmy-outline-regexp " +\\([[:digit:]]+\\)")) |
| 75 | + |
| 76 | +(defvar pygmy-block-number-regexp |
| 77 | + "^( +\\(block\\|shadow\\) +\\([[:digit:]]+\\)") |
| 78 | + |
| 79 | +(defvar pygmy-comment-start "( ") |
| 80 | +(defvar pygmy-comment-end ")") |
| 81 | + |
| 82 | +(defvar pygmy-first-cycle :true |
| 83 | + "Used by pygmy-global-cycle to decide whether to go to beginning of buffer.") |
| 84 | + |
| 85 | + |
| 86 | +;;---- FUNCTIONS -------------------------------------------------------------------- |
| 87 | + |
| 88 | +(defun pygmy-outline-level () |
| 89 | + "Adjust outline level so the top level is 1 instead of the length of the shortest heading. |
| 90 | + Using the default outline-level function with |
| 91 | + pygmy-outline-regexp, the top level would be 7 or so. Change |
| 92 | + this if necessary if pygmy-outline-regexp is changed." |
| 93 | + (- (outline-level) 6)) |
| 94 | + |
| 95 | +(defun start-of-block-p () |
| 96 | + "Answer whether point is at the start of a block marker." |
| 97 | + (interactive) |
| 98 | + (looking-at any-block-regexp)) |
| 99 | + |
| 100 | +(defun start-of-shadow-block-p () |
| 101 | + "Answer whether point is at the start of a shadow block." |
| 102 | + (interactive) |
| 103 | + (looking-at shadow-block-regexp)) |
| 104 | + |
| 105 | +(defun start-of-source-block-p () |
| 106 | + "Answer whether point is at the start of a source block." |
| 107 | + (interactive) |
| 108 | + (looking-at source-block-regexp)) |
| 109 | + |
| 110 | +(defun forward-block-narrow () |
| 111 | + "Move forward to start of next block and narrow the region to |
| 112 | + that block." |
| 113 | + (interactive) |
| 114 | + (widen) |
| 115 | + (outline-next-heading) |
| 116 | + (outline-show-subtree) |
| 117 | + (org-narrow-to-subtree) |
| 118 | + (setq pygmy-first-cycle :true)) |
| 119 | + |
| 120 | +(defun backward-block-narrow () |
| 121 | + "Move backward to start of previous block and narrow the region to that block." |
| 122 | + (interactive) |
| 123 | + (widen) |
| 124 | + (ignore-errors |
| 125 | + (outline-previous-heading)) |
| 126 | + (unless (start-of-block-p) |
| 127 | + (widen) |
| 128 | + ;;(org-cycle t) |
| 129 | + (outline-next-heading)) |
| 130 | + (outline-show-subtree) |
| 131 | + (org-narrow-to-subtree) |
| 132 | + (setq pygmy-first-cycle :true)) |
| 133 | + |
| 134 | +(defun pygmy-global-cycle (&optional arg) |
| 135 | + "When pygmy-first-cycle, run (org-cycle t), else run (org-global-cycle)." |
| 136 | + (interactive "P") |
| 137 | + (widen) |
| 138 | + (if pygmy-first-cycle |
| 139 | + (progn |
| 140 | + (org-cycle t) |
| 141 | + (beginning-of-buffer) |
| 142 | + (setq pygmy-first-cycle nil)) |
| 143 | + (org-global-cycle))) |
| 144 | + |
| 145 | +(defun start-of-block-p () |
| 146 | + "Answer whether point is at the start of a block marker." |
| 147 | + (interactive) |
| 148 | + (looking-at outline-regexp)) |
| 149 | + |
| 150 | +(defun beginning-of-block () |
| 151 | + "Move to the beginning of the current block" |
| 152 | + (interactive) |
| 153 | + (end-of-line) ; move out of block marker if we are in one |
| 154 | + (re-search-backward outline-regexp nil 'end)) |
| 155 | + |
| 156 | +(defun current-block-number () |
| 157 | + "Answer the block number of the current block. This is taken |
| 158 | + from the block header, so it is a 'logical' block number |
| 159 | + rather than a count of physical blocks." |
| 160 | + (interactive) |
| 161 | + (save-excursion |
| 162 | + (beginning-of-block) |
| 163 | + (when (start-of-block-p) |
| 164 | + (progn |
| 165 | + (re-search-forward pygmy-block-number-regexp) |
| 166 | + (string-to-number (match-string-no-properties 2)))))) |
| 167 | + |
| 168 | +(defun renumber-blocks () |
| 169 | + "Run through all the blocks, renumbering the blocks. Renumber |
| 170 | + any shadow block the same as its preceding source block. |
| 171 | + Start with the number in the first block, which must be a |
| 172 | + source block." |
| 173 | + (interactive) |
| 174 | + (save-excursion |
| 175 | + (save-restriction ;; do we really wish to later restore any narrowed state? |
| 176 | + (widen) ;; or maybe we should *not* widen and thus allow renumbering a (narrowed) region |
| 177 | + (beginning-of-buffer) |
| 178 | + (when (not (start-of-block-p)) ;; when not at first block, |
| 179 | + (outline-next-heading)) ;; go forward to the first block or end of buffer |
| 180 | + (if (start-of-shadow-block-p) |
| 181 | + (message "Shadow block cannot be the first block.") |
| 182 | + (when (start-of-block-p) |
| 183 | + (let ((blk-num (current-block-number))) |
| 184 | + (end-of-line) ;; so we don't renumber the very first block |
| 185 | + (while (re-search-forward pygmy-block-number-regexp nil t) |
| 186 | + ;;(message "%s %s" (match-string 1) (match-string 2)) |
| 187 | + (save-match-data ;; why do we need this? Ah, because current-block-number can change it |
| 188 | + (beginning-of-line) |
| 189 | + (when (start-of-source-block-p) |
| 190 | + (setq blk-num (1+ blk-num)))) ; it's a source block, so bump blk-num |
| 191 | + (replace-match (number-to-string blk-num) t t nil 2) |
| 192 | + (end-of-line)))))))) |
| 193 | + |
| 194 | +;;;###autoload |
| 195 | +(define-derived-mode pygmy-mode outline-mode "Pygmy" |
| 196 | + "Major mode for editing Forth pseudo block files. |
| 197 | +
|
| 198 | +This mode accompanies Pygmy Forth, available at http://pygmy.utoh.org. |
| 199 | +
|
| 200 | +This mode helps you edit a text file almost as if it were a |
| 201 | +traditional Forth block file. The trick is to mark the beginning |
| 202 | +of each logical block with a special comment. The opening |
| 203 | +parenthesis must start at the begininng of a line, be followed by |
| 204 | +one or more spaces, followed by either the word `block' or the |
| 205 | +word `shadow', followed by at least one space or a closing |
| 206 | +parenthesis. The comment must be contained on a single line. As |
| 207 | +a Forth comment, it must end eventually with a closing |
| 208 | +parenthesis. |
| 209 | +
|
| 210 | +Here are some examples: |
| 211 | +
|
| 212 | +( block 1 ------------------ load block) |
| 213 | +( shadow 1 ) |
| 214 | +( block 2 miscellaneous) |
| 215 | +( shadow 2 miscellaneous ) |
| 216 | +
|
| 217 | +The block numbers do not need to be consecutive, but they should |
| 218 | +be monotonically increasing. If not, run the command |
| 219 | +
|
| 220 | + M-x renumber-blocks. |
| 221 | +
|
| 222 | +Shadow blocks are not essential but, if they |
| 223 | +are used, should follow their associated source blocks. |
| 224 | +
|
| 225 | +The number of spaces between the opening parenthesis and `block' |
| 226 | +or `shadow' determines the outline level of the heading. This |
| 227 | +allows you to nest shadow blocks under their source blocks if you |
| 228 | +wish, e.g., |
| 229 | +
|
| 230 | +( block 1 ------------------ load block) |
| 231 | +( shadow 1 ) |
| 232 | +( block 2 miscellaneous) |
| 233 | +( shadow 2 miscellaneous ) |
| 234 | +( block 3 something else) |
| 235 | +( shadow 3 something else ) |
| 236 | +
|
| 237 | +or to nest a group of blocks under another block (perhaps a load |
| 238 | +block), e.g., |
| 239 | +
|
| 240 | +( block 1 Logic load block) |
| 241 | +( block2 Operators) |
| 242 | +( block3 Truth values) |
| 243 | +( block 4 Some other category) |
| 244 | +( block 5 This) |
| 245 | +( block 6 That) |
| 246 | +( block 7 The other) |
| 247 | +
|
| 248 | +Put this file (pygmy-mode.el) somewhere in your Emacs load path |
| 249 | +or put the full path to pygmy-mode.el in the autoload form shown |
| 250 | +below. |
| 251 | +
|
| 252 | +Put something like the following in your .emacs file so that |
| 253 | +files ending in .scr or .blk (for example) will be opened in |
| 254 | +pygmy mode. The following assumes pygmy.el is in your home |
| 255 | +directory. |
| 256 | +
|
| 257 | + (autoload |
| 258 | + \\='pygmy-mode |
| 259 | + \"~/pygmy-mode.el\" |
| 260 | + \"A major mode for editing Forth pseudo block files.\" t) |
| 261 | +
|
| 262 | + (add-to-list \\='auto-mode-alist \\='(\"\\\\.scr\\\\\\='\" . pygmy-mode)) |
| 263 | + (add-to-list \\='auto-mode-alist \\='(\"\\\\.blk\\\\\\='\" . pygmy-mode)) |
| 264 | +
|
| 265 | +The available commands are: |
| 266 | +
|
| 267 | +C-v (forward-block-narrow) and M-v (backward-block-narrow) |
| 268 | +(or the page-up and page-down keys) |
| 269 | + move forward or backward a single block. |
| 270 | +
|
| 271 | +renumber-blocks |
| 272 | + renumber the blocks consecutively, starting with the block |
| 273 | + number of the first block in the file. Each shadow block is |
| 274 | + given the same number as its preceding source block. |
| 275 | +
|
| 276 | +<tab> (org-cycle) |
| 277 | + When on a heading (the block comment line), cycle the |
| 278 | + visibility of the current subtree. |
| 279 | +
|
| 280 | +<backtab> (pygmy-global-cycle) |
| 281 | + Cycle outline visibility of the entire file through the 3 |
| 282 | + states: just major headings, all headings, and everything. |
| 283 | +
|
| 284 | +M-<down> (outline-move-subtree-down) |
| 285 | + Move current subtree down |
| 286 | +
|
| 287 | +M-<up> (outline-move-subtree-up) |
| 288 | + Move current subtree up |
| 289 | +
|
| 290 | +Other commands: |
| 291 | +\\{pygmy-mode-map} |
| 292 | +" |
| 293 | + (setq-local comment-start pygmy-comment-start) |
| 294 | + (setq-local comment-end pygmy-comment-end) |
| 295 | + (setq-local outline-regexp pygmy-outline-regexp) |
| 296 | + (setq-local org-outline-regexp pygmy-outline-regexp) |
| 297 | + (setq-local outline-level 'pygmy-outline-level)) |
| 298 | + |
| 299 | +(provide 'pygmy-mode) |
| 300 | + |
| 301 | +;;; pygmy-mode.el ends here |
0 commit comments