[4701a1a] | 1 | ;;; singular.el --- Emacs support for Computer Algebra System Singular |
---|
| 2 | |
---|
[eedab2] | 3 | ;; $Id: singular.el,v 1.48 1999-11-05 14:09:16 obachman Exp $ |
---|
[af6461] | 4 | |
---|
[4701a1a] | 5 | ;;; Commentary: |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | ;;; Code: |
---|
| 9 | |
---|
[1735b4e] | 10 | ;;{{{ Style and coding conventions |
---|
| 11 | |
---|
| 12 | ;; Style and coding conventions: |
---|
| 13 | ;; |
---|
| 14 | ;; - "Singular" is written with an upper-case `S' in comments, doc |
---|
| 15 | ;; strings, and messages. As part of symbols, it is written with |
---|
[e4b1fc] | 16 | ;; a lower-case `s'. |
---|
[f50151] | 17 | ;; - When referring to the Singular interactive mode, do it in that |
---|
| 18 | ;; wording. Use the notation `singular-interactive-mode' only when |
---|
| 19 | ;; really referring to the lisp object. |
---|
| 20 | ;; - use a `fill-column' of 75 for doc strings and comments |
---|
| 21 | ;; - mark incomplete doc strings or code with `NOT READY' optionally |
---|
| 22 | ;; followed by an explanation what exactly is missing |
---|
| 23 | ;; |
---|
[1735b4e] | 24 | ;; - use foldings to structure the source code but try not to exceed a |
---|
[f50151] | 25 | ;; maximum depth of two foldings |
---|
[1735b4e] | 26 | ;; - use lowercase folding titles except for first word |
---|
| 27 | ;; - folding-marks are `;;{{{' and `;;}}}' resp., for sake of standard |
---|
| 28 | ;; conformity |
---|
[f50151] | 29 | ;; - use the foldings to modularize code. That is, each folding should be, |
---|
| 30 | ;; as far as possible, self-content. Define a function `singular-*-init' |
---|
| 31 | ;; in the folding to do the initialization of the module contained in |
---|
| 32 | ;; that folding. Call that function from `singular-interactive-mode', |
---|
| 33 | ;; for example, instead of initializing the module directly from |
---|
| 34 | ;; `singular-interactive-mode'. Look at the code how it is done for the |
---|
| 35 | ;; simple section or for the folding stuff. |
---|
[1735b4e] | 36 | ;; |
---|
| 37 | ;; - use `singular' as prefix for all global symbols |
---|
| 38 | ;; - use `singular-debug' as prefix for all global symbols concerning |
---|
| 39 | ;; debugging. |
---|
[f50151] | 40 | ;; - use, whenever possible without names becoming too clumsy, some unique |
---|
| 41 | ;; prefix inside a folding |
---|
[1735b4e] | 42 | ;; |
---|
| 43 | ;; - mark dependencies on Emacs flavor/version with a comment of the form |
---|
[f50151] | 44 | ;; `;; Emacs[ <version> ]' resp. |
---|
| 45 | ;; `;; XEmacs[ <version> ][ <nasty comment> ]' |
---|
| 46 | ;; specified in that order, if possible |
---|
[1735b4e] | 47 | ;; - use a `cond' statement to execute Emacs flavor/version-dependent code, |
---|
| 48 | ;; not `if'. This is to make such checks more extensible. |
---|
[f50151] | 49 | ;; - try to define different functions for different flavors/version and |
---|
| 50 | ;; use `singular-fset' at library-loading time to set the function you |
---|
| 51 | ;; really need. If the function is named `singular-<basename>', the |
---|
[1735b4e] | 52 | ;; flavor/version-dependent functions should be named |
---|
| 53 | ;; `singular-<flavor>[-<version>]-<basename>'. |
---|
[f50151] | 54 | ;; |
---|
[1735b4e] | 55 | ;; - use `singular-debug' for debugging output/actions |
---|
| 56 | ;; - to switch between buffer and process names, use the functions |
---|
| 57 | ;; `singular-process-name-to-buffer-name' and |
---|
| 58 | ;; `singular-buffer-name-to-process-name' |
---|
[f50151] | 59 | ;; - call the function `singular-keep-region-active' as last statement in |
---|
| 60 | ;; an interactive function that should keep the region active (for |
---|
| 61 | ;; example, in functions that move the point). This is necessary to keep |
---|
| 62 | ;; XEmacs' zmacs regions active. |
---|
| 63 | ;; - to get the process of the current buffer, use `singular-process'. To |
---|
| 64 | ;; get the current process mark, use `singular-process-mark'. Both |
---|
| 65 | ;; functions check whether Singular is alive and throw an error if not, |
---|
| 66 | ;; so you do not have to care about that yourself. If you do not want an |
---|
| 67 | ;; error specify non-nil argument NO-ERROR. But use them anyway. |
---|
[213e96] | 68 | ;; - we assume that the buffer is *not* read-only |
---|
[71c107] | 69 | ;; - use `=' instead of `eq' when comparing buffer locations. Even if you |
---|
| 70 | ;; are sure that both operands are integers. |
---|
[1735b4e] | 71 | |
---|
| 72 | ;;}}} |
---|
| 73 | |
---|
| 74 | ;;{{{ Code common to both modes |
---|
[f50151] | 75 | ;;{{{ Customizing |
---|
[71c107] | 76 | (defgroup singular nil |
---|
| 77 | "Emacs interface to Singular. |
---|
| 78 | By now, the Emacs interface to Singular consists of Singular interactive |
---|
| 79 | mode only. Singular interactive mode provides a convenient front end to |
---|
| 80 | interactive Singular sessions running inside Emacs. |
---|
| 81 | In far future maybe there will be a mode for editing Singular source code |
---|
| 82 | such as libraries or procedures." |
---|
| 83 | :group 'external) |
---|
| 84 | |
---|
[f50151] | 85 | (defgroup singular-faces nil |
---|
| 86 | "Faces in Singular mode and Singular interactive mode." |
---|
| 87 | :group 'faces |
---|
| 88 | :group 'singular-interactive) |
---|
| 89 | ;;}}} |
---|
| 90 | |
---|
[1735b4e] | 91 | ;;{{{ Debugging stuff |
---|
[4701a1a] | 92 | (defvar singular-debug nil |
---|
[f50151] | 93 | "List of modes to debug or t to debug all modes. |
---|
| 94 | Currently, the following modes are supported: |
---|
| 95 | `interactive', |
---|
| 96 | `interactive-filter'.") |
---|
[4701a1a] | 97 | |
---|
[1735b4e] | 98 | (defun singular-debug-format (string) |
---|
| 99 | "Return STRING in a nicer format." |
---|
[e6de219] | 100 | (save-match-data |
---|
| 101 | (while (string-match "\n" string) |
---|
| 102 | (setq string (replace-match "^J" nil nil string))) |
---|
[1735b4e] | 103 | |
---|
[e6de219] | 104 | (if (> (length string) 16) |
---|
| 105 | (concat "<" (substring string 0 7) ">...<" (substring string -8) ">") |
---|
| 106 | (concat "<" string ">")))) |
---|
[1735b4e] | 107 | |
---|
[845cb34] | 108 | (defmacro singular-debug (mode form &optional else-form) |
---|
[4701a1a] | 109 | "Major debugging hook for singular.el. |
---|
[81da6ba] | 110 | Evaluates FORM if `singular-debug' equals t or if MODE is an element |
---|
| 111 | of `singular-debug', othwerwise ELSE-FORM." |
---|
| 112 | `(if (or (eq singular-debug t) |
---|
[a0cde2] | 113 | (memq ,mode singular-debug)) |
---|
[845cb34] | 114 | ,form |
---|
| 115 | ,else-form)) |
---|
[a0cde2] | 116 | ;;}}} |
---|
[1735b4e] | 117 | |
---|
| 118 | ;;{{{ Determining version |
---|
| 119 | (defvar singular-emacs-flavor nil |
---|
| 120 | "A symbol describing the current Emacs. |
---|
[f50151] | 121 | Currently, only Emacs \(`emacs') and XEmacs \(`xemacs') are supported.") |
---|
[1735b4e] | 122 | |
---|
| 123 | (defvar singular-emacs-major-version nil |
---|
| 124 | "An integer describing the major version of the current emacs.") |
---|
| 125 | |
---|
| 126 | (defvar singular-emacs-minor-version nil |
---|
[f50151] | 127 | "An integer describing the minor version of the current emacs.") |
---|
[1735b4e] | 128 | |
---|
[f50151] | 129 | (defun singular-fset (real-function emacs-function xemacs-function) |
---|
[1735b4e] | 130 | "Set REAL-FUNCTION to one of the functions, in dependency on Emacs flavor and version. |
---|
| 131 | Sets REAL-FUNCTION to XEMACS-FUNCTION if `singular-emacs-flavor' is |
---|
[f50151] | 132 | `xemacs', otherwise sets REAL-FUNCTION to EMACS-FUNCTION. |
---|
[1735b4e] | 133 | |
---|
[f50151] | 134 | This is not as common as it would be desirable. But it is sufficient so |
---|
| 135 | far." |
---|
[1735b4e] | 136 | (cond |
---|
| 137 | ;; XEmacs |
---|
| 138 | ((eq singular-emacs-flavor 'xemacs) |
---|
| 139 | (fset real-function xemacs-function)) |
---|
[f50151] | 140 | ;; Emacs |
---|
[1735b4e] | 141 | (t |
---|
[f50151] | 142 | (fset real-function emacs-function)))) |
---|
[1735b4e] | 143 | |
---|
| 144 | (defun singular-set-version () |
---|
| 145 | "Determine flavor, major version, and minor version of current emacs. |
---|
[f50151] | 146 | singular.el is guaranteed to run on Emacs 20.3 and XEmacs 20.3. |
---|
| 147 | It should run on newer version and on slightly older ones, too. |
---|
[1735b4e] | 148 | |
---|
[f50151] | 149 | This function is called exactly once when singular.el is loaded." |
---|
[1735b4e] | 150 | ;; get major and minor versions first |
---|
| 151 | (if (and (boundp 'emacs-major-version) |
---|
| 152 | (boundp 'emacs-minor-version)) |
---|
| 153 | (setq singular-emacs-major-version emacs-major-version |
---|
| 154 | singular-emacs-minor-version emacs-minor-version) |
---|
| 155 | (with-output-to-temp-buffer "*singular warnings*" |
---|
| 156 | (princ |
---|
| 157 | "You seem to have quite an old Emacs or XEmacs version. Some of the |
---|
| 158 | features from singular.el will not work properly. Consider upgrading to a |
---|
[845cb34] | 159 | more recent version of Emacs or XEmacs. singular.el is guaranteed to run |
---|
[f50151] | 160 | on Emacs 20.3 and XEmacs 20.3.")) |
---|
[1735b4e] | 161 | ;; assume the oldest version we support |
---|
[f50151] | 162 | (setq singular-emacs-major-version 20 |
---|
| 163 | singular-emacs-minor-version 3)) |
---|
[1735b4e] | 164 | |
---|
| 165 | ;; get flavor |
---|
| 166 | (if (string-match "XEmacs\\|Lucid" emacs-version) |
---|
| 167 | (setq singular-emacs-flavor 'xemacs) |
---|
| 168 | (setq singular-emacs-flavor 'emacs))) |
---|
| 169 | |
---|
| 170 | (singular-set-version) |
---|
| 171 | ;;}}} |
---|
[845cb34] | 172 | |
---|
[f50151] | 173 | ;;{{{ Syntax table |
---|
| 174 | (defvar singular-mode-syntax-table nil |
---|
| 175 | "Syntax table for `singular-interactive-mode' resp. `singular-mode'.") |
---|
[5322a3] | 176 | |
---|
[f50151] | 177 | (if singular-mode-syntax-table |
---|
| 178 | () |
---|
| 179 | (setq singular-mode-syntax-table (make-syntax-table)) |
---|
| 180 | ;; stolen from cc-mode.el except for back-tics which are special to Singular |
---|
| 181 | (modify-syntax-entry ?_ "_" singular-mode-syntax-table) |
---|
| 182 | (modify-syntax-entry ?\\ "\\" singular-mode-syntax-table) |
---|
| 183 | (modify-syntax-entry ?+ "." singular-mode-syntax-table) |
---|
| 184 | (modify-syntax-entry ?- "." singular-mode-syntax-table) |
---|
| 185 | (modify-syntax-entry ?= "." singular-mode-syntax-table) |
---|
| 186 | (modify-syntax-entry ?% "." singular-mode-syntax-table) |
---|
| 187 | (modify-syntax-entry ?< "." singular-mode-syntax-table) |
---|
| 188 | (modify-syntax-entry ?> "." singular-mode-syntax-table) |
---|
| 189 | (modify-syntax-entry ?& "." singular-mode-syntax-table) |
---|
| 190 | (modify-syntax-entry ?| "." singular-mode-syntax-table) |
---|
| 191 | (modify-syntax-entry ?\' "\"" singular-mode-syntax-table) |
---|
| 192 | (modify-syntax-entry ?\` "\"" singular-mode-syntax-table) |
---|
| 193 | ;; block and line-oriented comments |
---|
| 194 | (cond |
---|
| 195 | ;; Emacs |
---|
| 196 | ((eq singular-emacs-flavor 'emacs) |
---|
| 197 | (modify-syntax-entry ?/ ". 124b" singular-mode-syntax-table) |
---|
| 198 | (modify-syntax-entry ?* ". 23" singular-mode-syntax-table)) |
---|
| 199 | ;; XEmacs |
---|
| 200 | (t |
---|
| 201 | (modify-syntax-entry ?/ ". 1456" singular-mode-syntax-table) |
---|
| 202 | (modify-syntax-entry ?* ". 23" singular-mode-syntax-table))) |
---|
| 203 | (modify-syntax-entry ?\n "> b" singular-mode-syntax-table) |
---|
| 204 | (modify-syntax-entry ?\^m "> b" singular-mode-syntax-table)) |
---|
[07c212] | 205 | |
---|
[f50151] | 206 | (defun singular-mode-syntax-table-init () |
---|
| 207 | "Initialize syntax table of current buffer. |
---|
[99e62ad] | 208 | |
---|
[f50151] | 209 | This function is called at mode initialization time." |
---|
| 210 | (set-syntax-table singular-mode-syntax-table)) |
---|
| 211 | ;;}}} |
---|
[99e62ad] | 212 | |
---|
[f50151] | 213 | ;;{{{ Miscellaneous |
---|
| 214 | (defsubst singular-keep-region-active () |
---|
| 215 | "Do whatever is necessary to keep the region active in XEmacs. |
---|
| 216 | Ignore byte-compiler warnings you might see. This is not needed for |
---|
| 217 | Emacs." |
---|
| 218 | ;; XEmacs. We do not use the standard way here to test for flavor |
---|
| 219 | ;; because it is presumably faster with that test on `boundp'. |
---|
| 220 | (and (boundp 'zmacs-region-stays) |
---|
| 221 | (setq zmacs-region-stays t))) |
---|
| 222 | ;;}}} |
---|
| 223 | ;;}}} |
---|
[07c212] | 224 | |
---|
[f50151] | 225 | ;;{{{ Singular interactive mode |
---|
| 226 | ;;{{{ Customizing |
---|
[99e62ad] | 227 | |
---|
[f50151] | 228 | ;; Note: |
---|
| 229 | ;; |
---|
| 230 | ;; Some notes on Customize: |
---|
| 231 | ;; |
---|
| 232 | ;; - The documentation states that for the `:initialize' option of |
---|
| 233 | ;; `defcustom' the default value is `custom-initialize-set'. However, in |
---|
| 234 | ;; the source code of Customize `custom-initialize-reset' is used. So |
---|
| 235 | ;; better always specify the `:initialize' option explicitly. |
---|
| 236 | ;; - Customize is bad at setting buffer-local variables or properties. |
---|
| 237 | ;; This is quite natural since Customize itself uses its own buffer. So |
---|
| 238 | ;; changing buffer-local variables and properties with Customize is |
---|
| 239 | ;; possible only at a "Singular-global" level. That is, for all buffers |
---|
| 240 | ;; currently having Singular interactive mode as major mode. The function |
---|
| 241 | ;; `singular-map-buffer' helps to do such customization. |
---|
[71c107] | 242 | ;; - Important note: Customizable variables are not automatically marked as |
---|
| 243 | ;; user options. This has to be done as usual by marking them with a '*' |
---|
| 244 | ;; as first character of the documentation string. Without that, the |
---|
| 245 | ;; variables are not accessible to, for example, `set-variable'. |
---|
[f50151] | 246 | ;; |
---|
| 247 | ;; Some common customizing patterns: |
---|
| 248 | ;; |
---|
| 249 | ;; - How to customize buffer-local properties? |
---|
| 250 | ;; First, the `defcustom' itself must not set anything buffer-local since |
---|
| 251 | ;; at time of its definition (most likely) no Singular buffers will be |
---|
| 252 | ;; around. If there are Singular buffers we do not care about them. But |
---|
| 253 | ;; anyhow, at definition of the `defcustom' the global default has to be |
---|
| 254 | ;; set. Hence, the `:initialize' option should be set to |
---|
| 255 | ;; `custom-initialize-default'. |
---|
| 256 | ;; The buffer-local initialization has to be done at mode initialization |
---|
| 257 | ;; time. The global default value should then be used to set the local |
---|
| 258 | ;; properties. |
---|
| 259 | ;; At last, the function specified with the `:set' option should set the |
---|
| 260 | ;; local properties in all Singular buffers to the new, customized value. |
---|
| 261 | ;; Most likely, the function `singular-map-buffer' may be used for that. |
---|
| 262 | ;; In addition, the function should, of course, set the global value via |
---|
| 263 | ;; `set-default'. |
---|
| 264 | ;; For an example, see `singular-folding-line-move-ignore-folding'. |
---|
| 265 | ;; |
---|
| 266 | ;; - How to encapsulate other mode's global variables into Singular |
---|
| 267 | ;; interactive mode variables? |
---|
| 268 | ;; Set them always. That is, set them if the `defcustom' is evaluated |
---|
| 269 | ;; (use `custom-initialize-reset' as `:initial' function) and set them |
---|
| 270 | ;; when the Singular interactive mode variable is customized (by means |
---|
| 271 | ;; of an appropriate `:set' function). |
---|
| 272 | ;; For an example, see `singular-section-face-alist' (which does not |
---|
| 273 | ;; encapsulate another mode's variable, but Singular interactive mode's |
---|
| 274 | ;; own variable `singular-simple-sec-clear-type'). |
---|
| 275 | |
---|
| 276 | (defgroup singular-interactive nil |
---|
[71c107] | 277 | "Running interactive Singular sessions inside Emacs." |
---|
| 278 | :group 'singular |
---|
[f50151] | 279 | :group 'processes) |
---|
| 280 | |
---|
| 281 | (defgroup singular-sections-and-foldings nil |
---|
| 282 | "Sections and foldings in Singular interactive mode." |
---|
| 283 | :group 'singular-interactive) |
---|
| 284 | |
---|
| 285 | (defgroup singular-interactive-miscellaneous nil |
---|
| 286 | "Miscellaneous settings for Singular interactive mode." |
---|
| 287 | :group 'singular-interactive) |
---|
| 288 | |
---|
| 289 | (defgroup singular-demo-mode nil |
---|
| 290 | "Settings concerning Singular demo mode." |
---|
| 291 | :group 'singular-interactive) |
---|
| 292 | |
---|
| 293 | (defun singular-map-buffer (func &rest args) |
---|
| 294 | "Apply FUNC to ARGS in all existing Singular buffers. |
---|
| 295 | That is, in all buffers having Singular interactive major mode. The |
---|
| 296 | function is executed in the context of the buffer. This is a must-have for |
---|
| 297 | the customizing stuff to change buffer-local properties." |
---|
| 298 | (save-excursion |
---|
| 299 | (mapcar (function |
---|
| 300 | (lambda (buffer) |
---|
| 301 | (set-buffer buffer) |
---|
| 302 | (if (eq major-mode 'singular-interactive-mode) |
---|
| 303 | (apply func args)))) |
---|
| 304 | (buffer-list)))) |
---|
| 305 | ;;}}} |
---|
[99e62ad] | 306 | |
---|
[f50151] | 307 | ;;{{{ Comint |
---|
| 308 | |
---|
| 309 | ;; Note: |
---|
| 310 | ;; |
---|
| 311 | ;; We require Comint, but we really do not use it too much. One may argue |
---|
| 312 | ;; that this is bad since Comint is a standardized way to communicate with |
---|
| 313 | ;; external processes. One may argue further that many experienced Emacs |
---|
| 314 | ;; users are forced now to re-do their Comint customization for Singular |
---|
| 315 | ;; interactive mode. However, we believe that the intersection between |
---|
| 316 | ;; experienced Emacs users and users of Singular interactive mode is almost |
---|
| 317 | ;; empty. |
---|
| 318 | ;; |
---|
| 319 | ;; In fact, we used Comint really much in the beginning of this project. |
---|
| 320 | ;; Later during development it turned at that using Comint's input and |
---|
| 321 | ;; output processing is to inflexible and not appropriate for Singular |
---|
| 322 | ;; interactive mode with its input and output sections. So we begun to |
---|
| 323 | ;; rewrite large portions of Comint to adapt it to our needs. At some |
---|
| 324 | ;; point it came clear that it would be best to throw out Comint |
---|
| 325 | ;; alltogether, would not have been there some auxilliary functions which |
---|
| 326 | ;; are really useful but annoying to rewrite. These are, for example, the |
---|
| 327 | ;; command line history functions or the completion stuff offered by |
---|
| 328 | ;; Comint. |
---|
| 329 | ;; |
---|
| 330 | ;; Our policy with regard to these remainders of Comint is: Use the |
---|
| 331 | ;; functions to bind them to keys, but do not use them internally. |
---|
| 332 | ;; Encapsulate Comint customization into Singular interactive mode |
---|
| 333 | ;; customization. In particular, do not take care about Comint settings |
---|
| 334 | ;; which already may be present, overwrite them. Hide Comint from the |
---|
| 335 | ;; user. |
---|
| 336 | ;; |
---|
| 337 | ;; Here is how exactly we use Comint: |
---|
| 338 | ;; |
---|
| 339 | ;; - All variables necessary to use Comint's input ring are properly |
---|
| 340 | ;; initialized. One may find this in the `History' folding. |
---|
| 341 | ;; - `comint-prompt-regexp' is initialized since it is used in some |
---|
| 342 | ;; of the functions regarding input ring handling. Furthermore, its |
---|
| 343 | ;; initialization enables us to use functions as `comint-bol', etc. |
---|
| 344 | ;; Initialization is done in the `Skipping and stripping prompts ...' |
---|
| 345 | ;; folding. |
---|
| 346 | ;; - We call `comint-mode' as first step in `singular-interactive-mode'. |
---|
| 347 | ;; Most of the work done there is to initialize the local variables as |
---|
| 348 | ;; necessary. Besides that, the function does nothing that interferes |
---|
| 349 | ;; with Singular interactive mode. To be consequent we set |
---|
| 350 | ;; `comint-mode-hook' temporarily to nil when calling `comint-mode'. |
---|
| 351 | ;; - In `singular-exec', we use `comint-exec-1' to fire up the process. |
---|
| 352 | ;; Furthermore, we set `comint-ptyp' there as it is used in the signal |
---|
| 353 | ;; sending commands of Comint. All that `comint-exec-1' does is that it |
---|
| 354 | ;; sets up the process environment (it adds or modifies the setting of |
---|
| 355 | ;; the 'TERM' variable), sets the execution directory, and does some |
---|
| 356 | ;; magic with the process coding stuff. |
---|
| 357 | ;; - One more time the most important point: we do *not* use Comint's |
---|
| 358 | ;; output and input processing. In particular, we do not run any of |
---|
| 359 | ;; Comint's hooks on input or output. Anyway, we do better, don't we? |
---|
| 360 | |
---|
| 361 | (require 'comint) |
---|
[c100d9] | 362 | |
---|
| 363 | (defun singular-comint-init () |
---|
| 364 | "Initialize comint stuff for Singular interactive mode. |
---|
| 365 | |
---|
| 366 | This function is called at mode initialization time." |
---|
| 367 | (setq comint-completion-addsuffix '("/" . ""))) |
---|
[845cb34] | 368 | ;;}}} |
---|
[ef85f4] | 369 | |
---|
| 370 | ;;{{{ Font-locking |
---|
[f50151] | 371 | (defvar singular-font-lock-error-face 'singular-font-lock-error-face |
---|
| 372 | "Face name to use for Singular errors.") |
---|
| 373 | |
---|
| 374 | (defvar singular-font-lock-warning-face 'singular-font-lock-warning-face |
---|
| 375 | "Face name to use for Singular warnings.") |
---|
| 376 | |
---|
| 377 | (defvar singular-font-lock-prompt-face 'singular-font-lock-prompt-face |
---|
| 378 | "Face name to use for Singular prompts.") |
---|
| 379 | |
---|
| 380 | (defface singular-font-lock-error-face |
---|
| 381 | '((((class color)) (:foreground "Red" :bold t)) |
---|
| 382 | (t (:inverse-video t :bold t))) |
---|
| 383 | "*Font Lock mode face used to highlight Singular errors." |
---|
| 384 | :group 'singular-faces) |
---|
| 385 | |
---|
| 386 | (defface singular-font-lock-warning-face |
---|
| 387 | '((((class color)) (:foreground "OrangeRed" :bold nil)) |
---|
| 388 | (t (:inverse-video t :bold t))) |
---|
| 389 | "*Font Lock mode face used to highlight Singular warnings." |
---|
| 390 | :group 'singular-faces) |
---|
| 391 | |
---|
| 392 | (defface singular-font-lock-prompt-face |
---|
| 393 | '((((class color) (background light)) (:foreground "Blue" :bold t)) |
---|
| 394 | (((class color) (background dark)) (:foreground "LightSkyBlue" :bold t)) |
---|
| 395 | (t (:inverse-video t :bold t))) |
---|
| 396 | "*Font Lock mode face used to highlight Singular prompts." |
---|
| 397 | :group 'singular-faces) |
---|
| 398 | |
---|
| 399 | (defconst singular-font-lock-singular-types nil |
---|
| 400 | "List of Singular types.") |
---|
| 401 | |
---|
| 402 | (eval-when-compile |
---|
| 403 | (setq singular-font-lock-singular-types |
---|
| 404 | '("def" "ideal" "int" "intmat" "intvec" "link" "list" "map" "matrix" |
---|
| 405 | "module" "number" "poly" "proc" "qring" "resolution" "ring" "string" |
---|
| 406 | "vector"))) |
---|
| 407 | |
---|
| 408 | (defconst singular-interactive-font-lock-keywords-1 |
---|
[ef85f4] | 409 | '( |
---|
[f50151] | 410 | ("^\\([>.]\\) " 1 singular-font-lock-prompt-face t) |
---|
| 411 | ("^ [\\?].*" 0 singular-font-lock-error-face t) |
---|
| 412 | ("^// \\*\\*.*" 0 singular-font-lock-warning-face t) |
---|
[ef85f4] | 413 | ) |
---|
[f50151] | 414 | "Subdued level highlighting for Singular interactive mode") |
---|
[ef85f4] | 415 | |
---|
[f50151] | 416 | (defconst singular-interactive-font-lock-keywords-2 |
---|
[ef85f4] | 417 | (append |
---|
[f50151] | 418 | singular-interactive-font-lock-keywords-1 |
---|
| 419 | (eval-when-compile |
---|
| 420 | (list |
---|
| 421 | (cons |
---|
| 422 | (concat "\\<" (regexp-opt singular-font-lock-singular-types t) "\\>") |
---|
| 423 | 'font-lock-type-face)))) |
---|
| 424 | "Medium level highlighting for Singular interactive mode") |
---|
| 425 | |
---|
| 426 | (defconst singular-interactive-font-lock-keywords-3 |
---|
[b8d197] | 427 | (append |
---|
[f50151] | 428 | singular-interactive-font-lock-keywords-2 |
---|
[ef85f4] | 429 | '( |
---|
[6e3bdda] | 430 | ;; note: we use font-lock-reference-face here even Emacs says that |
---|
| 431 | ;; this face is obsolete and suggests to use font-lock-constant-face, |
---|
| 432 | ;; since XEmacs20/21 does not know the constant-face but the |
---|
| 433 | ;; reference-face. |
---|
| 434 | ("^ [\\?].*`\\(\\sw\\sw+;?\\)`" 1 font-lock-reference-face t) |
---|
[ef85f4] | 435 | )) |
---|
[f50151] | 436 | "Gaudy level highlighting for Singular interactive mode.") |
---|
| 437 | |
---|
| 438 | (defconst singular-interactive-font-lock-keywords singular-interactive-font-lock-keywords-1 |
---|
| 439 | "Default highlighting for Singular interactive mode.") |
---|
| 440 | |
---|
| 441 | (defconst singular-interactive-font-lock-defaults |
---|
| 442 | '((singular-interactive-font-lock-keywords |
---|
| 443 | singular-interactive-font-lock-keywords-1 |
---|
| 444 | singular-interactive-font-lock-keywords-2 |
---|
| 445 | singular-interactive-font-lock-keywords-3) |
---|
| 446 | ;; KEYWORDS-ONLY (do not fontify strings & comments if non-nil) |
---|
| 447 | nil |
---|
| 448 | ;; CASE-FOLD (ignore case if non-nil) |
---|
| 449 | nil |
---|
| 450 | ;; SYNTAX-ALIST (add this to Font Lock's syntax table) |
---|
| 451 | ((?_ . "w")) |
---|
| 452 | ;; SYNTAX-BEGIN |
---|
| 453 | singular-section-goto-beginning) |
---|
| 454 | "Default expressions to highlight in Singular interactive mode.") |
---|
| 455 | |
---|
| 456 | (defun singular-interactive-font-lock-init () |
---|
| 457 | "Initialize Font Lock mode for Singular interactive mode. |
---|
| 458 | |
---|
| 459 | For XEmacs, this function is called exactly once when singular.el is |
---|
| 460 | loaded. |
---|
| 461 | For Emacs, this function is called at mode initialization time." |
---|
| 462 | (cond |
---|
| 463 | ;; Emacs |
---|
| 464 | ((eq singular-emacs-flavor 'emacs) |
---|
| 465 | (singular-debug 'interactive (message "Setting up Font Lock mode for Emacs")) |
---|
| 466 | (set (make-local-variable 'font-lock-defaults) |
---|
| 467 | singular-interactive-font-lock-defaults)) |
---|
| 468 | ;; XEmacs |
---|
| 469 | ((eq singular-emacs-flavor 'xemacs) |
---|
| 470 | (singular-debug 'interactive (message "Setting up Font Lock mode for XEmacs")) |
---|
| 471 | (put 'singular-interactive-mode |
---|
| 472 | 'font-lock-defaults singular-interactive-font-lock-defaults)))) |
---|
| 473 | |
---|
| 474 | ;; XEmacs Font Lock mode initialization |
---|
| 475 | (cond |
---|
[ef85f4] | 476 | ;; XEmacs |
---|
| 477 | ((eq singular-emacs-flavor 'xemacs) |
---|
[f50151] | 478 | (singular-interactive-font-lock-init))) |
---|
[4701a1a] | 479 | ;;}}} |
---|
| 480 | |
---|
[f50151] | 481 | ;;{{{ Key map |
---|
| 482 | (defvar singular-interactive-mode-map nil |
---|
[4701a1a] | 483 | "Key map to use in Singular interactive mode.") |
---|
| 484 | |
---|
[e6de219] | 485 | (if singular-interactive-mode-map |
---|
| 486 | () |
---|
[f50151] | 487 | ;; create empty keymap first |
---|
[e6de219] | 488 | (cond |
---|
| 489 | ;; Emacs |
---|
| 490 | ((eq singular-emacs-flavor 'emacs) |
---|
[f50151] | 491 | (setq singular-interactive-mode-map (make-sparse-keymap))) |
---|
[e6de219] | 492 | ;; XEmacs |
---|
| 493 | (t |
---|
| 494 | (setq singular-interactive-mode-map (make-keymap)) |
---|
| 495 | (set-keymap-name singular-interactive-mode-map |
---|
| 496 | 'singular-interactive-mode-map))) |
---|
[ef85f4] | 497 | |
---|
[71c107] | 498 | ;; global settings |
---|
[9ad353] | 499 | (define-key help-map [?\C-s] 'singular-help) |
---|
[f50151] | 500 | |
---|
[71c107] | 501 | ;; settings for `singular-interactive-map' |
---|
| 502 | (substitute-key-definition 'beginning-of-line 'singular-beginning-of-line |
---|
| 503 | singular-interactive-mode-map global-map) |
---|
[f50151] | 504 | |
---|
[9ad353] | 505 | (define-key singular-interactive-mode-map "\t" 'singular-dynamic-complete) |
---|
| 506 | (define-key singular-interactive-mode-map [?\C-m] 'singular-send-or-copy-input) |
---|
| 507 | (define-key singular-interactive-mode-map [?\C-l] 'singular-recenter) |
---|
[f50151] | 508 | |
---|
[71c107] | 509 | ;; Comint functions |
---|
[9ad353] | 510 | (define-key singular-interactive-mode-map [?\M-r] 'comint-previous-matching-input) |
---|
| 511 | (define-key singular-interactive-mode-map [?\M-s] 'comint-next-matching-input) |
---|
[f50151] | 512 | |
---|
[71c107] | 513 | ;; C-c prefix |
---|
[9ad353] | 514 | (define-key singular-interactive-mode-map [?\C-c ?\C-t] 'singular-toggle-truncate-lines) |
---|
[f50151] | 515 | |
---|
[9ad353] | 516 | (define-key singular-interactive-mode-map [?\C-c ?\C-f] 'singular-folding-toggle-fold-at-point-or-all) |
---|
| 517 | (define-key singular-interactive-mode-map [?\C-c ?\C-o] 'singular-folding-toggle-fold-latest-output) |
---|
| 518 | (define-key singular-interactive-mode-map [?\C-c ?\C-w] 'singular-section-kill) |
---|
[71c107] | 519 | |
---|
[9ad353] | 520 | (define-key singular-interactive-mode-map [?\C-c ?\C-d] 'singular-demo-load) |
---|
| 521 | (define-key singular-interactive-mode-map [?\C-c ?\C-l] 'singular-load-library) |
---|
| 522 | (define-key singular-interactive-mode-map [(control c) (<)] 'singular-load-file) |
---|
[71c107] | 523 | |
---|
[9ad353] | 524 | (define-key singular-interactive-mode-map [?\C-c ?\C-r] 'singular-restart) |
---|
[fffd22] | 525 | (define-key singular-interactive-mode-map [?\C-c ?\$] 'singular-exit-singular)) |
---|
[71c107] | 526 | |
---|
| 527 | (defun singular-cursor-key-model-set (key-model) |
---|
| 528 | "Set keys according to KEY-MODEL. |
---|
| 529 | KEY-MODEL should be one of the valid values of `singular-cursor-key-model'." |
---|
| 530 | ;; convert symbols to list |
---|
| 531 | (cond ((eq key-model 'emacs) |
---|
| 532 | (setq key-model '(cursor cursor history))) |
---|
| 533 | ((eq key-model 'terminal) |
---|
| 534 | (setq key-model '(history history cursor)))) |
---|
| 535 | |
---|
| 536 | ;; work through list |
---|
| 537 | (mapcar (function (lambda (spec) |
---|
| 538 | (let ((key-description (nth 0 spec)) |
---|
| 539 | (prev-key (nth 1 spec)) |
---|
| 540 | (next-key (nth 2 spec))) |
---|
| 541 | (cond ((eq key-description 'cursor) |
---|
| 542 | (define-key singular-interactive-mode-map prev-key 'previous-line) |
---|
| 543 | (define-key singular-interactive-mode-map next-key 'next-line)) |
---|
| 544 | ((eq key-description 'history) |
---|
| 545 | (define-key singular-interactive-mode-map prev-key 'comint-previous-input) |
---|
| 546 | (define-key singular-interactive-mode-map next-key 'comint-next-input)) |
---|
| 547 | (t |
---|
| 548 | (define-key singular-interactive-mode-map prev-key nil) |
---|
| 549 | (define-key singular-interactive-mode-map next-key nil)))))) |
---|
| 550 | |
---|
| 551 | ;; here is where list position are mapped to keys |
---|
| 552 | (list (list (nth 0 key-model) [up] [down]) |
---|
| 553 | (list (nth 1 key-model) [?\C-p] [?\C-n]) |
---|
| 554 | (list (nth 2 key-model) [?\M-p] [?\M-n])))) |
---|
| 555 | |
---|
| 556 | (defcustom singular-cursor-key-model 'emacs |
---|
| 557 | "*Keys to use for cursor movement and history access, respectively. |
---|
| 558 | An experienced Emacs user would prefer setting `singular-cursor-key-model' |
---|
| 559 | to `emacs'. This means that C-p, C-n, and the cursor keys move the cursor, |
---|
| 560 | whereas M-p and M-n scroll through the history of Singular commands. |
---|
[f50151] | 561 | |
---|
| 562 | On the other hand, an user used to running Singular in a, say, xterm, would |
---|
[71c107] | 563 | prefer setting `singular-cursor-key-model' to `terminal'. This means that |
---|
| 564 | C-p, C-n, and the cursor keys scroll through the history of Singular |
---|
| 565 | commands, whereas M-p and M-n move the cursor. |
---|
| 566 | |
---|
| 567 | For those who do not like neither standard setting, there is the |
---|
| 568 | possibility to set this variable to a list of three elements where |
---|
| 569 | - the first element specifies the key bindings for the cursor keys, |
---|
| 570 | - the second element specifies the key bindings for C-p and C-n, and |
---|
| 571 | - the third element specifies the key bindings for M-p and M-n. |
---|
| 572 | Each list element should be one of |
---|
| 573 | - `cursor', meaning that the corresponding keys are bound to cursor movement, |
---|
| 574 | - `history', meaning that the corresponding keys are bound to history access, |
---|
| 575 | or |
---|
| 576 | - nil, meaning that the corresponding keys are not bound at all. |
---|
[f50151] | 577 | |
---|
| 578 | Changing this variable has an immediate effect only if one uses |
---|
| 579 | \\[customize] to do so." |
---|
[71c107] | 580 | :type '(choice (const :tag "Emacs-like" emacs) |
---|
| 581 | (const :tag "Terminal-like" terminal) |
---|
| 582 | (list :tag "User-defined" |
---|
| 583 | (choice :format "Cursor keys: %[Value Menu%] %v" |
---|
| 584 | :value cursor |
---|
| 585 | (const :tag "Cursor movement" cursor) |
---|
| 586 | (const :tag "History access" history) |
---|
| 587 | (const :tag "No binding" nil)) |
---|
| 588 | (choice :format "C-p, C-n: %[Value Menu%] %v" |
---|
| 589 | :value cursor |
---|
| 590 | (const :tag "Cursor movement" cursor) |
---|
| 591 | (const :tag "History access" history) |
---|
| 592 | (const :tag "No binding" nil)) |
---|
| 593 | (choice :format "M-p, M-n: %[Value Menu%] %v" |
---|
| 594 | :value history |
---|
| 595 | (const :tag "Cursor movement" cursor) |
---|
| 596 | (const :tag "History access" history) |
---|
| 597 | (const :tag "No binding" nil)))) |
---|
| 598 | :initialize 'custom-initialize-reset |
---|
[f50151] | 599 | :set (function |
---|
| 600 | (lambda (var value) |
---|
[71c107] | 601 | (singular-cursor-key-model-set value) |
---|
[f50151] | 602 | (set-default var value))) |
---|
| 603 | :group 'singular-interactive-miscellaneous) |
---|
| 604 | |
---|
| 605 | (defun singular-interactive-mode-map-init () |
---|
| 606 | "Initialize key map for Singular interactive mode. |
---|
| 607 | |
---|
| 608 | This function is called at mode initialization time." |
---|
| 609 | (use-local-map singular-interactive-mode-map)) |
---|
| 610 | ;;}}} |
---|
| 611 | |
---|
| 612 | ;;{{{ Menus and logos |
---|
[02cc28] | 613 | (defvar singular-interactive-mode-menu-1 nil |
---|
| 614 | "NOT READY [docu]") |
---|
| 615 | |
---|
| 616 | (defvar singular-interactive-mode-menu-2 nil |
---|
| 617 | "NOT READY [docu]") |
---|
| 618 | |
---|
[c100d9] | 619 | (defconst singular-menu-initial-library-menu |
---|
| 620 | '(["other..." (singular-load-library t) t]) |
---|
| 621 | "Menu definition for the inital library sub menu. |
---|
| 622 | This should be a list of vectors.") |
---|
| 623 | |
---|
| 624 | (defun singular-menu-install-libraries () |
---|
| 625 | "Update the singular command menu with libraries. |
---|
| 626 | Steps through the alist `singular-standard-libraries-alist' and for |
---|
| 627 | each entry add a new menu element in the submenu |
---|
| 628 | (\"Commands\" \"libraries\")." ;" font-lock-trick |
---|
| 629 | (singular-debug 'interactive (message "Installing library menu")) |
---|
| 630 | (let ((libs singular-standard-libraries-alist) |
---|
| 631 | current |
---|
| 632 | (menudef (append '("---") singular-menu-initial-library-menu))) |
---|
| 633 | (while libs |
---|
| 634 | (setq current (car (car libs))) |
---|
| 635 | (setq menudef (append (list (vector current |
---|
| 636 | (list 'singular-load-library nil current) |
---|
| 637 | t)) |
---|
| 638 | menudef)) |
---|
| 639 | (setq libs (cdr libs))) |
---|
[9ad353] | 640 | (easy-menu-change '("Commands") "Libraries" menudef))) |
---|
[c100d9] | 641 | |
---|
| 642 | (defun singular-menu-deinstall-libraries () |
---|
| 643 | "Initialize library submenu from singular command menu. |
---|
[9ad353] | 644 | Sets the submenu (\"Commands\" \"Libraries\") to the value of |
---|
[c100d9] | 645 | `singular-menu-initial-library-menu'." |
---|
| 646 | (singular-debug 'interactive |
---|
| 647 | (message "Removing libraries from menu")) |
---|
[9ad353] | 648 | (easy-menu-change '("Commands") "Libraries" singular-menu-initial-library-menu)) |
---|
[c100d9] | 649 | |
---|
[b7b6c7f] | 650 | ;; For some reasons emacs inserts new menus in the oppsite order. |
---|
| 651 | ;; Defining menu-2 prior to menu-1 will result in the follwoing menu: |
---|
| 652 | ;; Singular Commands |
---|
| 653 | ;; That's what we want. So DO NOT exchange both (or ..) statements! |
---|
[e5e79a] | 654 | (or singular-interactive-mode-menu-2 |
---|
[02cc28] | 655 | (easy-menu-define |
---|
| 656 | singular-interactive-mode-menu-2 |
---|
| 657 | singular-interactive-mode-map "" |
---|
[e5e79a] | 658 | (list |
---|
| 659 | "Commands" |
---|
[6e3bdda] | 660 | ["Fold/Unfold Latest Output" singular-folding-toggle-fold-latest-output t] |
---|
| 661 | ["Fold/Unfold At Point" singular-folding-toggle-fold-at-point-or-all t] |
---|
[9ad353] | 662 | ["Fold All Output" singular-folding-fold-all-output t] |
---|
| 663 | ["Unfold All Output" singular-folding-unfold-all-output t] |
---|
[4cb4e3] | 664 | "---" |
---|
| 665 | ["Truncate Lines" singular-toggle-truncate-lines |
---|
| 666 | :style toggle :selected truncate-lines] |
---|
[c100d9] | 667 | "--" |
---|
[9ad353] | 668 | (append |
---|
| 669 | '("Libraries") |
---|
| 670 | singular-menu-initial-library-menu) |
---|
| 671 | ["Load File..." singular-load-file t] |
---|
[e5e79a] | 672 | "---" |
---|
[6e3bdda] | 673 | ["Load Demo..." singular-demo-load (or singular-demo-exit-on-load |
---|
| 674 | (not singular-demo-mode))] |
---|
[9ad353] | 675 | ["Exit Demo" singular-demo-exit singular-demo-mode] |
---|
[e5e79a] | 676 | ))) |
---|
[02cc28] | 677 | |
---|
[b7b6c7f] | 678 | (or singular-interactive-mode-menu-1 |
---|
| 679 | (easy-menu-define singular-interactive-mode-menu-1 |
---|
| 680 | singular-interactive-mode-map "" |
---|
| 681 | '("Singular" |
---|
[9ad353] | 682 | ["Start Default" singular t] |
---|
| 683 | ["Start..." singular-other t] |
---|
| 684 | ["Restart" singular-restart t] |
---|
| 685 | "---" |
---|
| 686 | ["Exit" singular-exit-singular t] |
---|
| 687 | "---" |
---|
| 688 | ["Preferences" (customize-group 'singular-interactive) t] |
---|
[6e3bdda] | 689 | ["Singular Help" singular-help t]))) |
---|
[9ad353] | 690 | |
---|
| 691 | (defun customize-singular-interactive () |
---|
| 692 | (interactive) |
---|
| 693 | (customize-group 'singular-interactive)) |
---|
[4701a1a] | 694 | |
---|
[f50151] | 695 | (defun singular-interactive-mode-menu-init () |
---|
| 696 | "Initialize menus for Singular interactive mode. |
---|
[b8d197] | 697 | |
---|
[f50151] | 698 | This function is called at mode initialization time." |
---|
[b7b6c7f] | 699 | ;; Remove any potential menu which comint-mode might has added. |
---|
| 700 | (cond |
---|
| 701 | ;; Emacs |
---|
| 702 | ((eq singular-emacs-flavor 'emacs) |
---|
| 703 | ;; Note that easy-menu-remove is a nop in emacs. |
---|
| 704 | (define-key comint-mode-map [menu-bar signals] nil) |
---|
| 705 | (define-key comint-mode-map [menu-bar inout] nil) |
---|
| 706 | (define-key comint-mode-map [menu-bar completion] nil)) |
---|
| 707 | ;;Xemacs |
---|
| 708 | (t |
---|
[fffd22] | 709 | (easy-menu-remove '("Singular")) |
---|
[b7b6c7f] | 710 | (easy-menu-remove '("Comint1")) ; XEmacs 20 |
---|
| 711 | (easy-menu-remove '("Comint2")) ; XEmacs 20 |
---|
| 712 | (easy-menu-remove '("History")) ; XEmacs 20 |
---|
| 713 | (easy-menu-remove '("Complete")) ; XEmacs 21 |
---|
| 714 | (easy-menu-remove '("In/Out")) ; XEmacs 21 |
---|
| 715 | (easy-menu-remove '("Signals")))) ; XEmacs 21 |
---|
| 716 | |
---|
[e5e79a] | 717 | ;; Note: easy-menu-add is not necessary in emacs, since the menu |
---|
| 718 | ;; is added automatically with the keymap. |
---|
| 719 | ;; See help on `easy-menu-add' |
---|
| 720 | (easy-menu-add singular-interactive-mode-menu-1) |
---|
| 721 | (easy-menu-add singular-interactive-mode-menu-2)) |
---|
[a0cde2] | 722 | ;;}}} |
---|
| 723 | |
---|
[71c107] | 724 | ;;{{{ Skipping and stripping prompts and whitespace and other things |
---|
[213e96] | 725 | |
---|
| 726 | ;; Note: |
---|
| 727 | ;; |
---|
[71c107] | 728 | ;; Most of these functions handle prompt recognition, prompt skipping, |
---|
| 729 | ;; prompt stripping, and so on. It turned out that it would be very |
---|
| 730 | ;; inefficient to use one generic regular expression to do so. Hence, we |
---|
| 731 | ;; decided to hardcode the prompt skipping and stripping in an API. If one |
---|
| 732 | ;; decides to use some other prompt the whole API has to be changed. |
---|
| 733 | ;; Hopefully, the Singular prompt does not change in near future ... |
---|
| 734 | ;; |
---|
| 735 | ;; In addition to the API, the Comint mode variable `comint-mode-regexp' is |
---|
| 736 | ;; set on initialization of Singular interactive mode. Singular |
---|
| 737 | ;; interactive mode seems to do quite well without that, but for safety the |
---|
| 738 | ;; variable is set nonetheless. |
---|
[213e96] | 739 | |
---|
[71c107] | 740 | (defsubst singular-prompt-skip-forward () |
---|
| 741 | "Skip forward over prompts." |
---|
| 742 | (if (looking-at "\\([>.] \\)+") |
---|
| 743 | (goto-char (match-end 0)))) |
---|
| 744 | |
---|
| 745 | (defsubst singular-prompt-skip-backward () |
---|
| 746 | "Skip backward over prompts." |
---|
| 747 | ;; is that really the simplest and fastest method? The problem is that |
---|
| 748 | ;; `re-search-backward' is not greedy so on an regexp as "\\([>.] \\)+" |
---|
| 749 | ;; it stops right after the first occurence of the sub-expression. |
---|
| 750 | ;; Anyway, the `(- (point) 2)' expression is OK, even at bob. |
---|
| 751 | (while (re-search-backward "[>.] " (- (point) 2) t))) |
---|
| 752 | |
---|
| 753 | (defun singular-prompt-remove-string (string) |
---|
| 754 | "Remove all prompts from STRING." |
---|
| 755 | (while (string-match "^\\([>.] \\)+" string) |
---|
| 756 | (setq string (replace-match "" t t string))) |
---|
| 757 | string) |
---|
| 758 | |
---|
| 759 | (defun singular-prompt-remove-region (beg end) |
---|
| 760 | "Remove all superfluous prompts from region between BEG and END. |
---|
| 761 | Removes only sequences of prompts that start at beginning of line. Removes |
---|
| 762 | all but the last prompt of a sequence if that sequence ends at END, |
---|
| 763 | otherwise removes all prompts. |
---|
| 764 | The region between BEG and END should be accessible. BEG should be less |
---|
| 765 | than or equal to END. |
---|
| 766 | Leaves point at the position of the last sequence of prompts which has been |
---|
| 767 | deleted or at BEG if nothing has been deleted." |
---|
| 768 | ;; we cannot exclude this case, I think |
---|
| 769 | (if (/= beg end) |
---|
| 770 | ;; that's a nice trick to keep the last prompt if it ends at END: we |
---|
| 771 | ;; set `(1- END)' as search limit. Since BEG /= END there can be no |
---|
| 772 | ;; problems with the `1-'. |
---|
| 773 | (let ((end (copy-marker (1- end)))) |
---|
| 774 | (goto-char beg) |
---|
| 775 | (while (re-search-forward "^\\([>.] \\)+" end t) |
---|
| 776 | (delete-region (match-beginning 0) (match-end 0))) |
---|
| 777 | (set-marker end nil)))) |
---|
| 778 | |
---|
| 779 | (defun singular-prompt-remove-filter (beg end simple-sec-start) |
---|
| 780 | "Remove all superfluous prompts from text inserted into buffer." |
---|
| 781 | (cond (;; if a new simple section has been created remove all |
---|
| 782 | ;; prompts from that simple section |
---|
| 783 | simple-sec-start |
---|
| 784 | (singular-prompt-remove-region simple-sec-start end)) |
---|
| 785 | (;; if no simple section has been created check whether maybe the |
---|
| 786 | ;; region between beg and end consists of prompts only. This in |
---|
| 787 | ;; case that the user issued a command that did not output any |
---|
| 788 | ;; text. |
---|
| 789 | (and (goto-char beg) |
---|
| 790 | (re-search-forward "\\([>.] \\)+" end t) |
---|
| 791 | (= (match-end 0) end)) |
---|
| 792 | (singular-prompt-remove-region (progn (beginning-of-line) (point)) |
---|
| 793 | end)))) |
---|
| 794 | |
---|
| 795 | (defun singular-white-space-strip (string &optional trailing leading) |
---|
| 796 | "Strip off trailing or leading whitespace from STRING. |
---|
| 797 | Strips off trailing whitespace if optional argument TRAILING is non-nil. |
---|
| 798 | Strips off leading whitespace if optional argument LEADING is non-nil." |
---|
| 799 | (let (beg end) |
---|
[213e96] | 800 | (and leading |
---|
[71c107] | 801 | (string-match "\\`[ \t\n\r\f]+" string) |
---|
[213e96] | 802 | (setq beg (match-end 0))) |
---|
| 803 | (and trailing |
---|
[71c107] | 804 | (string-match "[ \t\n\r\f]+\\'" string) |
---|
[213e96] | 805 | (setq end (match-beginning 0))) |
---|
[71c107] | 806 | (if (or beg end) |
---|
| 807 | (substring string (or beg 0) (or end (length string))) |
---|
| 808 | string))) |
---|
[bd4207] | 809 | |
---|
[71c107] | 810 | (defconst singular-comint-prompt-regexp "^\\([>.] \\)+" |
---|
| 811 | "Regexp to match prompt patterns in Singular. |
---|
| 812 | This variable is used to initialize `comint-prompt-regexp' when Singular |
---|
| 813 | interactive mode starts up. It is not used in Singular interactive mode |
---|
| 814 | itself! One should refer to the source code for more information on how to |
---|
| 815 | adapt Singular interactive mode to some other prompt.") |
---|
[f50151] | 816 | |
---|
[71c107] | 817 | (defun singular-prompt-init () |
---|
| 818 | "Initialize prompt skipping and stripping for Singular interactive mode. |
---|
[f50151] | 819 | |
---|
[71c107] | 820 | This function is called at mode initialization time." |
---|
| 821 | ;; remove superfluous prompts in singular output |
---|
| 822 | (add-hook 'singular-post-output-filter-functions 'singular-prompt-remove-filter nil t) |
---|
[f50151] | 823 | |
---|
[71c107] | 824 | ;; some relict from Comint mode |
---|
| 825 | (setq comint-prompt-regexp singular-comint-prompt-regexp)) |
---|
[213e96] | 826 | ;;}}} |
---|
| 827 | |
---|
[f50151] | 828 | ;;{{{ Miscellaneous |
---|
| 829 | |
---|
| 830 | ;; Note: |
---|
| 831 | ;; |
---|
| 832 | ;; We assume a one-to-one correspondence between Singular buffers and |
---|
| 833 | ;; Singular processes. We always have (equal buffer-name (concat "*" |
---|
| 834 | ;; process-name "*")). |
---|
| 835 | |
---|
| 836 | (defsubst singular-buffer-name-to-process-name (buffer-name) |
---|
| 837 | "Create the process name for BUFFER-NAME. |
---|
| 838 | The process name is the buffer name with surrounding `*' stripped off." |
---|
| 839 | (substring buffer-name 1 -1)) |
---|
| 840 | |
---|
| 841 | (defsubst singular-process-name-to-buffer-name (process-name) |
---|
| 842 | "Create the buffer name for PROCESS-NAME. |
---|
| 843 | The buffer name is the process name with surrounding `*'." |
---|
| 844 | (concat "*" process-name "*")) |
---|
| 845 | |
---|
| 846 | (defsubst singular-run-hook-with-arg-and-value (hook value) |
---|
| 847 | "Call functions on HOOK. |
---|
| 848 | Provides argument VALUE to the functions. If a function returns a non-nil |
---|
| 849 | value it replaces VALUE as new argument to the remaining functions. |
---|
| 850 | Returns final VALUE." |
---|
| 851 | (while hook |
---|
| 852 | (setq value (or (funcall (car hook) value) value) |
---|
| 853 | hook (cdr hook))) |
---|
| 854 | value) |
---|
| 855 | |
---|
| 856 | (defsubst singular-process (&optional no-error) |
---|
| 857 | "Return process of current buffer. |
---|
| 858 | If no process is active this function silently returns nil if optional |
---|
| 859 | argument NO-ERROR is non-nil, otherwise it throws an error." |
---|
| 860 | (cond ((get-buffer-process (current-buffer))) |
---|
| 861 | (no-error nil) |
---|
| 862 | (t (error "No Singular running in this buffer")))) |
---|
| 863 | |
---|
| 864 | (defsubst singular-process-mark (&optional no-error) |
---|
| 865 | "Return process mark of current buffer. |
---|
| 866 | If no process is active this function silently returns nil if optional |
---|
| 867 | argument NO-ERROR is non-nil, otherwise it throws an error." |
---|
| 868 | (let ((process (singular-process no-error))) |
---|
| 869 | (and process |
---|
| 870 | (process-mark process)))) |
---|
| 871 | |
---|
| 872 | (defun singular-time-stamp-difference (new-time-stamp old-time-stamp) |
---|
| 873 | "Return the number of seconds between NEW-TIME-STAMP and OLD-TIME-STAMP. |
---|
| 874 | Both NEW-TIME-STAMP and OLD-TIME-STAMP should be in the format |
---|
| 875 | that is returned, for example, by `current-time'. |
---|
| 876 | Does not return a difference larger than 2^17 seconds." |
---|
| 877 | (let ((high-difference (min 1 (- (car new-time-stamp) (car old-time-stamp)))) |
---|
| 878 | (low-difference (- (cadr new-time-stamp) (cadr old-time-stamp)))) |
---|
| 879 | (+ (* high-difference 131072) low-difference))) |
---|
[71c107] | 880 | |
---|
| 881 | (defun singular-error (&rest message-args) |
---|
| 882 | "Apply `message' on MESSAGE-ARGS and do a `ding'. |
---|
| 883 | This function should be used instead of `error' in hooks where calling |
---|
| 884 | `error' is not a good idea." |
---|
| 885 | (apply 'message message-args) |
---|
| 886 | (ding)) |
---|
| 887 | |
---|
| 888 | (defun singular-pop-to-buffer (same-window &rest pop-to-buffer-args) |
---|
| 889 | "Pop to buffer in same or other window. |
---|
| 890 | Pops to buffer in same window if SAME-WINDOW equals t. Pops to buffer in |
---|
| 891 | other window if SAME-WINDOW equals nil. If SAME-WINDOW equals neither t |
---|
| 892 | nor nil the default behaviour of `pop-to-buffer' is used. The rest of the |
---|
| 893 | arguments is passed unchanged to `pop-to-buffer'." |
---|
| 894 | (let ((same-window-buffer-names |
---|
| 895 | (cond |
---|
| 896 | ((null same-window) |
---|
| 897 | nil) |
---|
| 898 | ((eq same-window t) |
---|
| 899 | (let* ((buffer-or-name (car pop-to-buffer-args)) |
---|
| 900 | (buffer-name (if (bufferp buffer-or-name) |
---|
| 901 | (buffer-name buffer-or-name) |
---|
| 902 | buffer-or-name))) |
---|
| 903 | (list buffer-name))) |
---|
| 904 | (t |
---|
| 905 | same-window-buffer-names)))) |
---|
| 906 | (apply 'pop-to-buffer pop-to-buffer-args))) |
---|
[f50151] | 907 | ;;}}} |
---|
| 908 | |
---|
| 909 | ;;{{{ Miscellaneous interactive |
---|
| 910 | (defun singular-recenter (&optional arg) |
---|
| 911 | "Center point in window and redisplay frame. With ARG, put point on line ARG. |
---|
| 912 | The desired position of point is always relative to the current window. |
---|
| 913 | Just C-u as prefix means put point in the center of the window. |
---|
| 914 | If ARG is omitted or nil, erases the entire frame and then redraws with |
---|
| 915 | point in the center of the current window. |
---|
| 916 | Scrolls window to the left margin and moves point to beginning of line." |
---|
| 917 | (interactive "P") |
---|
| 918 | (singular-reposition-point-and-window) |
---|
| 919 | (recenter arg)) |
---|
| 920 | |
---|
| 921 | (defun singular-reposition-point-and-window () |
---|
| 922 | "Scroll window to the left margin and move point to beginning of line." |
---|
| 923 | (interactive) |
---|
| 924 | (set-window-hscroll (selected-window) 0) |
---|
| 925 | (move-to-column 0) |
---|
| 926 | ;; be careful where to place point |
---|
| 927 | (singular-prompt-skip-forward)) |
---|
| 928 | |
---|
| 929 | (defun singular-toggle-truncate-lines () |
---|
| 930 | "Toggle `truncate-lines'. |
---|
| 931 | A non-nil value of `truncate-lines' means do not display continuation |
---|
| 932 | lines\; give each line of text one screen line. |
---|
| 933 | Repositions window and point after toggling `truncate-lines'." |
---|
| 934 | (interactive) |
---|
| 935 | (setq truncate-lines (not truncate-lines)) |
---|
| 936 | ;; reposition so that user does not get confused |
---|
[71c107] | 937 | (singular-reposition-point-and-window) |
---|
| 938 | ;; avoid calling `recenter' since it changes window layout more than |
---|
| 939 | ;; necessary |
---|
| 940 | (redraw-frame (selected-frame))) |
---|
[f50151] | 941 | |
---|
| 942 | ;; this is not a buffer-local variable even if at first glance it seems |
---|
| 943 | ;; that it should be one. But if one changes buffer the contents of this |
---|
| 944 | ;; variable becomes irrelevant since the last command is no longer a |
---|
| 945 | ;; horizontal scroll command. The same is true for the initial value, so |
---|
| 946 | ;; we set it to nil. |
---|
| 947 | (defvar singular-scroll-previous-amount nil |
---|
| 948 | "Amount of previous horizontal scroll command.") |
---|
| 949 | |
---|
| 950 | (defun singular-scroll-right (&optional scroll-amount) |
---|
| 951 | "Scroll selected window SCROLL-AMOUNT columns right. |
---|
| 952 | SCROLL-AMOUNT defaults to amount of previous horizontal scroll command. If |
---|
| 953 | the command immediately preceding this command has not been a horizontal |
---|
| 954 | scroll command SCROLL-AMOUNT defaults to window width minus 2. |
---|
| 955 | Moves point to leftmost visible column." |
---|
| 956 | (interactive "P") |
---|
| 957 | |
---|
| 958 | ;; get amount to scroll |
---|
| 959 | (setq singular-scroll-previous-amount |
---|
| 960 | (cond (scroll-amount (prefix-numeric-value scroll-amount)) |
---|
| 961 | ((eq last-command 'singular-scroll-horizontal) |
---|
| 962 | singular-scroll-previous-amount) |
---|
| 963 | (t (- (frame-width) 2))) |
---|
| 964 | this-command 'singular-scroll-horizontal) |
---|
| 965 | |
---|
| 966 | ;; scroll |
---|
| 967 | (scroll-right singular-scroll-previous-amount) |
---|
| 968 | (move-to-column (window-hscroll)) |
---|
| 969 | ;; be careful where to place point. But what if `(current-column)' |
---|
| 970 | ;; equals, say, one? Well, we simply do not care about that case. |
---|
| 971 | ;; Should not happen to often. |
---|
| 972 | (if (eq (current-column) 0) |
---|
| 973 | (singular-prompt-skip-forward))) |
---|
| 974 | |
---|
| 975 | (defun singular-scroll-left (&optional scroll-amount) |
---|
| 976 | "Scroll selected window SCROLL-AMOUNT columns left. |
---|
| 977 | SCROLL-AMOUNT defaults to amount of previous horizontal scroll command. If |
---|
| 978 | the command immediately preceding this command has not been a horizontal |
---|
| 979 | scroll command SCROLL-AMOUNT defaults to window width minus 2. |
---|
| 980 | Moves point to leftmost visible column." |
---|
| 981 | (interactive "P") |
---|
| 982 | |
---|
| 983 | ;; get amount to scroll |
---|
| 984 | (setq singular-scroll-previous-amount |
---|
| 985 | (cond (scroll-amount (prefix-numeric-value scroll-amount)) |
---|
| 986 | ((eq last-command 'singular-scroll-horizontal) |
---|
| 987 | singular-scroll-previous-amount) |
---|
| 988 | (t (- (frame-width) 2))) |
---|
| 989 | this-command 'singular-scroll-horizontal) |
---|
| 990 | |
---|
| 991 | ;; scroll |
---|
| 992 | (scroll-left singular-scroll-previous-amount) |
---|
| 993 | (move-to-column (window-hscroll)) |
---|
| 994 | ;; be careful where to place point. But what if `(current-column)' |
---|
| 995 | ;; equals, say, one? Well, we simply do not care about that case. |
---|
| 996 | ;; Should not happen to often. |
---|
| 997 | (if (eq (current-column) 0) |
---|
| 998 | (singular-prompt-skip-forward))) |
---|
| 999 | |
---|
[71c107] | 1000 | (defun singular-beginning-of-line (arg) |
---|
| 1001 | "Move point to the beginning of line, then skip past prompt, if any. |
---|
| 1002 | If prefix argument is given the prompt is not skipped." |
---|
| 1003 | (interactive "P") |
---|
| 1004 | (beginning-of-line) |
---|
| 1005 | (if (not arg) (singular-prompt-skip-forward))) |
---|
| 1006 | |
---|
[f50151] | 1007 | (defun singular-load-file (file &optional noexpand) |
---|
| 1008 | "Read a file into Singular (via '< \"FILE\";'). |
---|
| 1009 | If optional argument NOEXPAND is non-nil, FILE is left as it is entered by |
---|
| 1010 | the user, otherwise it is expanded using `expand-file-name'." |
---|
| 1011 | (interactive "fLoad file: ") |
---|
| 1012 | (let* ((filename (if noexpand file (expand-file-name file))) |
---|
| 1013 | (string (concat "< \"" filename "\";")) |
---|
| 1014 | (process (singular-process))) |
---|
| 1015 | (singular-input-filter process string) |
---|
| 1016 | (singular-send-string process string))) |
---|
| 1017 | |
---|
[4cb4e3] | 1018 | (defvar singular-load-library-history nil |
---|
[e74240] | 1019 | "History list for loading of Singular libraries. |
---|
[4cb4e3] | 1020 | Is used by `singular-load-library'.") |
---|
| 1021 | |
---|
[9ad353] | 1022 | (defun singular-load-library (nonstdlib &optional file) |
---|
[fffd22] | 1023 | "Read a Singular library (via 'LIB \"FILE\";'). |
---|
| 1024 | If called interactively asks for the name of a standard Singular |
---|
[e74240] | 1025 | library. If called interactively with a prefix argument asks for a file |
---|
[fffd22] | 1026 | name of a Singular library." |
---|
[9ad353] | 1027 | (interactive "P") |
---|
| 1028 | (let ((string (or file |
---|
| 1029 | (if nonstdlib |
---|
| 1030 | (read-file-name "Library file: ") |
---|
| 1031 | (completing-read "Library: " singular-standard-libraries-alist |
---|
| 1032 | nil nil nil 'singular-load-library-history)))) |
---|
| 1033 | (process (singular-process))) |
---|
| 1034 | (setq string (concat "LIB \"" string "\";")) |
---|
[f50151] | 1035 | (singular-input-filter process string) |
---|
| 1036 | (singular-send-string process string))) |
---|
| 1037 | ;;}}} |
---|
| 1038 | |
---|
| 1039 | ;;{{{ History |
---|
| 1040 | (defcustom singular-history-ignoredups t |
---|
[71c107] | 1041 | "*If non-nil, do not add input matching the last on the input history." |
---|
[f50151] | 1042 | :type 'boolean |
---|
| 1043 | :initialize 'custom-initialize-default |
---|
| 1044 | :group 'singular-interactive-miscellaneous) |
---|
| 1045 | |
---|
| 1046 | ;; this variable is used to set Comint's `comint-input-ring-size' |
---|
| 1047 | (defcustom singular-history-size 64 |
---|
[71c107] | 1048 | "*Size of the input history. |
---|
[f50151] | 1049 | |
---|
| 1050 | Changing this variable has no immediate effect even if one uses |
---|
| 1051 | \\[customize] to do so. The new value will be used only in new Singular |
---|
| 1052 | interactive mode buffers." |
---|
| 1053 | :type 'integer |
---|
| 1054 | :initialize 'custom-initialize-default |
---|
| 1055 | :group 'singular-interactive-miscellaneous) |
---|
| 1056 | |
---|
| 1057 | (defcustom singular-history-filter-regexp "\\`\\(..?\\|\\s *\\)\\'" |
---|
[71c107] | 1058 | "*Regular expression to filter strings *not* to insert in the input history. |
---|
[f50151] | 1059 | By default, input consisting of less than three characters and input |
---|
| 1060 | consisting of white-space only is not inserted into the input history." |
---|
| 1061 | :type 'regexp |
---|
| 1062 | :initialize 'custom-initialize-default |
---|
| 1063 | :group 'singular-interactive-miscellaneous) |
---|
| 1064 | |
---|
| 1065 | (defcustom singular-history-explicit-file-name nil |
---|
[71c107] | 1066 | "*If non-nil, use this as file name to load and save the input history. |
---|
[f50151] | 1067 | If this variable equals nil, the `SINGULARHIST' environment variable is |
---|
| 1068 | used to determine the file name. |
---|
| 1069 | One should note that the input history is saved to file only on regular |
---|
| 1070 | termination of Singular; that is, if one leaves Singular using the commands |
---|
| 1071 | `quit\;' or `exit\;'." |
---|
| 1072 | :type '(choice (const nil) file) |
---|
| 1073 | :initialize 'custom-initialize-default |
---|
| 1074 | :group 'singular-interactive-miscellaneous) |
---|
| 1075 | |
---|
| 1076 | (defun singular-history-read () |
---|
| 1077 | "Read the input history from file. |
---|
| 1078 | If `singular-history-explicit-file-name' is non-nil, uses that as file |
---|
| 1079 | name, otherwise tries environment variable `SINGULARHIST'. |
---|
| 1080 | This function is called from `singular-exec' every time a new Singular |
---|
| 1081 | process is started." |
---|
| 1082 | (singular-debug 'interactive (message "Reading input ring")) |
---|
| 1083 | (let ((comint-input-ring-file-name (or singular-history-explicit-file-name |
---|
| 1084 | (getenv "SINGULARHIST")))) |
---|
| 1085 | ;; `comint-read-input-ring' does nothing if |
---|
| 1086 | ;; `comint-input-ring-file-name' equals nil |
---|
| 1087 | (comint-read-input-ring t))) |
---|
| 1088 | |
---|
| 1089 | (defun singular-history-write () |
---|
| 1090 | "Write back the input history to file. |
---|
| 1091 | If `singular-history-explicit-file-name' is non-nil, uses that as file |
---|
| 1092 | name, otherwise tries environment variable `SINGULARHIST'. |
---|
[fffd22] | 1093 | This function is called either by `singular-exit-singular' or by |
---|
| 1094 | `singular-exit-sentinel' every time a Singular process terminates |
---|
| 1095 | regularly." |
---|
[f50151] | 1096 | (singular-debug 'interactive (message "Writing input ring back")) |
---|
| 1097 | (let ((comint-input-ring-file-name (or singular-history-explicit-file-name |
---|
| 1098 | (getenv "SINGULARHIST")))) |
---|
| 1099 | ;; `comint-write-input-ring' does nothing if |
---|
| 1100 | ;; `comint-input-ring-file-name' equals nil |
---|
| 1101 | (comint-write-input-ring))) |
---|
| 1102 | |
---|
| 1103 | (defun singular-history-insert (input) |
---|
| 1104 | "Insert string INPUT into the input history if necessary." |
---|
| 1105 | (if (and (not (string-match singular-history-filter-regexp input)) |
---|
| 1106 | (or (not singular-history-ignoredups) |
---|
| 1107 | (not (ring-p comint-input-ring)) |
---|
| 1108 | (ring-empty-p comint-input-ring) |
---|
| 1109 | (not (string-equal (ring-ref comint-input-ring 0) input)))) |
---|
| 1110 | (ring-insert comint-input-ring input)) |
---|
| 1111 | (setq comint-input-ring-index nil)) |
---|
| 1112 | |
---|
| 1113 | (defun singular-history-init () |
---|
| 1114 | "Initialize variables concerning the input history. |
---|
| 1115 | |
---|
| 1116 | This function is called at mode initialization time." |
---|
| 1117 | (setq comint-input-ring-size singular-history-size)) |
---|
| 1118 | ;;}}} |
---|
| 1119 | |
---|
| 1120 | ;;{{{ Simple section API for both Emacs and XEmacs |
---|
[845cb34] | 1121 | |
---|
| 1122 | ;; Note: |
---|
| 1123 | ;; |
---|
[e2cd14] | 1124 | ;; Sections and simple sections are used to mark Singular's input and |
---|
| 1125 | ;; output for further access. Here are some general notes on simple |
---|
| 1126 | ;; sections. Sections are explained in the respective folding. |
---|
| 1127 | ;; |
---|
| 1128 | ;; In general, simple sections are more or less Emacs' overlays or XEmacs |
---|
| 1129 | ;; extents, resp. But they are more than simply an interface to overlays |
---|
[f50151] | 1130 | ;; or extents. |
---|
[e2cd14] | 1131 | ;; |
---|
| 1132 | ;; - Simple sections are non-empty portions of text. They are interpreted |
---|
| 1133 | ;; as left-closed, right-opened intervals, i.e., the start point of a |
---|
| 1134 | ;; simple sections belongs to it whereas the end point does not. |
---|
| 1135 | ;; - Simple sections start and end at line borders only. |
---|
| 1136 | ;; - Simple sections do not overlap. Thus, any point in the buffer may be |
---|
| 1137 | ;; covered by at most one simple section. |
---|
| 1138 | ;; - Besides from their start and their end, simple sections have some type |
---|
| 1139 | ;; associated. |
---|
| 1140 | ;; - Simple sections are realized using overlays (extents for XEmacs) |
---|
| 1141 | ;; which define the start and, end, and type (via properties) of the |
---|
| 1142 | ;; simple section. Actually, as a lisp object a simple section is |
---|
| 1143 | ;; nothing else but the underlying overlay. |
---|
| 1144 | ;; - There may be so-called clear simple sections. Clear simple sections |
---|
| 1145 | ;; have not an underlying overlay. Instead, they start at the end of the |
---|
| 1146 | ;; preceding non-clear simple section, end at the beginning of the next |
---|
| 1147 | ;; non-clear simple section, and have the type defined by |
---|
| 1148 | ;; `singular-simple-sec-clear-type'. Clear simple sections are |
---|
| 1149 | ;; represented by nil. |
---|
| 1150 | ;; - Buffer narrowing does not restrict the extent of completely or |
---|
[570cea] | 1151 | ;; partially inaccessible simple sections. But one should note that |
---|
| 1152 | ;; some of the functions assume that there is no narrowing in |
---|
| 1153 | ;; effect. |
---|
[e2cd14] | 1154 | ;; - After creation, simple sections are not modified any further. |
---|
[f50151] | 1155 | ;; - There is one nasty little corner case: what if a non-clear simple |
---|
| 1156 | ;; section spans up to end of buffer? By definition, eob is not included |
---|
| 1157 | ;; in that section since they are right-opened intervals. Most of the |
---|
| 1158 | ;; functions react as if there is an imagenary empty clear simple section |
---|
| 1159 | ;; at eob. |
---|
| 1160 | ;; - Even though by now there are only two types of different simple |
---|
| 1161 | ;; sections there may be an arbitrary number of them. Furthermore, |
---|
| 1162 | ;; simple sections of different types may appear in arbitrary order. |
---|
[e2cd14] | 1163 | ;; |
---|
| 1164 | ;; - In `singular-interactive-mode', the whole buffer is covered with |
---|
| 1165 | ;; simple sections from the very beginning of the file up to the |
---|
| 1166 | ;; beginning of the line containing the last input or output. The |
---|
| 1167 | ;; remaining text up to `(point-max)' may be interpreted as covered by |
---|
| 1168 | ;; one clear simple section. Thus, it is most reasonable to define |
---|
| 1169 | ;; `input' to be the type of clear simple sections. |
---|
[845cb34] | 1170 | |
---|
| 1171 | (defvar singular-simple-sec-clear-type 'input |
---|
[e6de219] | 1172 | "Type of clear simple sections. |
---|
[f50151] | 1173 | If nil no clear simple sections are used. |
---|
| 1174 | |
---|
| 1175 | One should not set this variable directly. Rather, one should customize |
---|
| 1176 | `singular-section-face-alist'.") |
---|
[e6de219] | 1177 | |
---|
| 1178 | (defvar singular-simple-sec-last-end nil |
---|
[e2cd14] | 1179 | "Marker at the end of the last simple section. |
---|
| 1180 | Should be initialized by `singular-simple-sec-init' before any calls to |
---|
[f50151] | 1181 | `singular-simple-sec-create' are done. Instead of accessing this variable |
---|
| 1182 | directly one should use the macro `singular-simple-sec-last-end-position'. |
---|
[570cea] | 1183 | |
---|
| 1184 | This variable is buffer-local.") |
---|
[845cb34] | 1185 | |
---|
| 1186 | (defun singular-simple-sec-init (pos) |
---|
[f50151] | 1187 | "Initialize variables belonging to simple section management. |
---|
[845cb34] | 1188 | Creates the buffer-local marker `singular-simple-sec-last-end' and |
---|
[f50151] | 1189 | initializes it to POS. POS should be at beginning of a line. |
---|
| 1190 | |
---|
| 1191 | This function is called every time a new Singular session is started." |
---|
[845cb34] | 1192 | (make-local-variable 'singular-simple-sec-last-end) |
---|
[81da6ba] | 1193 | (if (not (markerp singular-simple-sec-last-end)) |
---|
[845cb34] | 1194 | (setq singular-simple-sec-last-end (make-marker))) |
---|
| 1195 | (set-marker singular-simple-sec-last-end pos)) |
---|
| 1196 | |
---|
[f50151] | 1197 | (defmacro singular-simple-sec-last-end-position () |
---|
| 1198 | "Return the marker position of `singular-simple-sec-last-end'. |
---|
| 1199 | This macro exists more or less for purposes of information hiding only." |
---|
| 1200 | '(marker-position singular-simple-sec-last-end)) |
---|
| 1201 | |
---|
| 1202 | (defsubst singular-simple-sec-lookup-face (type) |
---|
| 1203 | "Return the face to use for simple sections of type TYPE. |
---|
| 1204 | This accesses the `singular-section-type-alist'. It does not harm if nil |
---|
| 1205 | is associated with TYPE in that alist: In this case, this function will |
---|
| 1206 | never be called for that TYPE." |
---|
| 1207 | (cdr (assq type singular-section-face-alist))) |
---|
| 1208 | |
---|
[845cb34] | 1209 | ;; Note: |
---|
| 1210 | ;; |
---|
| 1211 | ;; The rest of the folding is either marked as |
---|
| 1212 | ;; Emacs |
---|
| 1213 | ;; or |
---|
| 1214 | ;; XEmacs |
---|
| 1215 | |
---|
| 1216 | (singular-fset 'singular-simple-sec-create |
---|
| 1217 | 'singular-emacs-simple-sec-create |
---|
[e6de219] | 1218 | 'singular-xemacs-simple-sec-create) |
---|
[845cb34] | 1219 | |
---|
[f50151] | 1220 | (singular-fset 'singular-simple-sec-at |
---|
| 1221 | 'singular-emacs-simple-sec-at |
---|
| 1222 | 'singular-xemacs-simple-sec-at) |
---|
[845cb34] | 1223 | |
---|
| 1224 | (singular-fset 'singular-simple-sec-start |
---|
| 1225 | 'singular-emacs-simple-sec-start |
---|
[e6de219] | 1226 | 'singular-xemacs-simple-sec-start) |
---|
[845cb34] | 1227 | |
---|
| 1228 | (singular-fset 'singular-simple-sec-end |
---|
| 1229 | 'singular-emacs-simple-sec-end |
---|
[e6de219] | 1230 | 'singular-xemacs-simple-sec-end) |
---|
| 1231 | |
---|
[845cb34] | 1232 | (singular-fset 'singular-simple-sec-type |
---|
| 1233 | 'singular-emacs-simple-sec-type |
---|
[e6de219] | 1234 | 'singular-xemacs-simple-sec-type) |
---|
[845cb34] | 1235 | |
---|
| 1236 | (singular-fset 'singular-simple-sec-before |
---|
| 1237 | 'singular-emacs-simple-sec-before |
---|
[e6de219] | 1238 | 'singular-xemacs-simple-sec-before) |
---|
[845cb34] | 1239 | |
---|
[f50151] | 1240 | (singular-fset 'singular-simple-sec-start-at |
---|
| 1241 | 'singular-emacs-simple-sec-start-at |
---|
| 1242 | 'singular-xemacs-simple-sec-start-at) |
---|
| 1243 | |
---|
| 1244 | (singular-fset 'singular-simple-sec-end-at |
---|
| 1245 | 'singular-emacs-simple-sec-end-at |
---|
| 1246 | 'singular-xemacs-simple-sec-end-at) |
---|
| 1247 | |
---|
[845cb34] | 1248 | (singular-fset 'singular-simple-sec-in |
---|
| 1249 | 'singular-emacs-simple-sec-in |
---|
[e6de219] | 1250 | 'singular-xemacs-simple-sec-in) |
---|
[845cb34] | 1251 | ;;}}} |
---|
| 1252 | |
---|
[f50151] | 1253 | ;;{{{ Simple section API for Emacs |
---|
| 1254 | (defsubst singular-emacs-simple-sec-start (simple-sec) |
---|
| 1255 | "Return start of non-clear simple section SIMPLE-SEC. |
---|
| 1256 | Narrowing has no effect on this function." |
---|
| 1257 | (overlay-start simple-sec)) |
---|
| 1258 | |
---|
| 1259 | (defsubst singular-emacs-simple-sec-end (simple-sec) |
---|
| 1260 | "Return end of non-clear simple section SIMPLE-SEC. |
---|
| 1261 | Narrowing has no effect on this function." |
---|
| 1262 | (overlay-end simple-sec)) |
---|
| 1263 | |
---|
| 1264 | (defsubst singular-emacs-simple-sec-type (simple-sec) |
---|
| 1265 | "Return type of SIMPLE-SEC. |
---|
| 1266 | Returns nil if SIMPLE-SEC happens to be an overlay but not a simple |
---|
| 1267 | section. |
---|
| 1268 | Narrowing has no effect on this function." |
---|
| 1269 | (if simple-sec |
---|
| 1270 | (overlay-get simple-sec 'singular-type) |
---|
| 1271 | singular-simple-sec-clear-type)) |
---|
| 1272 | |
---|
| 1273 | (defsubst singular-emacs-simple-sec-before (pos) |
---|
| 1274 | "Return simple section before buffer position POS. |
---|
| 1275 | This is the same as `singular-simple-sec-at' except if POS falls on a |
---|
| 1276 | section border. In this case `singular-simple-section-before' returns the |
---|
| 1277 | previous simple section instead of the current one. If POS falls on |
---|
| 1278 | beginning of buffer, the simple section at beginning of buffer is returned. |
---|
| 1279 | Narrowing has no effect on this function." |
---|
| 1280 | (singular-emacs-simple-sec-at (max 1 (1- pos)))) |
---|
| 1281 | |
---|
[845cb34] | 1282 | (defun singular-emacs-simple-sec-create (type end) |
---|
| 1283 | "Create a new simple section of type TYPE. |
---|
[f50151] | 1284 | Creates the section from end of previous simple section up to the first |
---|
| 1285 | beginning of line before END. That position should be larger than or equal |
---|
| 1286 | to `singular-simple-sec-last-end'. Updates `singular-simple-sec-last-end'. |
---|
| 1287 | Returns the new simple section or `empty' if no simple section has been |
---|
| 1288 | created. |
---|
| 1289 | Assumes that no narrowing is in effect." |
---|
| 1290 | (let ((last-end (singular-simple-sec-last-end-position)) |
---|
[845cb34] | 1291 | ;; `simple-sec' is the new simple section or `empty' |
---|
| 1292 | simple-sec) |
---|
| 1293 | |
---|
[e6de219] | 1294 | ;; get beginning of line before END. At this point we need that there |
---|
| 1295 | ;; are no restrictions. |
---|
| 1296 | (setq end (let ((old-point (point))) |
---|
[845cb34] | 1297 | (goto-char end) (beginning-of-line) |
---|
[e6de219] | 1298 | (prog1 (point) (goto-char old-point)))) |
---|
[845cb34] | 1299 | |
---|
| 1300 | (cond |
---|
| 1301 | ;; do not create empty sections |
---|
[f50151] | 1302 | ((eq end last-end) |
---|
| 1303 | 'empty) |
---|
| 1304 | ;; non-clear simple sections |
---|
[845cb34] | 1305 | ((not (eq type singular-simple-sec-clear-type)) |
---|
[f50151] | 1306 | ;; if type has not changed we only have to extend the previous simple |
---|
| 1307 | ;; section. If `last-end' happens to be 1 (meaning that we are |
---|
| 1308 | ;; creating the first non-clear simple section in the buffer), then |
---|
| 1309 | ;; `singular-simple-sec-before' returns nil, |
---|
| 1310 | ;; `singular-simple-sec-type' returns the type of clear simple |
---|
| 1311 | ;; sections that definitely does not equal TYPE, and a new simple |
---|
| 1312 | ;; section is created as necessary. |
---|
[845cb34] | 1313 | (setq simple-sec (singular-emacs-simple-sec-before last-end)) |
---|
| 1314 | (if (eq type (singular-emacs-simple-sec-type simple-sec)) |
---|
| 1315 | ;; move existing overlay |
---|
| 1316 | (setq simple-sec (move-overlay simple-sec (overlay-start simple-sec) end)) |
---|
| 1317 | ;; create new overlay |
---|
| 1318 | (setq simple-sec (make-overlay last-end end)) |
---|
| 1319 | ;; set type property |
---|
| 1320 | (overlay-put simple-sec 'singular-type type) |
---|
| 1321 | ;; set face |
---|
[f50151] | 1322 | (overlay-put simple-sec 'face (singular-simple-sec-lookup-face type)) |
---|
[845cb34] | 1323 | ;; evaporate empty sections |
---|
[f50151] | 1324 | (overlay-put simple-sec 'evaporate t)) |
---|
| 1325 | ;; update `singular-simple-sec-last-end' and return new simple |
---|
| 1326 | ;; section |
---|
| 1327 | (set-marker singular-simple-sec-last-end end) |
---|
| 1328 | simple-sec) |
---|
| 1329 | ;; clear simple sections |
---|
| 1330 | (t |
---|
| 1331 | ;; update `singular-simple-sec-last-end' and return nil |
---|
| 1332 | (set-marker singular-simple-sec-last-end end) |
---|
| 1333 | nil)))) |
---|
[845cb34] | 1334 | |
---|
[e6de219] | 1335 | (defun singular-emacs-simple-sec-start-at (pos) |
---|
[f50151] | 1336 | "Return start of clear simple section at position POS. |
---|
| 1337 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1338 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1339 | at POS. |
---|
| 1340 | Assumes that no narrowing is in effect (since `previous-overlay-change' |
---|
| 1341 | imlicitly does so)." |
---|
| 1342 | ;; yes, this `(1+ pos)' is OK at eob for |
---|
| 1343 | ;; `singular-emacs-simple-sec-before' as well as |
---|
| 1344 | ;; `previous-overlay-change' |
---|
| 1345 | (let ((previous-overlay-change-pos (1+ pos))) |
---|
[570cea] | 1346 | ;; this `while' loop at last will run into the end of the next |
---|
[f50151] | 1347 | ;; non-clear simple section or stop at bob. Since POS may be right at |
---|
| 1348 | ;; the end of a previous non-clear location, we have to search at least |
---|
| 1349 | ;; one time from POS+1 backwards. |
---|
| 1350 | (while (not (or (singular-emacs-simple-sec-before previous-overlay-change-pos) |
---|
| 1351 | (eq previous-overlay-change-pos 1))) |
---|
| 1352 | (setq previous-overlay-change-pos |
---|
| 1353 | (previous-overlay-change previous-overlay-change-pos))) |
---|
| 1354 | previous-overlay-change-pos)) |
---|
[e6de219] | 1355 | |
---|
| 1356 | (defun singular-emacs-simple-sec-end-at (pos) |
---|
[f50151] | 1357 | "Return end of clear simple section at position POS. |
---|
| 1358 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1359 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1360 | at POS. |
---|
| 1361 | Assumes that no narrowing is in effect (since `next-overlay-change' |
---|
| 1362 | imlicitly does so)." |
---|
| 1363 | (let ((next-overlay-change-pos (next-overlay-change pos))) |
---|
[570cea] | 1364 | ;; this `while' loop at last will run into the beginning of the next |
---|
[f50151] | 1365 | ;; non-clear simple section or stop at eob. Since POS may not be at |
---|
| 1366 | ;; the beginning of a non-clear simple section we may start searching |
---|
[570cea] | 1367 | ;; immediately. |
---|
[f50151] | 1368 | (while (not (or (singular-emacs-simple-sec-at next-overlay-change-pos) |
---|
| 1369 | (eq next-overlay-change-pos (point-max)))) |
---|
| 1370 | (setq next-overlay-change-pos |
---|
| 1371 | (next-overlay-change next-overlay-change-pos))) |
---|
| 1372 | next-overlay-change-pos)) |
---|
[845cb34] | 1373 | |
---|
| 1374 | (defun singular-emacs-simple-sec-at (pos) |
---|
[f50151] | 1375 | "Return simple section at buffer position POS. |
---|
| 1376 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1377 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1378 | at POS. |
---|
| 1379 | Narrowing has no effect on this function." |
---|
| 1380 | ;; at eob, `overlays-at' always returns nil so everything is OK for this |
---|
| 1381 | ;; case, too |
---|
[845cb34] | 1382 | (let ((overlays (overlays-at pos)) simple-sec) |
---|
| 1383 | ;; be careful, there may be other overlays! |
---|
| 1384 | (while (and overlays (not simple-sec)) |
---|
| 1385 | (if (singular-emacs-simple-sec-type (car overlays)) |
---|
| 1386 | (setq simple-sec (car overlays))) |
---|
| 1387 | (setq overlays (cdr overlays))) |
---|
| 1388 | simple-sec)) |
---|
| 1389 | |
---|
| 1390 | (defun singular-emacs-simple-sec-in (beg end) |
---|
| 1391 | "Return a list of all simple sections intersecting with the region from BEG to END. |
---|
[f50151] | 1392 | A simple section intersects the region if the section and the region have |
---|
| 1393 | at least one character in common. The sections are returned with |
---|
| 1394 | startpoints in increasing order and clear simple sections (that is, nil's) |
---|
| 1395 | inserted as necessary. BEG is assumed to be less than or equal to END. |
---|
| 1396 | The imagenary empty clear simple section at end of buffer is never included |
---|
| 1397 | in the result. |
---|
| 1398 | Narrowing has no effect on this function." |
---|
| 1399 | (let (overlays overlay-cursor) |
---|
| 1400 | (if (= beg end) |
---|
| 1401 | ;; `overlays-in' seems not be correct with respect to this case |
---|
| 1402 | nil |
---|
| 1403 | ;; go to END since chances are good that the overlays come in correct |
---|
| 1404 | ;; order, then |
---|
| 1405 | (setq overlays (let ((old-point (point))) |
---|
| 1406 | (goto-char end) |
---|
| 1407 | (prog1 (overlays-in beg end) |
---|
| 1408 | (goto-char old-point))) |
---|
| 1409 | |
---|
| 1410 | ;; now, turn overlays that are not simple sections into nils |
---|
| 1411 | overlays (mapcar (function |
---|
| 1412 | (lambda (overlay) |
---|
| 1413 | (and (singular-emacs-simple-sec-type overlay) |
---|
| 1414 | overlay))) |
---|
| 1415 | overlays) |
---|
| 1416 | ;; then, remove nils from list |
---|
| 1417 | overlays (delq nil overlays) |
---|
| 1418 | ;; now, we have to sort the list since documentation of `overlays-in' |
---|
| 1419 | ;; does not state anything about the order the overlays are returned in |
---|
| 1420 | overlays |
---|
| 1421 | (sort overlays |
---|
| 1422 | (function |
---|
| 1423 | (lambda (a b) |
---|
| 1424 | (< (overlay-start a) (overlay-start b)))))) |
---|
| 1425 | |
---|
| 1426 | ;; at last, we have the list of non-clear simple sections. Now, go and |
---|
| 1427 | ;; insert clear simple sections as necessary. |
---|
| 1428 | (if (null overlays) |
---|
| 1429 | ;; if there are no non-clear simple sections at all there can be |
---|
| 1430 | ;; only one large clear simple section |
---|
| 1431 | '(nil) |
---|
| 1432 | ;; we care about inside clear simple section first |
---|
| 1433 | (setq overlay-cursor overlays) |
---|
| 1434 | (while (cdr overlay-cursor) |
---|
| 1435 | (if (eq (overlay-end (car overlay-cursor)) |
---|
| 1436 | (overlay-start (cadr overlay-cursor))) |
---|
| 1437 | (setq overlay-cursor (cdr overlay-cursor)) |
---|
| 1438 | ;; insert nil |
---|
| 1439 | (setcdr overlay-cursor |
---|
| 1440 | (cons nil (cdr overlay-cursor))) |
---|
| 1441 | (setq overlay-cursor (cddr overlay-cursor)))) |
---|
| 1442 | ;; now, check BEG and END for clear simple sections |
---|
| 1443 | (if (> (overlay-start (car overlays)) beg) |
---|
| 1444 | (setq overlays (cons nil overlays))) |
---|
| 1445 | ;; `overlay-cursor' still points to the end |
---|
| 1446 | (if (< (overlay-end (car overlay-cursor)) end) |
---|
| 1447 | (setcdr overlay-cursor (cons nil nil))) |
---|
| 1448 | overlays)))) |
---|
[845cb34] | 1449 | ;;}}} |
---|
| 1450 | |
---|
[f50151] | 1451 | ;;{{{ Simple section API for XEmacs |
---|
| 1452 | (defsubst singular-xemacs-simple-sec-start (simple-sec) |
---|
| 1453 | "Return start of non-clear simple section SIMPLE-SEC. |
---|
| 1454 | Narrowing has no effect on this function." |
---|
| 1455 | (extent-start-position simple-sec)) |
---|
| 1456 | |
---|
| 1457 | (defsubst singular-xemacs-simple-sec-end (simple-sec) |
---|
| 1458 | "Return end of non-clear simple section SIMPLE-SEC. |
---|
| 1459 | Narrowing has no effect on this function." |
---|
| 1460 | (extent-end-position simple-sec)) |
---|
| 1461 | |
---|
| 1462 | (defsubst singular-xemacs-simple-sec-type (simple-sec) |
---|
| 1463 | "Return type of SIMPLE-SEC. |
---|
| 1464 | Returns nil if SIMPLE-SEC happens to be an extent but not a simple |
---|
| 1465 | section. |
---|
| 1466 | Narrowing has no effect on this function." |
---|
| 1467 | (if simple-sec |
---|
| 1468 | (extent-property simple-sec 'singular-type) |
---|
| 1469 | singular-simple-sec-clear-type)) |
---|
| 1470 | |
---|
| 1471 | (defsubst singular-xemacs-simple-sec-before (pos) |
---|
| 1472 | "Return simple section before buffer position POS. |
---|
| 1473 | This is the same as `singular-simple-sec-at' except if POS falls on a |
---|
| 1474 | section border. In this case `singular-simple-section-before' returns the |
---|
| 1475 | previous simple section instead of the current one. If POS falls on |
---|
| 1476 | beginning of buffer, the simple section at beginning of buffer is returned. |
---|
| 1477 | Narrowing has no effect on this function." |
---|
| 1478 | (singular-xemacs-simple-sec-at (max 1 (1- pos)))) |
---|
| 1479 | |
---|
[93d8a1] | 1480 | (defun singular-xemacs-simple-sec-create (type end) |
---|
| 1481 | "Create a new simple section of type TYPE. |
---|
[f50151] | 1482 | Creates the section from end of previous simple section up to the first |
---|
| 1483 | beginning of line before END. That position should be larger than or equal |
---|
| 1484 | to `singular-simple-sec-last-end'. Updates `singular-simple-sec-last-end'. |
---|
| 1485 | Returns the new simple section or `empty' if no simple section has been |
---|
| 1486 | created. |
---|
| 1487 | Assumes that no narrowing is in effect." |
---|
| 1488 | (let ((last-end (singular-simple-sec-last-end-position)) |
---|
[93d8a1] | 1489 | ;; `simple-sec' is the new simple section or `empty' |
---|
| 1490 | simple-sec) |
---|
| 1491 | |
---|
| 1492 | ;; get beginning of line before END. At this point we need that there |
---|
| 1493 | ;; are no restrictions. |
---|
| 1494 | (setq end (let ((old-point (point))) |
---|
| 1495 | (goto-char end) (beginning-of-line) |
---|
| 1496 | (prog1 (point) (goto-char old-point)))) |
---|
| 1497 | |
---|
| 1498 | (cond |
---|
| 1499 | ;; do not create empty sections |
---|
[f50151] | 1500 | ((eq end last-end) |
---|
| 1501 | 'empty) |
---|
| 1502 | ;; non-clear simple sections |
---|
[93d8a1] | 1503 | ((not (eq type singular-simple-sec-clear-type)) |
---|
[f50151] | 1504 | ;; if type has not changed we only have to extend the previous simple |
---|
| 1505 | ;; section. If `last-end' happens to be 1 (meaning that we are |
---|
| 1506 | ;; creating the first non-clear simple section in the buffer), then |
---|
| 1507 | ;; `singular-simple-sec-before' returns nil, |
---|
| 1508 | ;; `singular-simple-sec-type' returns the type of clear simple |
---|
| 1509 | ;; sections that definitely does not equal TYPE, and a new simple |
---|
| 1510 | ;; section is created as necessary. |
---|
[93d8a1] | 1511 | (setq simple-sec (singular-xemacs-simple-sec-before last-end)) |
---|
| 1512 | (if (eq type (singular-xemacs-simple-sec-type simple-sec)) |
---|
| 1513 | ;; move existing extent |
---|
| 1514 | (setq simple-sec (set-extent-endpoints simple-sec |
---|
| 1515 | (extent-start-position simple-sec) end)) |
---|
| 1516 | ;; create new extent |
---|
| 1517 | (setq simple-sec (make-extent last-end end)) |
---|
| 1518 | ;; set type property |
---|
| 1519 | (set-extent-property simple-sec 'singular-type type) |
---|
[f50151] | 1520 | ;; set face. In contrast to Emacs, we do not need to set somethin |
---|
| 1521 | ;; like `evaporate'. `detachable' is set by XEmacs by default. |
---|
| 1522 | (set-extent-property simple-sec 'face (singular-simple-sec-lookup-face type))) |
---|
| 1523 | ;; update `singular-simple-sec-last-end' and return new simple |
---|
| 1524 | ;; section |
---|
| 1525 | (set-marker singular-simple-sec-last-end end) |
---|
| 1526 | simple-sec) |
---|
| 1527 | ;; clear simple sections |
---|
| 1528 | (t |
---|
| 1529 | ;; update `singular-simple-sec-last-end' and return nil |
---|
| 1530 | (set-marker singular-simple-sec-last-end end) |
---|
| 1531 | nil)))) |
---|
[93d8a1] | 1532 | |
---|
| 1533 | (defun singular-xemacs-simple-sec-start-at (pos) |
---|
[f50151] | 1534 | "Return start of clear simple section at position POS. |
---|
| 1535 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1536 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1537 | at POS. |
---|
| 1538 | Assumes that no narrowing is in effect (since `previous-extent-change' |
---|
| 1539 | imlicitly does so)." |
---|
| 1540 | ;; get into some hairy details at end of buffer. Look if there is a |
---|
| 1541 | ;; non-clear simple section immediately ending at end of buffer and |
---|
| 1542 | ;; return the start of the imagenary empty clear simple section in that |
---|
| 1543 | ;; case. If buffer is empty this test fails since |
---|
| 1544 | ;; `singular-xemacs-simple-sec-before' (corretly) returns nil. But in |
---|
| 1545 | ;; that case the following loop returns the correct result. |
---|
| 1546 | (if (and (eq pos (point-max)) |
---|
| 1547 | (singular-xemacs-simple-sec-before pos)) |
---|
| 1548 | pos |
---|
| 1549 | (let ((previous-extent-change-pos (min (1+ pos) (point-max)))) |
---|
| 1550 | ;; this `while' loop at last will run into the end of the next |
---|
| 1551 | ;; non-clear simple section or stop at bob. Since POS may be right at |
---|
| 1552 | ;; the end of a previous non-clear location, we have to search at least |
---|
| 1553 | ;; one time from POS+1 backwards. |
---|
| 1554 | (while (not (or (singular-xemacs-simple-sec-before previous-extent-change-pos) |
---|
| 1555 | (eq previous-extent-change-pos 1))) |
---|
| 1556 | (setq previous-extent-change-pos |
---|
| 1557 | (previous-extent-change previous-extent-change-pos))) |
---|
| 1558 | previous-extent-change-pos))) |
---|
[93d8a1] | 1559 | |
---|
| 1560 | (defun singular-xemacs-simple-sec-end-at (pos) |
---|
[f50151] | 1561 | "Return end of clear simple section at position POS. |
---|
| 1562 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1563 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1564 | at POS. |
---|
| 1565 | Assumes that no narrowing is in effect (since `next-extent-change' |
---|
| 1566 | imlicitly does so)." |
---|
| 1567 | (let ((next-extent-change-pos (next-extent-change pos))) |
---|
[93d8a1] | 1568 | ;; this `while' loop at last will run into the beginning of the next |
---|
[f50151] | 1569 | ;; non-clear simple section or stop at eob. Since POS may not be at |
---|
| 1570 | ;; the beginning of a non-clear simple section we may start searching |
---|
[93d8a1] | 1571 | ;; immediately. |
---|
[f50151] | 1572 | (while (not (or (singular-xemacs-simple-sec-at next-extent-change-pos) |
---|
| 1573 | (eq next-extent-change-pos (point-max)))) |
---|
| 1574 | (setq next-extent-change-pos |
---|
| 1575 | (next-extent-change next-extent-change-pos))) |
---|
| 1576 | next-extent-change-pos)) |
---|
[93d8a1] | 1577 | |
---|
| 1578 | (defun singular-xemacs-simple-sec-at (pos) |
---|
[f50151] | 1579 | "Return simple section at buffer position POS. |
---|
| 1580 | Assumes the existence of an imagenary empty clear simple section if POS is |
---|
| 1581 | at end of buffer and there is non-clear simple section immediately ending |
---|
| 1582 | at POS. |
---|
| 1583 | Narrowing has no effect on this function." |
---|
| 1584 | ;; at eob, `map-extent' always returns nil so everything is OK for this |
---|
| 1585 | ;; case, too. Do not try to use `extent-at' at this point. `extent-at' |
---|
| 1586 | ;; does not return extents outside narrowed text. |
---|
[93d8a1] | 1587 | (map-extents (function (lambda (ext args) ext)) |
---|
[f50151] | 1588 | nil pos pos nil nil 'singular-type)) |
---|
[93d8a1] | 1589 | |
---|
| 1590 | (defun singular-xemacs-simple-sec-in (beg end) |
---|
| 1591 | "Return a list of all simple sections intersecting with the region from BEG to END. |
---|
[f50151] | 1592 | A simple section intersects the region if the section and the region have |
---|
| 1593 | at least one character in common. The sections are returned with |
---|
| 1594 | startpoints in increasing order and clear simple sections (that is, nil's) |
---|
| 1595 | inserted as necessary. BEG is assumed to be less than or equal to END. |
---|
| 1596 | The imagenary empty clear simple section at end of buffer is never included |
---|
| 1597 | in the result. |
---|
| 1598 | Narrowing has no effect on this function." |
---|
| 1599 | (let (extents extent-cursor) |
---|
| 1600 | (if (= beg end) |
---|
| 1601 | ;; `mapcar-extents' may return some extents in this case, so |
---|
| 1602 | ;; exclude it |
---|
| 1603 | nil |
---|
| 1604 | ;; OK, that's a little bit easier than for Emacs ... |
---|
| 1605 | (setq extents (mapcar-extents 'identity nil nil beg end nil 'singular-type)) |
---|
| 1606 | |
---|
| 1607 | ;; now we have the list of non-clear simple sections. Go and |
---|
| 1608 | ;; insert clear simple sections as necessary. |
---|
| 1609 | (if (null extents) |
---|
| 1610 | ;; if there are no non-clear simple sections at all there can be |
---|
| 1611 | ;; only one large clear simple section |
---|
| 1612 | '(nil) |
---|
| 1613 | ;; we care about inside clear simple section first |
---|
| 1614 | (setq extent-cursor extents) |
---|
| 1615 | (while (cdr extent-cursor) |
---|
| 1616 | (if (eq (extent-end-position (car extent-cursor)) |
---|
| 1617 | (extent-start-position (cadr extent-cursor))) |
---|
| 1618 | (setq extent-cursor (cdr extent-cursor)) |
---|
| 1619 | ;; insert nil |
---|
| 1620 | (setcdr extent-cursor |
---|
| 1621 | (cons nil (cdr extent-cursor))) |
---|
| 1622 | (setq extent-cursor (cddr extent-cursor)))) |
---|
| 1623 | ;; now, check BEG and END for clear simple sections |
---|
| 1624 | (if (> (extent-start-position (car extents)) beg) |
---|
| 1625 | (setq extents (cons nil extents))) |
---|
| 1626 | ;; `extent-cursor' still points to the end |
---|
| 1627 | (if (< (extent-end-position (car extent-cursor)) end) |
---|
| 1628 | (setcdr extent-cursor (cons nil nil))) |
---|
| 1629 | extents)))) |
---|
[93d8a1] | 1630 | ;;}}} |
---|
| 1631 | |
---|
[f50151] | 1632 | ;;{{{ Section API |
---|
[abc5138] | 1633 | |
---|
| 1634 | ;; Note: |
---|
| 1635 | ;; |
---|
[e2cd14] | 1636 | ;; Sections are built on simple sections. Their purpose is to cover the |
---|
| 1637 | ;; difference between clear and non-clear simple sections. |
---|
| 1638 | ;; |
---|
| 1639 | ;; - Sections consist of a simple section, its type, and its start and end |
---|
| 1640 | ;; points. This is redundant information only in the case of non-clear |
---|
| 1641 | ;; simple section. |
---|
| 1642 | ;; - Sections are read-only objects, neither are they modified nor are they |
---|
| 1643 | ;; created. |
---|
[570cea] | 1644 | ;; - Buffer narrowing does not restrict the extent of completely or |
---|
[f50151] | 1645 | ;; partially inaccessible sections. In contrast to simple sections the |
---|
| 1646 | ;; functions concerning sections do not assume that there is no narrowing |
---|
| 1647 | ;; in effect. However, most functions provide an optional argument |
---|
| 1648 | ;; RESTRICTED that restricts the start and end point of the returned |
---|
| 1649 | ;; sections to the currently active restrictions. Of course, that does |
---|
| 1650 | ;; not affect the range of the underlying simple sections, only the |
---|
| 1651 | ;; additional start and end points being returned. One should note that |
---|
| 1652 | ;; by restricting sections one may get empty sections, that is, sections |
---|
| 1653 | ;; for which the additional start and end point are equal. |
---|
[71c107] | 1654 | ;; - In many cases it is not desirable that the user operates on sections |
---|
| 1655 | ;; which are not completely accessible. To check that a section is |
---|
| 1656 | ;; completely accessible the `singular-section-check' function should be |
---|
| 1657 | ;; used. |
---|
[e2cd14] | 1658 | ;; - Sections are independent from implementation dependencies. There are |
---|
| 1659 | ;; no different versions of the functions for Emacs and XEmacs. |
---|
[f50151] | 1660 | ;; - Whenever possible, one should not access simple section directly. |
---|
| 1661 | ;; Instead, one should use the section API. |
---|
| 1662 | |
---|
| 1663 | (defcustom singular-section-face-alist '((input . nil) |
---|
| 1664 | (output . singular-section-output-face)) |
---|
| 1665 | "*Alist that maps section types to faces. |
---|
| 1666 | Should be a list consisting of elements (SECTION-TYPE . FACE-OR-NIL), where |
---|
| 1667 | SECTION-TYPE is either `input' or `output'. |
---|
| 1668 | |
---|
| 1669 | At any time, the Singular interactive mode buffer is completely covered by |
---|
| 1670 | sections of two different types: input sections and output sections. This |
---|
| 1671 | variable determines which faces are used to display the different sections. |
---|
| 1672 | |
---|
| 1673 | If for type SECTION-TYPE the value FACE-OR-NIL is a face it is used to |
---|
| 1674 | display the contents of all sections of that particular type. |
---|
| 1675 | If instead FACE-OR-NIL equals nil sections of that type become so-called |
---|
| 1676 | clear sections. The content of clear sections is displayed as regular |
---|
| 1677 | text, with no faces at all attached to them. |
---|
| 1678 | |
---|
| 1679 | Some notes and restrictions on this variable (believe them or not): |
---|
| 1680 | o Changing this variable during a Singular session may cause unexpected |
---|
| 1681 | results (but not too serious ones, though). |
---|
| 1682 | o There may be only one clear section type defined at a time. |
---|
| 1683 | o Choosing clear input sections is a good idea. |
---|
| 1684 | o Choosing clear output sections is a bad idea. |
---|
| 1685 | o Consequence: Not to change this variable is a good idea." |
---|
| 1686 | ;; to add new section types, simply extend the `list' widget. |
---|
| 1687 | ;; The rest should work unchanged. Do not forget to update docu. |
---|
| 1688 | :type '(list (cons :tag "Input sections" |
---|
| 1689 | (const :format "" input) |
---|
| 1690 | (choice :format |
---|
| 1691 | "Choose either clear or non-clear input sections. For non-clear sections, |
---|
| 1692 | select or modify a face (preferably `singular-section-input-face') used to |
---|
| 1693 | display the sections. |
---|
| 1694 | %[Choice%] |
---|
| 1695 | %v |
---|
| 1696 | " |
---|
| 1697 | (const :tag "Clear sections" nil) |
---|
| 1698 | (face :tag "Non-clear sections"))) |
---|
| 1699 | (cons :tag "Output sections" |
---|
| 1700 | (const :format "" output) |
---|
| 1701 | (choice :format |
---|
| 1702 | "Choose either clear or non-clear ouput sections. For non-clear sections, |
---|
| 1703 | select or modify a face (preferably `singular-section-output-face') used to |
---|
| 1704 | display the sections. |
---|
| 1705 | %[Choice%] |
---|
| 1706 | %v |
---|
| 1707 | " |
---|
| 1708 | (const :tag "Clear sections" nil) |
---|
| 1709 | (face :tag "Non-clear sections")))) |
---|
| 1710 | :initialize 'custom-initialize-reset |
---|
| 1711 | ;; this function checks for validity (only one clear section |
---|
| 1712 | ;; type) and sets `singular-simple-sec-clear-type' accordingly. |
---|
| 1713 | ;; In case of an error, nothing is set or modified. |
---|
| 1714 | :set (function (lambda (var value) |
---|
| 1715 | (let* ((cdrs-with-nils (mapcar 'cdr value)) |
---|
| 1716 | (cdrs-without-nils (delq nil (copy-sequence cdrs-with-nils)))) |
---|
| 1717 | (if (> (- (length cdrs-with-nils) (length cdrs-without-nils)) 1) |
---|
| 1718 | (error "Only one clear section type allowed (see `singular-section-face-alist')") |
---|
| 1719 | (set-default var value) |
---|
| 1720 | (setq singular-simple-sec-clear-type (car (rassq nil value))))))) |
---|
| 1721 | :group 'singular-faces |
---|
| 1722 | :group 'singular-sections-and-foldings) |
---|
| 1723 | |
---|
| 1724 | (defface singular-section-input-face '((t nil)) |
---|
| 1725 | "*Face to use for input sections. |
---|
| 1726 | It may be not sufficient to modify this face to change the appearance of |
---|
| 1727 | input sections. See `singular-section-face-alist' for more information." |
---|
| 1728 | :group 'singular-faces |
---|
| 1729 | :group 'singular-sections-and-foldings) |
---|
| 1730 | |
---|
| 1731 | (defface singular-section-output-face '((t (:bold t))) |
---|
| 1732 | "*Face to use for output sections. |
---|
| 1733 | It may be not sufficient to modify this face to change the appearance of |
---|
| 1734 | output sections. See `singular-section-face-alist' for more information." |
---|
| 1735 | :group 'singular-faces |
---|
| 1736 | :group 'singular-sections-and-foldings) |
---|
| 1737 | |
---|
| 1738 | (defsubst singular-section-create (simple-sec type start end) |
---|
| 1739 | "Create and return a new section." |
---|
| 1740 | (vector simple-sec type start end)) |
---|
| 1741 | |
---|
| 1742 | (defsubst singular-section-simple-sec (section) |
---|
| 1743 | "Return underlying simple section of SECTION." |
---|
| 1744 | (aref section 0)) |
---|
| 1745 | |
---|
| 1746 | (defsubst singular-section-type (section) |
---|
| 1747 | "Return type of SECTION." |
---|
| 1748 | (aref section 1)) |
---|
| 1749 | |
---|
| 1750 | (defsubst singular-section-start (section) |
---|
| 1751 | "Return start of SECTION." |
---|
| 1752 | (aref section 2)) |
---|
| 1753 | |
---|
| 1754 | (defsubst singular-section-end (section) |
---|
| 1755 | "Return end of SECTION." |
---|
| 1756 | (aref section 3)) |
---|
[abc5138] | 1757 | |
---|
[e6de219] | 1758 | (defun singular-section-at (pos &optional restricted) |
---|
[abc5138] | 1759 | "Return section at position POS. |
---|
[e6de219] | 1760 | Returns section intersected with current restriction if RESTRICTED is |
---|
| 1761 | non-nil." |
---|
[abc5138] | 1762 | (let* ((simple-sec (singular-simple-sec-at pos)) |
---|
| 1763 | (type (singular-simple-sec-type simple-sec)) |
---|
[570cea] | 1764 | start end) |
---|
| 1765 | (if simple-sec |
---|
| 1766 | (setq start (singular-simple-sec-start simple-sec) |
---|
| 1767 | end (singular-simple-sec-end simple-sec)) |
---|
| 1768 | (save-restriction |
---|
| 1769 | (widen) |
---|
| 1770 | (setq start (singular-simple-sec-start-at pos) |
---|
| 1771 | end (singular-simple-sec-end-at pos)))) |
---|
[f50151] | 1772 | (cond |
---|
| 1773 | ;; not restricted first |
---|
| 1774 | ((not restricted) |
---|
| 1775 | (singular-section-create simple-sec type start end)) |
---|
| 1776 | ;; restricted and degenerated |
---|
| 1777 | ((and restricted |
---|
| 1778 | (< end (point-min))) |
---|
| 1779 | (singular-section-create simple-sec type (point-min) (point-min))) |
---|
| 1780 | ;; restricted and degenerated |
---|
| 1781 | ((and restricted |
---|
| 1782 | (> start (point-max))) |
---|
| 1783 | (singular-section-create simple-sec type (point-max) (point-max))) |
---|
| 1784 | ;; restricted but not degenrated |
---|
| 1785 | (t |
---|
| 1786 | (singular-section-create simple-sec type |
---|
| 1787 | (max start (point-min)) |
---|
| 1788 | (min end (point-max))))))) |
---|
[abc5138] | 1789 | |
---|
[570cea] | 1790 | (defun singular-section-before (pos &optional restricted) |
---|
| 1791 | "Return section before position POS. |
---|
[f50151] | 1792 | This is the same as `singular-section-at' except if POS falls on a section |
---|
| 1793 | border. In this case `singular-section-before' returns the previous |
---|
| 1794 | section instead of the current one. If POS falls on beginning of buffer, |
---|
| 1795 | the section at beginning of buffer is returned. |
---|
[570cea] | 1796 | Returns section intersected with current restriction if RESTRICTED is |
---|
| 1797 | non-nil." |
---|
| 1798 | (singular-section-at (max 1 (1- pos)) restricted)) |
---|
| 1799 | |
---|
[f50151] | 1800 | (defun singular-section-in (beg end &optional restricted) |
---|
| 1801 | "Return a list of all sections intersecting with the region from BEG to END. |
---|
| 1802 | A section intersects with the region if the section and the region have at |
---|
| 1803 | least one character in common. The sections are returned in increasing |
---|
| 1804 | order. |
---|
| 1805 | If optional argument RESTRICTED is non-nil only sections which are |
---|
| 1806 | completely in the intersection of the region and the current restriction |
---|
| 1807 | are returned." |
---|
| 1808 | ;; exchange BEG and END if necessary as a special service to our users |
---|
| 1809 | (let* ((reg-beg (min beg end)) |
---|
| 1810 | (reg-end (max beg end)) |
---|
| 1811 | ;; we need these since we widen the buffer later on |
---|
| 1812 | (point-min (point-min)) |
---|
| 1813 | (point-max (point-max)) |
---|
| 1814 | simple-sections) |
---|
| 1815 | (if (and restricted |
---|
| 1816 | (or (> reg-beg point-max) (< reg-end point-min))) |
---|
| 1817 | ;; degenerate restrictions |
---|
| 1818 | nil |
---|
| 1819 | ;; do the intersection if necessary and get simple sections |
---|
| 1820 | (setq reg-beg (if restricted (max reg-beg point-min) reg-beg) |
---|
| 1821 | reg-end (if restricted (min reg-end point-max) reg-end) |
---|
| 1822 | simple-sections (singular-simple-sec-in reg-beg reg-end)) |
---|
| 1823 | ;; we still have REG-BEG <= REG-END in any case. SIMPLE-SECTIONS |
---|
| 1824 | ;; contains the list of simple sections intersecting with the region |
---|
| 1825 | ;; from REG-BEG and REG-END. |
---|
| 1826 | |
---|
| 1827 | (if (null simple-sections) |
---|
| 1828 | nil |
---|
| 1829 | ;; and here we even have REG-BEG < REG-END |
---|
| 1830 | (save-restriction |
---|
| 1831 | (widen) |
---|
| 1832 | ;; get sections intersecting with the region from REG-BEG to |
---|
| 1833 | ;; REG-END |
---|
| 1834 | (let* ((sections (singular-section-in-internal simple-sections |
---|
| 1835 | reg-beg reg-end)) |
---|
| 1836 | first-section-start last-section-end) |
---|
| 1837 | (if (not restricted) |
---|
| 1838 | sections |
---|
| 1839 | (setq first-section-start (singular-section-start (car sections)) |
---|
| 1840 | last-section-end (singular-section-end (car (last sections)))) |
---|
| 1841 | ;; popping off first element is easy ... |
---|
| 1842 | (if (< first-section-start point-min) |
---|
| 1843 | (setq sections (cdr sections))) |
---|
| 1844 | ;; ... but last element is harder to pop off |
---|
| 1845 | (cond |
---|
| 1846 | (;; no elements left |
---|
| 1847 | (null sections) |
---|
| 1848 | nil) |
---|
| 1849 | (;; one element left |
---|
| 1850 | (null (cdr sections)) |
---|
| 1851 | (if (> last-section-end point-max) |
---|
| 1852 | nil |
---|
| 1853 | sections)) |
---|
| 1854 | (;; more than one element left |
---|
| 1855 | t |
---|
| 1856 | (if (> last-section-end point-max) |
---|
| 1857 | (setcdr (last sections 2) nil)) |
---|
| 1858 | sections))))))))) |
---|
| 1859 | |
---|
| 1860 | (defun singular-section-in-internal (simple-sections reg-beg reg-end) |
---|
| 1861 | "Create a list of sections from SIMPLE-SECTIONS. |
---|
| 1862 | This is the back-end for `singular-section-in'. |
---|
| 1863 | First simple section should be such that it contains REG-BEG, last simple |
---|
| 1864 | section should be such that it contains or ends at REG-END. These |
---|
| 1865 | arguments are used to find the start resp. end of clear simple sections of |
---|
| 1866 | terminal clear simple sections in SIMPLE-SECTIONS. |
---|
| 1867 | Assumes that REG-BEG < REG-END. |
---|
| 1868 | Assumes that SIMPLE-SECTIONS is not empty. |
---|
| 1869 | Assumes that no narrowing is in effect." |
---|
| 1870 | (let* (;; we pop off the extra nil at the end of the loop |
---|
| 1871 | (sections (cons nil nil)) |
---|
| 1872 | (sections-end sections) |
---|
| 1873 | (simple-section (car simple-sections)) |
---|
| 1874 | type start end) |
---|
| 1875 | |
---|
| 1876 | ;; first, get unrestricted start |
---|
| 1877 | (setq start (if simple-section |
---|
| 1878 | (singular-simple-sec-start simple-section) |
---|
| 1879 | ;; here we need that no narrowing is in effect |
---|
| 1880 | (singular-simple-sec-start-at reg-beg))) |
---|
| 1881 | |
---|
| 1882 | ;; loop through all simple sections but last |
---|
| 1883 | (while (cdr simple-sections) |
---|
| 1884 | (setq simple-section (car simple-sections) |
---|
| 1885 | type (singular-simple-sec-type simple-section) |
---|
| 1886 | end (if simple-section |
---|
| 1887 | (singular-simple-sec-end simple-section) |
---|
| 1888 | (singular-simple-sec-start (cadr simple-sections))) |
---|
| 1889 | |
---|
| 1890 | ;; append the new section to `sections-end' |
---|
| 1891 | sections-end |
---|
| 1892 | (setcdr sections-end |
---|
| 1893 | (cons (singular-section-create simple-section type start end) nil)) |
---|
| 1894 | |
---|
| 1895 | ;; get next simple section and its start |
---|
| 1896 | simple-sections (cdr simple-sections) |
---|
| 1897 | start end)) |
---|
| 1898 | |
---|
| 1899 | ;; care about last simple section |
---|
| 1900 | (setq simple-section (car simple-sections) |
---|
| 1901 | type (singular-simple-sec-type simple-section) |
---|
| 1902 | end (if simple-section |
---|
| 1903 | (singular-simple-sec-end simple-section) |
---|
| 1904 | ;; the `1-' is OK since REG-BEG < REG-END. |
---|
| 1905 | ;; here we need that no narrowing is in effect |
---|
| 1906 | (singular-simple-sec-end-at (1- reg-end)))) |
---|
| 1907 | (setcdr sections-end |
---|
| 1908 | (cons (singular-section-create simple-section type start end) nil)) |
---|
| 1909 | |
---|
| 1910 | ;; we should not forget to pop off our auxilliary cons-cell |
---|
| 1911 | (cdr sections))) |
---|
| 1912 | |
---|
| 1913 | (defun singular-section-mapsection (func sections &optional type-filter negate-filter) |
---|
| 1914 | "Apply FUNC to each section in SECTIONS, and make a list of the results. |
---|
| 1915 | If optional argument TYPE-FILTER is non-nil it should be a list of section |
---|
| 1916 | types. FUNC is then applied only to those sections with type occuring in |
---|
| 1917 | TYPE-FILTER. If in addition optional argument NEGATE-FILTER is non-nil |
---|
| 1918 | FUNC is applied only to those sections with type not occuring in |
---|
| 1919 | TYPE-FILTER. |
---|
| 1920 | |
---|
| 1921 | In any case the length of the list this function returns equals the |
---|
| 1922 | number of sections actually processed." |
---|
| 1923 | (if (not type-filter) |
---|
| 1924 | (mapcar func sections) |
---|
| 1925 | ;; copy the list first |
---|
| 1926 | (let ((sections (copy-sequence sections))) |
---|
| 1927 | ;; filter elements and turn them to t's |
---|
| 1928 | (setq sections |
---|
| 1929 | (mapcar (function |
---|
| 1930 | (lambda (section) |
---|
| 1931 | ;; that strange expression evaluates to t iff the |
---|
| 1932 | ;; section should be removed. The `not' is to |
---|
| 1933 | ;; canonize boolean values to t or nil, resp. |
---|
| 1934 | (or (eq (not (memq (singular-section-type section) type-filter)) |
---|
| 1935 | (not negate-filter)) |
---|
| 1936 | section))) |
---|
| 1937 | sections) |
---|
| 1938 | |
---|
| 1939 | ;; remove t's now |
---|
| 1940 | sections (delq t sections)) |
---|
| 1941 | |
---|
| 1942 | ;; call function for remaining sections |
---|
| 1943 | (mapcar func sections)))) |
---|
[abc5138] | 1944 | ;;}}} |
---|
| 1945 | |
---|
[f50151] | 1946 | ;;{{{ Section miscellaneous |
---|
[71c107] | 1947 | (defun singular-section-check (section &optional no-error) |
---|
| 1948 | "Check whether SECTION is completely accessible and return t if so. |
---|
| 1949 | If otherwise SECTION is restricted either in part or as a whole, this |
---|
| 1950 | function fails with an error or returns nil if optional argument NO-ERROR |
---|
| 1951 | is non-nil." |
---|
| 1952 | (cond ((and (>= (singular-section-start section) (point-min)) |
---|
| 1953 | (<= (singular-section-end section) (point-max))) t) |
---|
| 1954 | (no-error nil) |
---|
| 1955 | (t (error "section is restricted either in part or as a whole")))) |
---|
| 1956 | |
---|
| 1957 | (defun singular-section-to-string (section &optional raw) |
---|
| 1958 | "Get contents of SECTION as a string. |
---|
| 1959 | Returns text between start and end of SECTION. |
---|
| 1960 | Removes prompts from section contents unless optional argument RAW is |
---|
| 1961 | non-nil. |
---|
| 1962 | Narrowing has no effect on this function." |
---|
[213e96] | 1963 | (save-restriction |
---|
| 1964 | (widen) |
---|
[f50151] | 1965 | (let ((string (buffer-substring (singular-section-start section) |
---|
[71c107] | 1966 | (singular-section-end section)))) |
---|
[f50151] | 1967 | (if raw |
---|
| 1968 | string |
---|
[71c107] | 1969 | (singular-prompt-remove-string string))))) |
---|
[213e96] | 1970 | ;;}}} |
---|
| 1971 | |
---|
[f50151] | 1972 | ;;{{{ Section miscellaneous interactive |
---|
| 1973 | (defun singular-section-goto-beginning () |
---|
| 1974 | "Move point to beginning of current section." |
---|
| 1975 | (interactive) |
---|
| 1976 | (goto-char (singular-section-start (singular-section-at (point)))) |
---|
| 1977 | (singular-keep-region-active)) |
---|
[81da6ba] | 1978 | |
---|
[f50151] | 1979 | (defun singular-section-goto-end () |
---|
| 1980 | "Move point to end of current section." |
---|
| 1981 | (interactive) |
---|
| 1982 | (goto-char (singular-section-end (singular-section-at (point)))) |
---|
| 1983 | (singular-keep-region-active)) |
---|
| 1984 | |
---|
| 1985 | (defun singular-section-backward (n) |
---|
| 1986 | "Move backward until encountering the beginning of a section. |
---|
| 1987 | With argument, do this that many times. With N less than zero, call |
---|
| 1988 | `singular-section-forward' with argument -N." |
---|
| 1989 | (interactive "p") |
---|
| 1990 | (while (> n 0) |
---|
| 1991 | (goto-char (singular-section-start (singular-section-before (point)))) |
---|
| 1992 | (setq n (1- n))) |
---|
| 1993 | (if (< n 0) |
---|
| 1994 | (singular-section-forward (- n)) |
---|
| 1995 | (singular-keep-region-active))) |
---|
| 1996 | |
---|
| 1997 | (defun singular-section-forward (n) |
---|
| 1998 | "Move forward until encountering the end of a section. |
---|
| 1999 | With argument, do this that many times. With N less than zero, call |
---|
| 2000 | `singular-section-backward' with argument -N." |
---|
| 2001 | (interactive "p") |
---|
| 2002 | (while (> n 0) |
---|
| 2003 | (goto-char (singular-section-end (singular-section-at (point)))) |
---|
| 2004 | (setq n (1- n))) |
---|
| 2005 | (if (< n 0) |
---|
| 2006 | (singular-section-backward (- n)) |
---|
| 2007 | (singular-keep-region-active))) |
---|
[71c107] | 2008 | |
---|
| 2009 | (defun singular-section-kill (section &optional raw no-error) |
---|
| 2010 | "Kill SECTION. |
---|
| 2011 | Puts the contents of SECTION into the kill ring. Removes prompts from |
---|
| 2012 | contents unless optional argument RAW is non-nil. |
---|
| 2013 | If called interactively, kills section point currently is in. Does a raw |
---|
| 2014 | section kill if called with a prefix argument, otherwise strips prompts. |
---|
| 2015 | Does not kill sections that are restricted either in part or as a whole. |
---|
| 2016 | Rather fails with an error in such cases or silently fails if optional |
---|
| 2017 | argument NO-ERROR is non-nil." |
---|
| 2018 | (interactive (list (singular-section-at (point)) |
---|
| 2019 | current-prefix-arg nil)) |
---|
| 2020 | (when (singular-section-check section no-error) |
---|
| 2021 | (kill-new (singular-section-to-string section raw)) |
---|
| 2022 | (delete-region (singular-section-start section) |
---|
| 2023 | (singular-section-end section)))) |
---|
[81da6ba] | 2024 | ;;}}} |
---|
| 2025 | |
---|
[f50151] | 2026 | ;;{{{ Folding sections for both Emacs and XEmacs |
---|
| 2027 | (defcustom singular-folding-ellipsis "Singular I/O ..." |
---|
| 2028 | "*Ellipsis to show for folded input or output. |
---|
| 2029 | Changing this variable has an immediate effect only if one uses |
---|
| 2030 | \\[customize] to do so. |
---|
| 2031 | However, even then it may be necessary to refresh display completely (using |
---|
| 2032 | \\[recenter], for example) for the new settings to be visible." |
---|
| 2033 | :type 'string |
---|
| 2034 | :initialize 'custom-initialize-default |
---|
| 2035 | :set (function |
---|
| 2036 | (lambda (var value) |
---|
| 2037 | ;; set in all singular buffers |
---|
| 2038 | (singular-map-buffer 'singular-folding-set-ellipsis value) |
---|
| 2039 | (set-default var value))) |
---|
| 2040 | :group 'singular-sections-and-foldings) |
---|
| 2041 | |
---|
| 2042 | (defcustom singular-folding-line-move-ignore-folding t |
---|
| 2043 | "*If non-nil, ignore folded sections when moving point up or down. |
---|
| 2044 | This variable is used to initialize `line-move-ignore-invisible'. However, |
---|
| 2045 | documentation states that setting `line-move-ignore-invisible' to a non-nil |
---|
| 2046 | value may result in a slow-down when moving the point up or down. One |
---|
| 2047 | should try to set this variable to nil if point motion seems too slow. |
---|
| 2048 | |
---|
| 2049 | Changing this variable has an immediate effect only if one uses |
---|
| 2050 | \\[customize] to do so." |
---|
| 2051 | :type 'boolean |
---|
| 2052 | :initialize 'custom-initialize-default |
---|
| 2053 | :set (function |
---|
| 2054 | (lambda (var value) |
---|
| 2055 | ;; set in all singular buffers |
---|
| 2056 | (singular-map-buffer 'set 'line-move-ignore-invisible value) |
---|
| 2057 | (set-default var value))) |
---|
| 2058 | :group 'singular-sections-and-foldings) |
---|
| 2059 | |
---|
| 2060 | (defun singular-folding-set-ellipsis (ellipsis) |
---|
| 2061 | "Set ellipsis to show for folded input or output in current buffer." |
---|
| 2062 | (cond |
---|
| 2063 | ;; Emacs |
---|
| 2064 | ((eq singular-emacs-flavor 'emacs) |
---|
| 2065 | (setq buffer-display-table (or (copy-sequence standard-display-table) |
---|
| 2066 | (make-display-table))) |
---|
| 2067 | (set-display-table-slot buffer-display-table |
---|
| 2068 | 'selective-display (vconcat ellipsis))) |
---|
| 2069 | ;; XEmacs |
---|
| 2070 | (t |
---|
| 2071 | (set-glyph-image invisible-text-glyph ellipsis (current-buffer))))) |
---|
| 2072 | |
---|
| 2073 | (defun singular-folding-init () |
---|
| 2074 | "Initializes folding of sections for the current buffer. |
---|
| 2075 | That includes setting `buffer-invisibility-spec' and the ellipsis to show |
---|
| 2076 | for hidden text. |
---|
| 2077 | |
---|
| 2078 | This function is called at mode initialization time." |
---|
| 2079 | ;; initialize `buffer-invisibility-spec' first |
---|
| 2080 | (let ((singular-invisibility-spec (cons 'singular-interactive-mode t))) |
---|
| 2081 | (if (and (listp buffer-invisibility-spec) |
---|
| 2082 | (not (member singular-invisibility-spec buffer-invisibility-spec))) |
---|
| 2083 | (setq buffer-invisibility-spec |
---|
| 2084 | (cons singular-invisibility-spec buffer-invisibility-spec)) |
---|
| 2085 | (setq buffer-invisibility-spec (list singular-invisibility-spec)))) |
---|
| 2086 | ;; ignore invisible lines on movements |
---|
| 2087 | (set (make-local-variable 'line-move-ignore-invisible) |
---|
| 2088 | singular-folding-line-move-ignore-folding) |
---|
| 2089 | ;; now for the ellipsis |
---|
| 2090 | (singular-folding-set-ellipsis singular-folding-ellipsis)) |
---|
| 2091 | |
---|
| 2092 | (defun singular-folding-fold (section &optional no-error) |
---|
| 2093 | "Fold section SECTION if it is not already folded. |
---|
| 2094 | Does not fold sections that do not end in a newline or that are restricted |
---|
| 2095 | either in part or as a whole. Rather fails with an error in such cases |
---|
| 2096 | or silently fails if optional argument NO-ERROR is non-nil. |
---|
| 2097 | This is for safety only: In both cases the result may be confusing to the |
---|
| 2098 | user." |
---|
| 2099 | (let* ((start (singular-section-start section)) |
---|
| 2100 | (end (singular-section-end section))) |
---|
[71c107] | 2101 | (cond ((not (singular-section-check section no-error)) |
---|
| 2102 | nil) |
---|
[f50151] | 2103 | ((not (eq (char-before end) ?\n)) |
---|
| 2104 | (unless no-error |
---|
[71c107] | 2105 | (error "Section does not end in a newline"))) |
---|
[f50151] | 2106 | ((not (singular-folding-foldedp section)) |
---|
| 2107 | ;; fold but only if not already folded |
---|
| 2108 | (singular-folding-fold-internal section))))) |
---|
| 2109 | |
---|
| 2110 | (defun singular-folding-unfold (section &optional no-error invisibility-overlay-or-extent) |
---|
| 2111 | "Unfold section SECTION if it is not already unfolded. |
---|
| 2112 | Does not unfold sections that are restricted either in part or as a whole. |
---|
| 2113 | Rather fails with an error in such cases or silently fails if optional |
---|
[71c107] | 2114 | argument NO-ERROR is non-nil. |
---|
| 2115 | This is for safety only: The result may be confusing to the user. |
---|
[9ad353] | 2116 | If optional argument INVISIBILITY-OVERLAY-OR-EXTENT is non-nil it should be |
---|
[f50151] | 2117 | the invisibility overlay or extent, respectively, of the section to |
---|
| 2118 | unfold." |
---|
| 2119 | (let* ((start (singular-section-start section)) |
---|
| 2120 | (end (singular-section-end section))) |
---|
[71c107] | 2121 | (cond ((not (singular-section-check section no-error)) |
---|
| 2122 | nil) |
---|
[f50151] | 2123 | ((or invisibility-overlay-or-extent |
---|
| 2124 | (setq invisibility-overlay-or-extent (singular-folding-foldedp section))) |
---|
| 2125 | ;; unfold but only if not already unfolded |
---|
| 2126 | (singular-folding-unfold-internal section invisibility-overlay-or-extent))))) |
---|
| 2127 | |
---|
| 2128 | (defun singular-folding-fold-at-point () |
---|
| 2129 | "Fold section point currently is in. |
---|
| 2130 | Does not fold sections that do not end in a newline or that are restricted |
---|
| 2131 | either in part or as a whole. Rather fails with an error in such cases." |
---|
| 2132 | (interactive) |
---|
| 2133 | (singular-folding-fold (singular-section-at (point)))) |
---|
| 2134 | |
---|
| 2135 | (defun singular-folding-unfold-at-point () |
---|
| 2136 | "Unfold section point currently is in. |
---|
| 2137 | Does not unfold sections that are restricted either in part or as a whole. |
---|
| 2138 | Rather fails with an error in such cases." |
---|
| 2139 | (interactive) |
---|
| 2140 | (singular-folding-unfold (singular-section-at (point)))) |
---|
[abc5138] | 2141 | |
---|
[f50151] | 2142 | (defun singular-folding-fold-latest-output () |
---|
| 2143 | "Fold latest output section. |
---|
| 2144 | Does not fold sections that do not end in a newline or that are restricted |
---|
| 2145 | either in part or as a whole. Rather fails with an error in such cases." |
---|
[68cb1e6] | 2146 | (interactive) |
---|
[f50151] | 2147 | (singular-folding-fold (singular-latest-output-section))) |
---|
| 2148 | |
---|
| 2149 | (defun singular-folding-unfold-latest-output () |
---|
| 2150 | "Unfolds latest output section. |
---|
| 2151 | Does not unfold sections that are restricted either in part or as a whole. |
---|
| 2152 | Rather fails with an error in such cases." |
---|
[68cb1e6] | 2153 | (interactive) |
---|
[f50151] | 2154 | (singular-folding-unfold (singular-latest-output-section))) |
---|
[68cb1e6] | 2155 | |
---|
[f50151] | 2156 | (defun singular-folding-fold-all-output () |
---|
| 2157 | "Fold all complete, unfolded output sections. |
---|
| 2158 | That is, all output sections that are not restricted in part or as a whole |
---|
| 2159 | and that end in a newline." |
---|
[68cb1e6] | 2160 | (interactive) |
---|
[f50151] | 2161 | (singular-section-mapsection (function (lambda (section) (singular-folding-fold section t))) |
---|
| 2162 | (singular-section-in (point-min) (point-max) t) |
---|
| 2163 | '(output))) |
---|
[68cb1e6] | 2164 | |
---|
[f50151] | 2165 | (defun singular-folding-unfold-all-output () |
---|
| 2166 | "Unfold all complete, folded output sections. |
---|
| 2167 | That is, all output sections that are not restricted in part or as a whole." |
---|
| 2168 | (interactive) |
---|
| 2169 | (singular-section-mapsection (function (lambda (section) (singular-folding-unfold section t))) |
---|
| 2170 | (singular-section-in (point-min) (point-max) t) |
---|
| 2171 | '(output))) |
---|
| 2172 | |
---|
| 2173 | (defun singular-folding-toggle-fold-at-point-or-all (&optional arg) |
---|
| 2174 | "Fold or unfold section point currently is in or all output sections. |
---|
| 2175 | Without prefix argument, folds unfolded sections and unfolds folded |
---|
| 2176 | sections. With prefix argument, folds all output sections if argument is |
---|
| 2177 | positive, otherwise unfolds all output sections. |
---|
| 2178 | Does neither fold nor unfold sections that do not end in a newline or that |
---|
| 2179 | are restricted either in part or as a whole. Rather fails with an error in |
---|
| 2180 | such cases." |
---|
| 2181 | (interactive "P") |
---|
| 2182 | (cond ((not arg) |
---|
| 2183 | ;; fold or unfold section at point |
---|
| 2184 | (let* ((section (singular-section-at (point))) |
---|
| 2185 | (invisibility-overlay-or-extent (singular-folding-foldedp section))) |
---|
| 2186 | (if invisibility-overlay-or-extent |
---|
| 2187 | (singular-folding-unfold section nil invisibility-overlay-or-extent) |
---|
| 2188 | (singular-folding-fold section)))) |
---|
| 2189 | ((> (prefix-numeric-value arg) 0) |
---|
| 2190 | (singular-folding-fold-all-output)) |
---|
| 2191 | (t |
---|
| 2192 | (singular-folding-unfold-all-output)))) |
---|
| 2193 | |
---|
| 2194 | (defun singular-folding-toggle-fold-latest-output (&optional arg) |
---|
| 2195 | "Fold or unfold latest output section. |
---|
| 2196 | Folds unfolded sections and unfolds folded sections. |
---|
| 2197 | Does neither fold nor unfold sections that do not end in a newline or that |
---|
| 2198 | are restricted either in part or as a whole. Rather fails with an error in |
---|
| 2199 | such cases." |
---|
[68cb1e6] | 2200 | (interactive) |
---|
[f50151] | 2201 | (let* ((section (singular-latest-output-section)) |
---|
| 2202 | (invisibility-overlay-or-extent (singular-folding-foldedp section))) |
---|
| 2203 | (if invisibility-overlay-or-extent |
---|
| 2204 | (singular-folding-unfold section nil invisibility-overlay-or-extent) |
---|
| 2205 | (singular-folding-fold section)))) |
---|
| 2206 | |
---|
| 2207 | ;; Note: |
---|
| 2208 | ;; |
---|
| 2209 | ;; The rest of the folding is either marked as |
---|
| 2210 | ;; Emacs |
---|
| 2211 | ;; or |
---|
| 2212 | ;; XEmacs |
---|
| 2213 | |
---|
| 2214 | (singular-fset 'singular-folding-fold-internal |
---|
| 2215 | 'singular-emacs-folding-fold-internal |
---|
| 2216 | 'singular-xemacs-folding-fold-internal) |
---|
| 2217 | |
---|
| 2218 | (singular-fset 'singular-folding-unfold-internal |
---|
| 2219 | 'singular-emacs-folding-unfold-internal |
---|
| 2220 | 'singular-xemacs-folding-unfold-internal) |
---|
| 2221 | |
---|
| 2222 | (singular-fset 'singular-folding-foldedp |
---|
| 2223 | 'singular-emacs-folding-foldedp-internal |
---|
| 2224 | 'singular-xemacs-folding-foldedp-internal) |
---|
| 2225 | ;;}}} |
---|
| 2226 | |
---|
| 2227 | ;;{{{ Folding sections for Emacs |
---|
| 2228 | |
---|
| 2229 | ;; Note: |
---|
| 2230 | ;; |
---|
| 2231 | ;; For Emacs, we use overlays to hide text (so-called "invisibility |
---|
| 2232 | ;; overlays"). In addition to their `invisible' property, they have the |
---|
| 2233 | ;; `singular-invisible' property set. Setting the intangible property does |
---|
| 2234 | ;; not work very well for Emacs. We use the variable |
---|
| 2235 | ;; `line-move-ignore-invisible' which works quite well. |
---|
| 2236 | |
---|
| 2237 | (defun singular-emacs-folding-fold-internal (section) |
---|
| 2238 | "Fold section SECTION. |
---|
| 2239 | SECTION should end in a newline. That terminal newline is not |
---|
| 2240 | folded or otherwise ellipsis does not appear. |
---|
| 2241 | SECTION should be unfolded." |
---|
| 2242 | (let* ((start (singular-section-start section)) |
---|
| 2243 | ;; do not make trailing newline invisible |
---|
| 2244 | (end (1- (singular-section-end section))) |
---|
| 2245 | invisibility-overlay) |
---|
| 2246 | ;; create new overlay and add properties |
---|
| 2247 | (setq invisibility-overlay (make-overlay start end)) |
---|
| 2248 | ;; mark them as invisibility overlays |
---|
| 2249 | (overlay-put invisibility-overlay 'singular-invisible t) |
---|
| 2250 | ;; set invisible properties |
---|
| 2251 | (overlay-put invisibility-overlay 'invisible 'singular-interactive-mode) |
---|
| 2252 | ;; evaporate empty invisibility overlays |
---|
| 2253 | (overlay-put invisibility-overlay 'evaporate t))) |
---|
| 2254 | |
---|
| 2255 | (defun singular-emacs-folding-unfold-internal (section &optional invisibility-overlay) |
---|
| 2256 | "Unfold section SECTION. |
---|
| 2257 | SECTION should be folded. |
---|
| 2258 | If optional argument INVISIBILITY-OVERLAY is non-nil it should be the |
---|
| 2259 | invisibility overlay of the section to unfold." |
---|
| 2260 | (let ((invisibility-overlay |
---|
| 2261 | (or invisibility-overlay |
---|
| 2262 | (singular-emacs-folding-foldedp-internal section)))) |
---|
| 2263 | ;; to keep number of overlays low we delete it |
---|
| 2264 | (delete-overlay invisibility-overlay))) |
---|
| 2265 | |
---|
| 2266 | (defun singular-emacs-folding-foldedp-internal (section) |
---|
| 2267 | "Returns non-nil iff SECTION is folded. |
---|
| 2268 | More specifically, returns the invisibility overlay if there is one. |
---|
| 2269 | Narrowing has no effect on this function." |
---|
| 2270 | (let* ((start (singular-section-start section)) |
---|
| 2271 | (overlays (overlays-at start)) |
---|
| 2272 | invisibility-overlay) |
---|
| 2273 | ;; check for invisibility overlay |
---|
| 2274 | (while (and overlays (not invisibility-overlay)) |
---|
| 2275 | (if (overlay-get (car overlays) 'singular-invisible) |
---|
| 2276 | (setq invisibility-overlay (car overlays)) |
---|
| 2277 | (setq overlays (cdr overlays)))) |
---|
| 2278 | invisibility-overlay)) |
---|
| 2279 | ;;}}} |
---|
| 2280 | |
---|
| 2281 | ;;{{{ Folding sections for XEmacs |
---|
| 2282 | |
---|
| 2283 | ;; Note: |
---|
| 2284 | ;; |
---|
| 2285 | ;; For XEmacs, we use extents to hide text (so-called "invisibility |
---|
| 2286 | ;; extents"). In addition to their `invisible' property, they have the |
---|
| 2287 | ;; `singular-invisible' property set. To ignore invisible text we use the |
---|
| 2288 | ;; variable `line-move-ignore-invisible' which works quite well. |
---|
| 2289 | |
---|
| 2290 | (defun singular-xemacs-folding-fold-internal (section) |
---|
| 2291 | "Fold section SECTION. |
---|
| 2292 | SECTION should end in a newline. That terminal newline is not |
---|
| 2293 | folded or otherwise ellipsis does not appear. |
---|
| 2294 | SECTION should be unfolded." |
---|
| 2295 | (let* ((start (singular-section-start section)) |
---|
| 2296 | ;; do not make trailing newline invisible |
---|
| 2297 | (end (1- (singular-section-end section))) |
---|
| 2298 | invisibility-extent) |
---|
| 2299 | ;; create new extent and add properties |
---|
| 2300 | (setq invisibility-extent (make-extent start end)) |
---|
| 2301 | ;; mark them as invisibility extents |
---|
| 2302 | (set-extent-property invisibility-extent 'singular-invisible t) |
---|
| 2303 | ;; set invisible properties |
---|
| 2304 | (set-extent-property invisibility-extent 'invisible 'singular-interactive-mode))) |
---|
| 2305 | |
---|
| 2306 | (defun singular-xemacs-folding-unfold-internal (section &optional invisibility-extent) |
---|
| 2307 | "Unfold section SECTION. |
---|
| 2308 | SECTION should be folded. |
---|
| 2309 | If optional argument INVISIBILITY-EXTENT is non-nil it should be the |
---|
| 2310 | invisibility extent of the section to unfold." |
---|
| 2311 | (let ((invisibility-extent |
---|
| 2312 | (or invisibility-extent |
---|
| 2313 | (singular-xemacs-folding-foldedp-internal section)))) |
---|
| 2314 | ;; to keep number of extents low we delete it |
---|
| 2315 | (delete-extent invisibility-extent))) |
---|
| 2316 | |
---|
| 2317 | (defun singular-xemacs-folding-foldedp-internal (section) |
---|
| 2318 | "Returns non-nil iff SECTION is folded. |
---|
| 2319 | More specifically, returns the invisibility extent if there is one. |
---|
| 2320 | Narrowing has no effect on this function." |
---|
| 2321 | ;; do not try to use `extent-at' at this point. `extent-at' does not |
---|
| 2322 | ;; return extents outside narrowed text. |
---|
| 2323 | (let* ((start (singular-section-start section)) |
---|
| 2324 | (invisibility-extent (map-extents |
---|
| 2325 | (function (lambda (ext args) ext)) |
---|
| 2326 | nil start start nil nil 'singular-invisible))) |
---|
| 2327 | invisibility-extent)) |
---|
[845cb34] | 2328 | ;;}}} |
---|
[abc5138] | 2329 | |
---|
[f50151] | 2330 | ;;{{{ Online help |
---|
[2135c8a] | 2331 | |
---|
[f50151] | 2332 | ;; Note: |
---|
| 2333 | ;; |
---|
| 2334 | ;; Catching user's help commands to Singular and translating them to calls |
---|
[71c107] | 2335 | ;; to `info' is quite a difficult task due to the asynchronous nature of |
---|
[f50151] | 2336 | ;; communication with Singular. We use an heuristic approach which should |
---|
| 2337 | ;; work in most cases: |
---|
[71c107] | 2338 | ;; |
---|
| 2339 | ;; - `singular-help-pre-input-filter' scans user's input for help commands. |
---|
| 2340 | ;; If user issues a help command the filter sets a time stamp and passes |
---|
| 2341 | ;; the input unchanged to Singular. |
---|
| 2342 | ;; - Singular receives the help command and barfs that it could not process |
---|
| 2343 | ;; it. We call that error message "Singular's response". That response |
---|
| 2344 | ;; in particular contains the help topic the user requested. If the |
---|
| 2345 | ;; response for some reasons is not recognized and filtered in the later |
---|
| 2346 | ;; steps the user gets some reasonable response on her command that way. |
---|
| 2347 | ;; - `singular-help-pre-output-filter' on each output from Singular checks |
---|
| 2348 | ;; (using the time stamp set by `singular-help-pre-input-filter') whether |
---|
| 2349 | ;; the user issued a help command at most one second ago. If so, |
---|
| 2350 | ;; `singular-help-pre-output-filter' starts checking Singular's output |
---|
| 2351 | ;; for the response on the help command. If it finds one it remembers |
---|
| 2352 | ;; the help topic in `singular-help-topic' and removes the response from |
---|
| 2353 | ;; Singular's output. |
---|
| 2354 | ;; There is some extra magic built into the filter to handle responses |
---|
| 2355 | ;; from Singular which are received by emacs not in one string but in |
---|
| 2356 | ;; more than one piece (we call that pending output). |
---|
| 2357 | ;; - As the last step step of this procedure, `singular-post-output-filter' |
---|
| 2358 | ;; fires up an Info buffer using `singular-help' if the variable |
---|
| 2359 | ;; `singular-help-topic' is non-nil. This step is separated from the |
---|
| 2360 | ;; previous one since joining both leads to some trouble in point |
---|
| 2361 | ;; management. This is mainly due to the fact that `singular-help' opens |
---|
| 2362 | ;; a new window. |
---|
| 2363 | ;; |
---|
| 2364 | ;; To show some online help, the online help manual has to be available, of |
---|
| 2365 | ;; course. There is a number of possibilites for the user to set the file |
---|
| 2366 | ;; name of the manual explicitly, as described in the documentation string |
---|
| 2367 | ;; to `singular-help'. But in general the file name should be recognized |
---|
| 2368 | ;; automatically by Singular interactive mode. For that to work, Singular |
---|
| 2369 | ;; prints the file name when it comes up and option `--emacs' is specified. |
---|
| 2370 | ;; This is recognized by `singular-scan-header-pre-output-filter' which |
---|
| 2371 | ;; sets the variable `singular-help-file-name' accordingly. For more |
---|
| 2372 | ;; information one should refer to the `Header scanning ...' folding. |
---|
| 2373 | ;; |
---|
| 2374 | ;; Another variable which needs to be set for proper operation is |
---|
| 2375 | ;; `singular-help-topics-alist' for completion of help topics and for |
---|
| 2376 | ;; recognition of help topics around point. It is no error for this |
---|
| 2377 | ;; variable not to be set: simply the features do not work then. |
---|
[f50151] | 2378 | |
---|
[71c107] | 2379 | ;; this `require' is necessary since we use functions from the Info package |
---|
| 2380 | ;; which are not declared as `autoload' |
---|
[f50151] | 2381 | (require 'info) |
---|
| 2382 | |
---|
| 2383 | (defcustom singular-help-same-window 'default |
---|
[71c107] | 2384 | "*Specifies how to open the window for Singular online help. |
---|
| 2385 | If this variable equals t, Singular online help comes up in the selected |
---|
[f50151] | 2386 | window. |
---|
| 2387 | If this variable equals nil, Singular online help comes up in another |
---|
[71c107] | 2388 | window. |
---|
| 2389 | If this variable equals neither t nor nil, the standard Emacs behaviour to |
---|
| 2390 | open the Info buffer is adopted (which very much depends on the settings of |
---|
| 2391 | `same-window-buffer-names')." |
---|
[f50151] | 2392 | :initialize 'custom-initialize-default |
---|
| 2393 | :type '(choice (const :tag "This window" t) |
---|
| 2394 | (const :tag "Other window" nil) |
---|
| 2395 | (const :tag "Default" default)) |
---|
| 2396 | :group 'singular-interactive-miscellaneous) |
---|
| 2397 | |
---|
| 2398 | (defcustom singular-help-explicit-file-name nil |
---|
[71c107] | 2399 | "*Specifies the file name of the Singular online manual. |
---|
| 2400 | If non-nil, used as file name of the Singular online manual. |
---|
| 2401 | |
---|
| 2402 | This variable should be customized only if all other attempts of Singular |
---|
| 2403 | interactive mode fail to determine the file name of the Singular online |
---|
| 2404 | manual. For more information one should refer to the `singular-help' |
---|
| 2405 | function." |
---|
[f50151] | 2406 | :initialize 'custom-initialize-default |
---|
[71c107] | 2407 | :type '(choice (const nil) file) |
---|
[f50151] | 2408 | :group 'singular-interactive-miscellaneous) |
---|
| 2409 | |
---|
[aecc57] | 2410 | (defvar singular-help-file-name nil |
---|
[71c107] | 2411 | "File name of the Singular online manual. |
---|
| 2412 | This variable should not be modified by the user. |
---|
[aecc57] | 2413 | |
---|
| 2414 | This variable is buffer-local.") |
---|
| 2415 | |
---|
[71c107] | 2416 | (defconst singular-help-fall-back-file-name "singular.hlp" |
---|
| 2417 | "Fall-back file name of the Singular online manual. |
---|
| 2418 | This variable is used if the file name of the Singular online manual cannot |
---|
| 2419 | be determined otherwise.") |
---|
| 2420 | |
---|
| 2421 | (defvar singular-help-time-stamp '(0 0) |
---|
| 2422 | "The time stamp that is set when the user issues a help command. |
---|
[f50151] | 2423 | |
---|
| 2424 | This variable is buffer-local.") |
---|
| 2425 | |
---|
| 2426 | (defvar singular-help-response-pending nil |
---|
[71c107] | 2427 | "If non-nil, Singular's response has not been completely received. |
---|
[f50151] | 2428 | |
---|
| 2429 | This variable is buffer-local.") |
---|
| 2430 | |
---|
| 2431 | (defvar singular-help-topic nil |
---|
[71c107] | 2432 | "If non-nil, contains help topic to show in post output filter. |
---|
[f50151] | 2433 | |
---|
| 2434 | This variable is buffer-local.") |
---|
| 2435 | |
---|
[71c107] | 2436 | (defconst singular-help-command-regexp "^\\s-*\\(help\\|\?\\)" |
---|
[f50151] | 2437 | "Regular expression to match Singular help commands.") |
---|
| 2438 | |
---|
| 2439 | (defconst singular-help-response-line-1 |
---|
[71c107] | 2440 | "^// \\*\\* Your help command could not be executed\\. Use\n" |
---|
| 2441 | "Regular expression that matches the first line of Singular's response.") |
---|
[f50151] | 2442 | |
---|
| 2443 | (defconst singular-help-response-line-2 |
---|
[71c107] | 2444 | "^// \\*\\* C-h C-s \\(.*\\)\n" |
---|
| 2445 | "Regular expression that matches the second line of Singular's response. |
---|
| 2446 | First subexpression matches help topic.") |
---|
[f50151] | 2447 | |
---|
| 2448 | (defconst singular-help-response-line-3 |
---|
[71c107] | 2449 | "^// \\*\\* to enter the Singular online help\\. For general\n" |
---|
| 2450 | "Regular expression that matches the third line of Singular's response.") |
---|
[f50151] | 2451 | |
---|
| 2452 | (defconst singular-help-response-line-4 |
---|
[71c107] | 2453 | "^// \\*\\* information on Singular running under Emacs, type C-h m\\.\n" |
---|
| 2454 | "Regular expression that matches the fourth line of Singular's response.") |
---|
[f50151] | 2455 | |
---|
| 2456 | (defun singular-help-pre-input-filter (input) |
---|
| 2457 | "Check user's input for help commands. |
---|
[71c107] | 2458 | Sets time stamp if one is found. Passes user's input on to Singular |
---|
| 2459 | unchanged." |
---|
[f50151] | 2460 | (if (string-match singular-help-command-regexp input) |
---|
[71c107] | 2461 | (setq singular-help-time-stamp (current-time))) |
---|
[f50151] | 2462 | ;; return nil so that input passes unchanged |
---|
| 2463 | nil) |
---|
| 2464 | |
---|
| 2465 | (defun singular-help-pre-output-filter (output) |
---|
| 2466 | "Check for Singular's response on a help command. |
---|
[71c107] | 2467 | Removes it and sets `singular-help-topic' accordingly." |
---|
[f50151] | 2468 | ;; check first |
---|
[71c107] | 2469 | ;; - whether a help statement has been issued at most one second ago, or |
---|
[f50151] | 2470 | ;; - whether there is a pending response. |
---|
| 2471 | ;; Only if one of these conditions is met we go on and check text for a |
---|
| 2472 | ;; response on a help command. Checking uncoditionally every piece of |
---|
| 2473 | ;; output would be far too expensive. |
---|
| 2474 | ;; If check fails nil is returned, what is exactly what we need for the |
---|
| 2475 | ;; filter. |
---|
[71c107] | 2476 | (if (or (= (cadr (current-time)) (cadr singular-help-time-stamp)) |
---|
[f50151] | 2477 | singular-help-response-pending) |
---|
| 2478 | ;; if response is pending for more than five seconds, give up |
---|
| 2479 | (if (and singular-help-response-pending |
---|
| 2480 | (> (singular-time-stamp-difference (current-time) singular-help-time-stamp) 5)) |
---|
| 2481 | ;; this command returns nil, what is exactly what we need for the filter |
---|
| 2482 | (setq singular-help-response-pending nil) |
---|
[71c107] | 2483 | ;; go through output, removing the response. If there is a |
---|
| 2484 | ;; pending response we nevertheless check for all lines, not only |
---|
| 2485 | ;; for the pending one. At last, pending responses should not |
---|
| 2486 | ;; occur to often. |
---|
| 2487 | (when (string-match singular-help-response-line-1 output) |
---|
| 2488 | (setq output (replace-match "" t t output)) |
---|
| 2489 | (setq singular-help-response-pending t)) |
---|
| 2490 | (when (string-match singular-help-response-line-2 output) |
---|
| 2491 | ;; after all, we found what we are looking for |
---|
| 2492 | (setq singular-help-topic (substring output (match-beginning 1) (match-end 1))) |
---|
| 2493 | (setq output (replace-match "" t t output)) |
---|
| 2494 | (setq singular-help-response-pending t)) |
---|
| 2495 | (when (string-match singular-help-response-line-3 output) |
---|
| 2496 | (setq output (replace-match "" t t output)) |
---|
| 2497 | (setq singular-help-response-pending t)) |
---|
| 2498 | (when (string-match singular-help-response-line-4 output) |
---|
| 2499 | (setq output (replace-match "" t t output)) |
---|
| 2500 | ;; we completely removed the help from output! |
---|
| 2501 | (setq singular-help-response-pending nil)) |
---|
| 2502 | |
---|
| 2503 | ;; return modified OUTPUT |
---|
| 2504 | output))) |
---|
[f50151] | 2505 | |
---|
| 2506 | (defun singular-help-post-output-filter (&rest ignore) |
---|
[71c107] | 2507 | "Call `singular-help' if `singular-help-topic' is non-nil." |
---|
[f50151] | 2508 | (when singular-help-topic |
---|
[e74240] | 2509 | (save-excursion |
---|
| 2510 | (singular-help singular-help-topic)) |
---|
[f50151] | 2511 | (setq singular-help-topic nil))) |
---|
| 2512 | |
---|
[71c107] | 2513 | (defvar singular-help-topic-history nil |
---|
| 2514 | "History of help topics used as arguments to `singular-help'.") |
---|
[f50151] | 2515 | |
---|
[71c107] | 2516 | (defun singular-help (&optional help-topic) |
---|
| 2517 | "Show help on HELP-TOPIC in Singular online manual. |
---|
| 2518 | |
---|
| 2519 | The file name of the Singular online manual is determined in the following |
---|
| 2520 | manner: |
---|
| 2521 | o if the \(customizable) variable `singular-help-explicit-file-name' is |
---|
| 2522 | non-nil, it is used as file name; |
---|
| 2523 | o otherwise, if the variable `singular-help-file-name' is non-nil, is is |
---|
| 2524 | used as file name. This variable should be set by Singular interactive |
---|
| 2525 | mode itself, but there may be instances where this fails. Anyway, it |
---|
| 2526 | should be not set by the user. |
---|
| 2527 | o otherwise, if the environment variable SINGULAR_INFO_FILE is set, it is |
---|
| 2528 | used as file name; |
---|
| 2529 | o otherwise, the constant `singular-help-fall-back-file-name' is used |
---|
| 2530 | as file name." |
---|
| 2531 | (interactive |
---|
| 2532 | (list (completing-read "Help topic: " singular-help-topics-alist |
---|
| 2533 | nil nil nil 'singular-help-topic-history))) |
---|
| 2534 | |
---|
| 2535 | ;; get help file and topic |
---|
[fffd22] | 2536 | (let ((help-file-name (or singular-help-explicit-file-name |
---|
[71c107] | 2537 | singular-help-file-name |
---|
| 2538 | (getenv "SINGULAR_INFO_FILE") |
---|
[fffd22] | 2539 | singular-help-fall-back-file-name)) |
---|
| 2540 | (help-topic (cond ((or (null help-topic) |
---|
| 2541 | (string= help-topic "")) |
---|
| 2542 | "Top") |
---|
| 2543 | ;; try to get the real topic from the alist. |
---|
| 2544 | ;; It's OK if the alist is empty. |
---|
| 2545 | ((cdr (assoc help-topic |
---|
| 2546 | singular-help-topics-alist))) |
---|
| 2547 | (t help-topic))) |
---|
| 2548 | (continue t)) |
---|
| 2549 | |
---|
| 2550 | ;; pop to Info buffer |
---|
[71c107] | 2551 | (singular-pop-to-buffer singular-help-same-window "*info*") |
---|
| 2552 | |
---|
[fffd22] | 2553 | ;; test whether we are already in Singular's online manual |
---|
| 2554 | (unless (and (boundp 'Info-current-file) |
---|
| 2555 | (equal Info-current-file help-file-name)) |
---|
| 2556 | ;; jump to Singular's top node |
---|
| 2557 | (condition-case signal |
---|
| 2558 | (Info-find-node help-file-name "Top") |
---|
| 2559 | ;; in case of an error jump to info directory |
---|
| 2560 | (error |
---|
| 2561 | (Info-directory) |
---|
[71c107] | 2562 | ;; if we have been called interactively we pass the error down, |
---|
[fffd22] | 2563 | ;; otherwise we assumes that we have been called from a hook and |
---|
| 2564 | ;; call `singular-error' |
---|
[71c107] | 2565 | (if (interactive-p) |
---|
| 2566 | (signal (car signal) (cdr signal)) |
---|
[fffd22] | 2567 | (singular-error "Singular online manual %s not found" |
---|
| 2568 | help-file-name)) |
---|
| 2569 | ;; do not continue |
---|
| 2570 | (setq continue nil)))) |
---|
| 2571 | |
---|
| 2572 | (when continue |
---|
| 2573 | ;; jump to desired node |
---|
| 2574 | (condition-case signal |
---|
| 2575 | (Info-goto-node help-topic) |
---|
| 2576 | ;; in case of an error jump to Singular's top node |
---|
| 2577 | (error |
---|
| 2578 | (Info-goto-node "Top") |
---|
| 2579 | ;; if we have been called interactively we pass the error down, |
---|
| 2580 | ;; otherwise we assumes that we have been called from a hook and |
---|
| 2581 | ;; call `singular-error' |
---|
| 2582 | (if (interactive-p) |
---|
| 2583 | (signal (car signal) (cdr signal)) |
---|
| 2584 | (singular-error "Singular help topic %s not found" |
---|
| 2585 | help-topic))))))) |
---|
[f50151] | 2586 | |
---|
| 2587 | (defun singular-help-init () |
---|
| 2588 | "Initialize online help support for Singular interactive mode. |
---|
| 2589 | |
---|
| 2590 | This function is called at mode initialization time." |
---|
[71c107] | 2591 | (make-local-variable 'singular-help-file-name) |
---|
[f50151] | 2592 | (make-local-variable 'singular-help-time-stamp) |
---|
| 2593 | (make-local-variable 'singular-help-response-pending) |
---|
| 2594 | (make-local-variable 'singular-help-topic) |
---|
| 2595 | (add-hook 'singular-pre-input-filter-functions 'singular-help-pre-input-filter) |
---|
| 2596 | (add-hook 'singular-pre-output-filter-functions 'singular-help-pre-output-filter) |
---|
| 2597 | (add-hook 'singular-post-output-filter-functions 'singular-help-post-output-filter)) |
---|
| 2598 | ;;}}} |
---|
| 2599 | |
---|
[aecc57] | 2600 | ;;{{{ Singular commands, help topics and standard libraries alists |
---|
| 2601 | (defvar singular-commands-alist nil |
---|
| 2602 | "An alist containing all Singular commands to complete. |
---|
| 2603 | |
---|
| 2604 | This variable is buffer-local.") |
---|
| 2605 | |
---|
| 2606 | (defvar singular-help-topics-alist nil |
---|
| 2607 | "An alist containg all Singular help topics to complete. |
---|
| 2608 | |
---|
| 2609 | This variable is buffer-local.") |
---|
| 2610 | |
---|
| 2611 | (defvar singular-standard-libraries-alist nil |
---|
| 2612 | "An alist containing all Singular standard libraries names. |
---|
| 2613 | |
---|
| 2614 | This variable is buffer-local.") |
---|
| 2615 | ;;}}} |
---|
| 2616 | |
---|
[e5e79a] | 2617 | ;;{{{ Scanning of header and handling of emacs home directory |
---|
| 2618 | ;; |
---|
| 2619 | ;; Scanning of header |
---|
| 2620 | ;; |
---|
[aecc57] | 2621 | (defconst singular-scan-header-emacs-home-regexp "^// \\*\\* EmacsDir: \\(.+\\)\n" |
---|
[e5e79a] | 2622 | "Regular expression matching the location of emacs home in Singular |
---|
| 2623 | header.") |
---|
| 2624 | |
---|
[aecc57] | 2625 | (defconst singular-scan-header-info-file-regexp "^// \\*\\* InfoFile: \\(.+\\)\n" |
---|
[e5e79a] | 2626 | "Regular expression matching the location of Singular info file in |
---|
| 2627 | Singular header.") |
---|
| 2628 | |
---|
[aecc57] | 2629 | (defconst singular-scan-header-time-stamp 0 |
---|
[e5e79a] | 2630 | "A time stamp set by singular-scan-header. |
---|
| 2631 | |
---|
| 2632 | This variable is buffer-local.") |
---|
| 2633 | |
---|
[aecc57] | 2634 | (defvar singular-scan-header-scan-for '() |
---|
[e5e79a] | 2635 | "List of things to scan for in Singular header. |
---|
| 2636 | If `singular-scan-header-pre-output-filter' finds one thing in the current |
---|
| 2637 | output, it removes the corresponding value from the list. |
---|
| 2638 | If this variable gets nil, `singular-scan-header-pre-output-filter' is |
---|
| 2639 | removed from the pre-output-filter. |
---|
[aecc57] | 2640 | This variable is initialized in `singular-scan-header-init'. Possible |
---|
| 2641 | values of this list are up to now `help-file' and `emacs-home'. |
---|
[e5e79a] | 2642 | |
---|
| 2643 | This variable is buffer-local.") |
---|
| 2644 | |
---|
[aecc57] | 2645 | (defun singular-scan-header-got-emacs-home () |
---|
| 2646 | "Load Singular completion and libraries files. |
---|
| 2647 | Assumes that `singular-emacs-home-directory' is set to the appropriate |
---|
| 2648 | value and loads the files \"cmd-cmpl.el\", \"hlp-cmpl.el\", and |
---|
| 2649 | \"lib-cmpl.el\". |
---|
| 2650 | On success calls `singular-menu-install-libraries'." |
---|
| 2651 | (or (load (singular-expand-emacs-file-name "cmd-cmpl.el" t) t t t) |
---|
| 2652 | (message "Can't find command completion file! Command completion disabled.")) |
---|
| 2653 | (or (load (singular-expand-emacs-file-name "hlp-cmpl.el" t) t t t) |
---|
| 2654 | (message "Can't find help topic completion file! Help completion disabled.")) |
---|
| 2655 | (if (load (singular-expand-emacs-file-name "lib-cmpl.el" t) t t t) |
---|
| 2656 | (singular-menu-install-libraries) |
---|
| 2657 | (message "Can't find library index file!"))) |
---|
| 2658 | |
---|
| 2659 | |
---|
[e5e79a] | 2660 | (defun singular-scan-header-pre-output-filter (output) |
---|
[c100d9] | 2661 | "Filter function for hook `singular-pro-output-filter-functions'. |
---|
| 2662 | Scans the Singular header for special markers using the regexps |
---|
| 2663 | `singular-scan-header-info-file-regexp' and |
---|
| 2664 | `singular-scan-header-emacs-home-regexp', removes them, loads the |
---|
| 2665 | completion files, the library-list file, calls |
---|
| 2666 | `singular-menu-install-libraries' and sets `singular-help-file-name'. |
---|
| 2667 | Removes itself from the hook if all special markers were found or if it has |
---|
| 2668 | been searching for more than 20 seconds." |
---|
| 2669 | (singular-debug 'interactive (message "scanning header")) |
---|
[e5e79a] | 2670 | (let ((changed nil)) |
---|
| 2671 | |
---|
| 2672 | ;; Search for emacs home directory |
---|
| 2673 | (when (string-match singular-scan-header-emacs-home-regexp output) |
---|
[aecc57] | 2674 | (let ((emacs-home (substring output (match-beginning 1) (match-end 1)))) |
---|
| 2675 | (singular-debug 'interactive |
---|
| 2676 | (message "scan header: emacs home path found")) |
---|
| 2677 | ;; in any case, remove marker from output |
---|
| 2678 | (setq output (replace-match "" t t output)) |
---|
| 2679 | (setq changed t) |
---|
| 2680 | ;; if not already done, do action an singular-emacs-home |
---|
| 2681 | (when (memq 'emacs-home singular-scan-header-scan-for) |
---|
| 2682 | (singular-debug 'interactive (message "scan header: initializing emacs-home-directory")) |
---|
| 2683 | (setq singular-scan-header-scan-for (delq 'emacs-home singular-scan-header-scan-for)) |
---|
| 2684 | (setq singular-emacs-home-directory emacs-home) |
---|
| 2685 | (singular-scan-header-got-emacs-home)))) |
---|
[e5e79a] | 2686 | |
---|
| 2687 | ;; Search for Singular info file |
---|
| 2688 | (when (string-match singular-scan-header-info-file-regexp output) |
---|
[aecc57] | 2689 | (let ((file-name (substring output (match-beginning 1) (match-end 1)))) |
---|
| 2690 | (singular-debug 'interactive |
---|
| 2691 | (message "scan header: singular.hlp path found")) |
---|
| 2692 | ;; in any case, remove marker from output |
---|
| 2693 | (setq output (replace-match "" t t output)) |
---|
| 2694 | (setq changed t) |
---|
| 2695 | ;; if not already done, do action on help-file-name |
---|
| 2696 | (when (memq 'info-file singular-scan-header-scan-for) |
---|
| 2697 | (singular-debug 'interactive (message "scan header: initializing help-file-name")) |
---|
| 2698 | (setq singular-scan-header-scan-for (delq 'info-file singular-scan-header-scan-for)) |
---|
| 2699 | (setq singular-help-file-name file-name)))) |
---|
[e5e79a] | 2700 | |
---|
| 2701 | ;; Remove from hook if everything is found or if we already waited |
---|
| 2702 | ;; too long. |
---|
| 2703 | (if (or (eq singular-scan-header-scan-for nil) |
---|
[c100d9] | 2704 | (> (singular-time-stamp-difference (current-time) singular-scan-header-time-stamp) 20)) |
---|
[e5e79a] | 2705 | (remove-hook 'singular-pre-output-filter-functions 'singular-scan-header-pre-output-filter)) |
---|
| 2706 | |
---|
| 2707 | ;; Return new output string if we changed it, nil otherwise |
---|
| 2708 | (and changed output))) |
---|
| 2709 | |
---|
[c100d9] | 2710 | (defun singular-scan-header-init () |
---|
| 2711 | "Initialize scanning of header for Singular interactive mode. |
---|
| 2712 | |
---|
| 2713 | This function is called by `singular-exec'." |
---|
| 2714 | (singular-debug 'interactive (message "Initializing scan-header")) |
---|
| 2715 | (set (make-local-variable 'singular-scan-header-time-stamp) (current-time)) |
---|
[aecc57] | 2716 | (set (make-local-variable 'singular-scan-header-scan-for) '()) |
---|
| 2717 | |
---|
| 2718 | (make-local-variable 'singular-emacs-home-directory) |
---|
| 2719 | ;; if singular-emacs-home is set try to load the completion files. |
---|
| 2720 | ;; Otherwise set marker that we still have to search for it. |
---|
| 2721 | (if singular-emacs-home-directory |
---|
| 2722 | (singular-scan-header-got-emacs-home) |
---|
| 2723 | (setq singular-scan-header-scan-for (append singular-scan-header-scan-for '(emacs-home)))) |
---|
| 2724 | |
---|
[c100d9] | 2725 | ;; Up to now this seems to be the best place to initialize |
---|
| 2726 | ;; `singular-help-file-name' since singular-help gets initialized |
---|
| 2727 | ;; only on mode start-up, not on Singular start-up |
---|
[aecc57] | 2728 | ;; |
---|
| 2729 | ;; if singular-help-file-name is not set, mark, that we have to scan for it |
---|
| 2730 | (make-local-variable 'singular-help-file-name) |
---|
| 2731 | (or singular-help-file-name |
---|
| 2732 | (setq singular-scan-header-scan-for (append singular-scan-header-scan-for '(info-file)))) |
---|
[c100d9] | 2733 | |
---|
| 2734 | (add-hook 'singular-pre-output-filter-functions 'singular-scan-header-pre-output-filter)) |
---|
| 2735 | |
---|
[aecc57] | 2736 | (defun singular-scan-header-exit () |
---|
| 2737 | "Reinitialize scanning of header for Singular interactive mode. |
---|
| 2738 | |
---|
| 2739 | This function is called by `singular-exit-sentinel'." |
---|
| 2740 | ;; unset variables so that all subsequent calls of Singular will |
---|
| 2741 | ;; scan the header. |
---|
| 2742 | (singular-debug 'interactive (message "Deinitializing scan-header")) |
---|
| 2743 | (setq singular-emacs-home-directory nil) |
---|
| 2744 | (setq singular-help-file-name nil)) |
---|
| 2745 | |
---|
[e5e79a] | 2746 | ;; |
---|
| 2747 | ;; handling of emacs home directory |
---|
| 2748 | ;; |
---|
[aecc57] | 2749 | ;; A note on `singular-emacs-home-directory': If this variable is set |
---|
| 2750 | ;; before singular.el is evaluated, the header of the first Singular |
---|
| 2751 | ;; started is NOT searched for the singular-emacs-home-directory. |
---|
| 2752 | ;; Anyhow, all subsequent calls of Singular will scan the header |
---|
| 2753 | ;; regardless of the initial state of this variable. (The exit-sentinel |
---|
| 2754 | ;; will set this variable back to nil.) |
---|
| 2755 | ;; See also `singular-scan-header-exit'. |
---|
[e5e79a] | 2756 | (defvar singular-emacs-home-directory nil |
---|
| 2757 | "Path to the emacs sub-directory of Singular as string. |
---|
[c100d9] | 2758 | `singular-scan-header-pre-output-filter' searches the Singular header for |
---|
| 2759 | the path and sets this variable to the corresponding value. |
---|
| 2760 | Its value is redifined on every start of Singular. |
---|
[e5e79a] | 2761 | |
---|
| 2762 | This variable is buffer-local.") |
---|
| 2763 | |
---|
| 2764 | (defun singular-expand-emacs-file-name (file &optional noerror) |
---|
[c100d9] | 2765 | "Add absolute path of emacs home directory. |
---|
[e5e79a] | 2766 | Adds the content of `singular-emacs-home-directory' to the string FILE. |
---|
| 2767 | If `singular-emacs-home-directory' is nil, return nil and signal |
---|
| 2768 | an error unless optional argument NOERROR is not nil." |
---|
| 2769 | (if singular-emacs-home-directory |
---|
[9ad353] | 2770 | (concat singular-emacs-home-directory |
---|
| 2771 | (if (memq (aref singular-emacs-home-directory |
---|
| 2772 | (1- (length singular-emacs-home-directory))) |
---|
| 2773 | '(?/ ?\\)) |
---|
| 2774 | "" "/") |
---|
| 2775 | file) |
---|
[e5e79a] | 2776 | (if noerror |
---|
| 2777 | nil |
---|
| 2778 | (error "Variable singular-emacs-home-directory not set")))) |
---|
| 2779 | ;;}}} |
---|
| 2780 | |
---|
| 2781 | ;;{{{ Filename, Command, and Help Completion |
---|
| 2782 | (defun singular-completion-init () |
---|
[c100d9] | 2783 | "Initialize completion for Singular interactive mode. |
---|
| 2784 | Initializes completion of file names, commands and help topics. |
---|
[e5e79a] | 2785 | |
---|
[c100d9] | 2786 | This function is called by `singular-exec'." |
---|
| 2787 | (singular-debug 'interactive (message "Initializing completion")) |
---|
| 2788 | (set (make-local-variable 'singular-commands-alist) nil) |
---|
| 2789 | (set (make-local-variable 'singular-help-topics-alist) nil)) |
---|
[e5e79a] | 2790 | |
---|
| 2791 | (defun singular-completion-do (pattern beg end completion-alist) |
---|
| 2792 | "Try completion on string PATTERN using alist COMPLETION-ALIST. |
---|
[e74240] | 2793 | Inserts completed version of PATTERN as new text between BEG and END. |
---|
[e5e79a] | 2794 | Assumes the COMPLETION-ALIST is not nil." |
---|
| 2795 | (let ((completion (try-completion pattern completion-alist))) |
---|
| 2796 | (cond ((eq completion t) |
---|
| 2797 | (message "[Sole completion]")) ;; nothing to complete |
---|
| 2798 | ((null completion) ;; no completion found |
---|
| 2799 | (message "Can't find completion for \"%s\"" pattern) |
---|
| 2800 | (ding)) |
---|
| 2801 | ((not (string= pattern completion)) |
---|
| 2802 | (delete-region beg end) |
---|
| 2803 | (insert completion)) |
---|
| 2804 | (t |
---|
| 2805 | (message "Making completion list...") |
---|
| 2806 | (let ((list (all-completions pattern |
---|
| 2807 | completion-alist))) |
---|
| 2808 | (with-output-to-temp-buffer "*Completions*" |
---|
| 2809 | (display-completion-list list))) |
---|
| 2810 | (message "Making completion list...%s" "done"))))) |
---|
| 2811 | |
---|
| 2812 | (defun singular-dynamic-complete () |
---|
| 2813 | "Dynamic complete word before point. |
---|
[e74240] | 2814 | Performs file name completion if point is inside a string. |
---|
| 2815 | Performs completion of Singular help topics if point is at the end of a |
---|
[e5e79a] | 2816 | help command (\"help\" or \"?\"). |
---|
[e74240] | 2817 | Otherwise performs completion of Singular commands." |
---|
[e5e79a] | 2818 | (interactive) |
---|
| 2819 | ;; Check if we are inside a string. The search is done back to the |
---|
| 2820 | ;; process-mark which should be the beginning of the current input. |
---|
| 2821 | ;; No check at this point whether there is a process! |
---|
| 2822 | (if (save-excursion |
---|
| 2823 | (nth 3 (parse-partial-sexp (singular-process-mark) (point)))) |
---|
| 2824 | ;; then: inside string, thus expand filename |
---|
| 2825 | (comint-dynamic-complete-as-filename) |
---|
| 2826 | ;; else: expand command or help |
---|
| 2827 | (let ((end (point)) |
---|
| 2828 | beg) |
---|
| 2829 | (if (save-excursion |
---|
| 2830 | (beginning-of-line) |
---|
[9ad353] | 2831 | (singular-prompt-skip-forward) |
---|
| 2832 | (looking-at "[ \t]*\\([\\?]\\|help \\)[ \t]*\\(.*\\)")) |
---|
[e5e79a] | 2833 | ;; then: help completion |
---|
[c100d9] | 2834 | (if singular-help-topics-alist |
---|
[e5e79a] | 2835 | (singular-completion-do (match-string 2) (match-beginning 2) |
---|
[c100d9] | 2836 | end singular-help-topics-alist) |
---|
[e5e79a] | 2837 | (message "Completion of Singular help topics disabled.") |
---|
| 2838 | (ding)) |
---|
| 2839 | ;; else: command completion |
---|
| 2840 | (save-excursion |
---|
| 2841 | (skip-chars-backward "a-zA-Z0-9") |
---|
| 2842 | (setq beg (point))) |
---|
[c100d9] | 2843 | (if singular-commands-alist |
---|
[e5e79a] | 2844 | (singular-completion-do (buffer-substring beg end) beg |
---|
[c100d9] | 2845 | end singular-commands-alist) |
---|
[e5e79a] | 2846 | (message "Completion of Singular commands disabled.") |
---|
| 2847 | (ding)))))) |
---|
| 2848 | ;;}}} |
---|
| 2849 | |
---|
[f50151] | 2850 | ;;{{{ Debugging filters |
---|
[81da6ba] | 2851 | (defun singular-debug-pre-input-filter (string) |
---|
| 2852 | "Display STRING and some markers in mini-buffer." |
---|
| 2853 | (singular-debug 'interactive-filter |
---|
[f50151] | 2854 | (message "Pre-input filter: %s (li %S ci %S lo %S co %S)" |
---|
| 2855 | (singular-debug-format string) |
---|
[81da6ba] | 2856 | (marker-position singular-last-input-section-start) |
---|
| 2857 | (marker-position singular-current-input-section-start) |
---|
| 2858 | (marker-position singular-last-output-section-start) |
---|
| 2859 | (marker-position singular-current-output-section-start))) |
---|
| 2860 | nil) |
---|
| 2861 | |
---|
| 2862 | (defun singular-debug-post-input-filter (beg end) |
---|
| 2863 | "Display BEG, END, and some markers in mini-buffer." |
---|
| 2864 | (singular-debug 'interactive-filter |
---|
[f50151] | 2865 | (message "Post-input filter: (beg %S end %S) (li %S ci %S lo %S co %S)" |
---|
| 2866 | beg end |
---|
[81da6ba] | 2867 | (marker-position singular-last-input-section-start) |
---|
| 2868 | (marker-position singular-current-input-section-start) |
---|
| 2869 | (marker-position singular-last-output-section-start) |
---|
| 2870 | (marker-position singular-current-output-section-start)))) |
---|
| 2871 | |
---|
| 2872 | (defun singular-debug-pre-output-filter (string) |
---|
| 2873 | "Display STRING and some markers in mini-buffer." |
---|
| 2874 | (singular-debug 'interactive-filter |
---|
[f50151] | 2875 | (message "Pre-output filter: %s (li %S ci %S lo %S co %S)" |
---|
| 2876 | (singular-debug-format string) |
---|
[81da6ba] | 2877 | (marker-position singular-last-input-section-start) |
---|
| 2878 | (marker-position singular-current-input-section-start) |
---|
| 2879 | (marker-position singular-last-output-section-start) |
---|
| 2880 | (marker-position singular-current-output-section-start))) |
---|
| 2881 | nil) |
---|
[845cb34] | 2882 | |
---|
[81da6ba] | 2883 | (defun singular-debug-post-output-filter (beg end simple-sec-start) |
---|
| 2884 | "Display BEG, END, SIMPLE-SEC-START, and some markers in mini-buffer." |
---|
| 2885 | (singular-debug 'interactive-filter |
---|
[f50151] | 2886 | (message "Post-output filter: (beg %S end %S sss %S) (li %S ci %S lo %S co %S)" |
---|
| 2887 | beg end simple-sec-start |
---|
[81da6ba] | 2888 | (marker-position singular-last-input-section-start) |
---|
| 2889 | (marker-position singular-current-input-section-start) |
---|
| 2890 | (marker-position singular-last-output-section-start) |
---|
| 2891 | (marker-position singular-current-output-section-start)))) |
---|
[2135c8a] | 2892 | |
---|
[f50151] | 2893 | (defun singular-debug-filter-init () |
---|
| 2894 | "Add debug filters to the necessary hooks. |
---|
| 2895 | |
---|
| 2896 | This function is called at mode initialization time." |
---|
| 2897 | (add-hook 'singular-pre-input-filter-functions |
---|
| 2898 | 'singular-debug-pre-input-filter nil t) |
---|
| 2899 | (add-hook 'singular-post-input-filter-functions |
---|
| 2900 | 'singular-debug-post-input-filter nil t) |
---|
| 2901 | (add-hook 'singular-pre-output-filter-functions |
---|
| 2902 | 'singular-debug-pre-output-filter nil t) |
---|
| 2903 | (add-hook 'singular-post-output-filter-functions |
---|
| 2904 | 'singular-debug-post-output-filter nil t)) |
---|
[845cb34] | 2905 | ;;}}} |
---|
| 2906 | |
---|
[570cea] | 2907 | ;;{{{ Demo mode |
---|
[71c107] | 2908 | |
---|
| 2909 | ;; Note: |
---|
| 2910 | ;; |
---|
| 2911 | ;; For documentation on Singular demo mode one should refer to the doc |
---|
| 2912 | ;; string of `singular-demo-load'. |
---|
| 2913 | ;; Singular demo mode should have been implemented as a minor mode but it |
---|
| 2914 | ;; did not seem worth it. |
---|
| 2915 | |
---|
[f50151] | 2916 | (defcustom singular-demo-chunk-regexp "\\(\n\\s *\n\\)" |
---|
[71c107] | 2917 | "*Regular expressions to recognize chunks of a demo file. |
---|
| 2918 | If there is a subexpression specified its contents is removed after the |
---|
| 2919 | chunk has been displayed. |
---|
[f50151] | 2920 | The default value is \"\\\\(\\n\\\\s *\\n\\\\)\" which means that chunks are |
---|
[71c107] | 2921 | separated by one blank line which is removed after the chunks have been |
---|
| 2922 | displayed." |
---|
[f50151] | 2923 | :type 'regexp |
---|
[71c107] | 2924 | :initialize 'custom-initialize-default |
---|
[f50151] | 2925 | :group 'singular-demo-mode) |
---|
| 2926 | |
---|
[71c107] | 2927 | (defcustom singular-demo-print-messages t |
---|
| 2928 | "*If non-nil, print message on how to continue demo mode." |
---|
[f50151] | 2929 | :type 'boolean |
---|
[71c107] | 2930 | :initialize 'custom-initialize-default |
---|
[f50151] | 2931 | :group 'singular-demo-mode) |
---|
| 2932 | |
---|
[71c107] | 2933 | (defcustom singular-demo-exit-on-load t |
---|
| 2934 | "*If non-nil, an active demo is automatically discarded when a new one is loaded. |
---|
[f50151] | 2935 | Otherwise, the load is aborted with an error." |
---|
| 2936 | :type 'boolean |
---|
[71c107] | 2937 | :initialize 'custom-initialize-default |
---|
[f50151] | 2938 | :group 'singular-demo-mode) |
---|
| 2939 | |
---|
| 2940 | (defcustom singular-demo-load-directory nil |
---|
[71c107] | 2941 | "*Directory where demo files usually reside. |
---|
[f50151] | 2942 | If non-nil, this directory is offered as a starting point to search for |
---|
[71c107] | 2943 | demo files when `singular-demo-load' is called interactively for the first |
---|
| 2944 | time. (In further calls, `singular-demo-load' offers the directory where |
---|
| 2945 | the last demo file has been loaded from as starting point). |
---|
| 2946 | |
---|
| 2947 | If this variable equals nil whatever Emacs offers by default is used as |
---|
| 2948 | first-time starting point. In general, this is the directory where |
---|
| 2949 | Singular has been started in." |
---|
[f50151] | 2950 | :type '(choice (const nil) (file)) |
---|
[71c107] | 2951 | :initialize 'custom-initialize-default |
---|
[f50151] | 2952 | :group 'singular-demo-mode) |
---|
[5fb2b98] | 2953 | |
---|
[570cea] | 2954 | (defvar singular-demo-mode nil |
---|
| 2955 | "Non-nil if Singular demo mode is on. |
---|
| 2956 | |
---|
| 2957 | This variable is buffer-local.") |
---|
| 2958 | |
---|
| 2959 | (defvar singular-demo-old-mode-name nil |
---|
| 2960 | "Used to store previous `mode-name' before switching to demo mode. |
---|
| 2961 | |
---|
| 2962 | This variable is buffer-local.") |
---|
| 2963 | |
---|
[213e96] | 2964 | (defvar singular-demo-end nil |
---|
| 2965 | "Marker pointing to end of demo file. |
---|
| 2966 | |
---|
| 2967 | This variable is buffer-local.") |
---|
| 2968 | |
---|
[71c107] | 2969 | (defvar singular-demo-last-directory nil |
---|
| 2970 | "If non-nil, directory from which the last demo file has been loaded. |
---|
| 2971 | |
---|
| 2972 | This variable is buffer-local.") |
---|
| 2973 | |
---|
[f50151] | 2974 | (defun singular-demo-load (demo-file) |
---|
| 2975 | "Load demo file DEMO-FILE and enter Singular demo mode. |
---|
[71c107] | 2976 | |
---|
| 2977 | The Singular demo mode allows to step conveniently through a prepared demo |
---|
| 2978 | file. The contents of the demo file is made visible and executed in |
---|
| 2979 | portions called chunks. How the chunks have to be marked in the demo file |
---|
| 2980 | is described below. |
---|
| 2981 | |
---|
| 2982 | After loading the demo file with this function, \\[singular-send-or-copy-input] displays the first |
---|
| 2983 | chunk of the demo file at the Singular prompt. This chunk may be modified |
---|
| 2984 | \(or even deleted) and then sent to Singular entering \\[singular-send-or-copy-input] as any command |
---|
| 2985 | would have been sent to Singular. The next time \\[singular-send-or-copy-input] is entered, the next |
---|
| 2986 | chunk of the demo file is displayed, and so on. |
---|
| 2987 | |
---|
| 2988 | One may interrupt this sequence and enter commands at the Singular input |
---|
| 2989 | prompt as usual. As soon as \\[singular-send-or-copy-input] is entered directly after the input |
---|
| 2990 | prompt, the next chunk of the demo file is displayed. Here is the exact |
---|
| 2991 | algorithm how this magic works: If point is located at the very end of the |
---|
| 2992 | buffer *and* immediately after Singular's last input prompt, the next chunk |
---|
| 2993 | of the demo file is displayed. In particular, if there is any text after |
---|
| 2994 | the last input prompt that text is sent to Singular as usual and no new |
---|
| 2995 | chunks are displayed. |
---|
| 2996 | |
---|
| 2997 | After displaying the last chunk of DEMO-FILE, Singular demo mode |
---|
| 2998 | automatically terminates and normal operation is resumed. To prematurely |
---|
| 2999 | exit Singular demo mode \\[singular-demo-exit] may be used. |
---|
| 3000 | |
---|
| 3001 | DEMO-FILE should consist of regular Singular commands. Portions of text |
---|
| 3002 | separated by a blank line are taken to be the chunks of the demo file. |
---|
| 3003 | |
---|
| 3004 | There is a number of variables to configure Singular demo mode. Refer to |
---|
| 3005 | the `singular-demo-mode' customization group for more information. |
---|
| 3006 | |
---|
| 3007 | Important note: The unprocessed contents of DEMO-FILE is hidden using |
---|
| 3008 | buffer narrowing. Emacs gets terribly confused when during demo mode the |
---|
| 3009 | buffer is either narrowed to some other region or if the buffer is widened. |
---|
| 3010 | The safest thing to do if that happens by accident is to explicitly exit |
---|
| 3011 | the demo by means of \\[singular-demo-exit] and to try to resume somehow |
---|
| 3012 | normal operation. |
---|
| 3013 | |
---|
| 3014 | `singular-demo-load' runs the functions on `singular-demo-mode-enter-hook' |
---|
| 3015 | just after demo mode has been entered. The functions on |
---|
| 3016 | `singular-demo-mode-exit-hook' are executed after Singular demo mode has |
---|
| 3017 | been exited, either prematurely or due to the end of the demo file. |
---|
| 3018 | However, it its important to note that in the latter case the last chunk of |
---|
| 3019 | the demo file is still waiting to be sent to Singular." |
---|
[f50151] | 3020 | (interactive |
---|
| 3021 | (list |
---|
[71c107] | 3022 | (let ((demo-file-name |
---|
| 3023 | (cond |
---|
| 3024 | ;; Emacs |
---|
| 3025 | ((eq singular-emacs-flavor 'emacs) |
---|
| 3026 | (read-file-name "Load demo file: " |
---|
| 3027 | (or singular-demo-last-directory |
---|
| 3028 | singular-demo-load-directory) |
---|
| 3029 | nil t)) |
---|
| 3030 | ;; XEmacs |
---|
| 3031 | (t |
---|
| 3032 | ;; there are some problems with the window being popped up when this |
---|
| 3033 | ;; function is called from a menu. It does not display the contents |
---|
| 3034 | ;; of `singular-demo-load-directory' but of `default-directory'. |
---|
| 3035 | (let ((default-directory (or singular-demo-last-directory |
---|
| 3036 | singular-demo-load-directory |
---|
| 3037 | default-directory))) |
---|
| 3038 | (read-file-name "Load demo file: " |
---|
| 3039 | (or singular-demo-last-directory |
---|
| 3040 | singular-demo-load-directory) |
---|
| 3041 | nil t)))))) |
---|
| 3042 | |
---|
| 3043 | (setq singular-demo-last-directory (file-name-directory demo-file-name)) |
---|
| 3044 | demo-file-name))) |
---|
[5322a3] | 3045 | |
---|
[f50151] | 3046 | ;; check for running demo |
---|
| 3047 | (if singular-demo-mode |
---|
| 3048 | (if singular-demo-exit-on-load |
---|
| 3049 | ;; silently exit running demo |
---|
[6e3bdda] | 3050 | (singular-demo-exit t) |
---|
[f50151] | 3051 | (error "There already is a demo running, exit with `singular-demo-exit' first"))) |
---|
| 3052 | |
---|
| 3053 | ;; load new demo |
---|
| 3054 | (let ((old-point-min (point-min))) |
---|
| 3055 | (unwind-protect |
---|
| 3056 | (progn |
---|
| 3057 | (goto-char (point-max)) |
---|
| 3058 | (widen) |
---|
| 3059 | (cond |
---|
| 3060 | ;; XEmacs |
---|
| 3061 | ((eq singular-emacs-flavor 'xemacs) |
---|
| 3062 | ;; load file and remember its end |
---|
| 3063 | (set-marker singular-demo-end |
---|
| 3064 | (+ (point) (nth 1 (insert-file-contents-literally demo-file))))) |
---|
| 3065 | ;; Emacs |
---|
| 3066 | (t |
---|
| 3067 | ;; Emacs does something like an `insert-before-markers' so |
---|
| 3068 | ;; save all essential markers |
---|
| 3069 | (let ((pmark-pos (marker-position (singular-process-mark))) |
---|
| 3070 | (sliss-pos (marker-position singular-last-input-section-start)) |
---|
| 3071 | (sciss-pos (marker-position singular-current-input-section-start)) |
---|
| 3072 | (sloss-pos (marker-position singular-last-output-section-start)) |
---|
| 3073 | (scoss-pos (marker-position singular-current-output-section-start))) |
---|
| 3074 | |
---|
| 3075 | (unwind-protect |
---|
| 3076 | ;; load file and remember its end |
---|
| 3077 | (set-marker singular-demo-end |
---|
| 3078 | (+ (point) (nth 1 (insert-file-contents-literally demo-file)))) |
---|
| 3079 | |
---|
| 3080 | ;; restore markers. |
---|
| 3081 | ;; This is unwind-protected. |
---|
| 3082 | (set-marker (singular-process-mark) pmark-pos) |
---|
| 3083 | (set-marker singular-last-input-section-start sliss-pos) |
---|
| 3084 | (set-marker singular-current-input-section-start sciss-pos) |
---|
| 3085 | (set-marker singular-last-output-section-start sloss-pos) |
---|
| 3086 | (set-marker singular-current-output-section-start scoss-pos)))))) |
---|
| 3087 | |
---|
| 3088 | ;; completely hide demo file. |
---|
| 3089 | ;; This is unwind-protected. |
---|
| 3090 | (narrow-to-region old-point-min (point)))) |
---|
| 3091 | |
---|
| 3092 | ;; switch demo mode on |
---|
| 3093 | (setq singular-demo-old-mode-name mode-name |
---|
| 3094 | mode-name "Singular Demo" |
---|
| 3095 | singular-demo-mode t) |
---|
| 3096 | (run-hooks 'singular-demo-mode-enter-hook) |
---|
| 3097 | (if singular-demo-print-messages (message "Hit RET to start demo")) |
---|
| 3098 | (force-mode-line-update)) |
---|
| 3099 | |
---|
| 3100 | (defun singular-demo-exit-internal () |
---|
| 3101 | "Exit Singular demo mode. |
---|
| 3102 | Recovers the old mode name, sets `singular-demo-mode' to nil, runs |
---|
| 3103 | the hooks on `singular-demo-mode-exit-hook'." |
---|
| 3104 | (setq mode-name singular-demo-old-mode-name |
---|
| 3105 | singular-demo-mode nil) |
---|
| 3106 | (run-hooks 'singular-demo-mode-exit-hook) |
---|
| 3107 | (force-mode-line-update)) |
---|
| 3108 | |
---|
[6e3bdda] | 3109 | (defun singular-demo-exit (&optional no-message) |
---|
[f50151] | 3110 | "Prematurely exit Singular demo mode. |
---|
| 3111 | Cleans up everything that is left from the demo. |
---|
| 3112 | Runs the hooks on `singular-demo-mode-exit-hook'. |
---|
[71c107] | 3113 | Does nothing when Singular demo mode is not active." |
---|
[f50151] | 3114 | (interactive) |
---|
| 3115 | (when singular-demo-mode |
---|
| 3116 | ;; clean up hidden rest of demo file |
---|
[5322a3] | 3117 | (let ((old-point-min (point-min)) |
---|
| 3118 | (old-point-max (point-max))) |
---|
| 3119 | (unwind-protect |
---|
| 3120 | (progn |
---|
| 3121 | (widen) |
---|
| 3122 | (delete-region old-point-max singular-demo-end)) |
---|
[f50151] | 3123 | ;; this is unwind-protected |
---|
[5322a3] | 3124 | (narrow-to-region old-point-min old-point-max))) |
---|
[6e3bdda] | 3125 | (singular-demo-exit-internal) |
---|
| 3126 | (or no-message |
---|
| 3127 | (if singular-demo-print-messages (message "Demo exited"))))) |
---|
[5322a3] | 3128 | |
---|
[570cea] | 3129 | (defun singular-demo-show-next-chunk () |
---|
| 3130 | "Show next chunk of demo file at input prompt. |
---|
[71c107] | 3131 | Assumes that Singular demo mode is active. |
---|
[570cea] | 3132 | Moves point to end of buffer and widenes the buffer such that the next |
---|
| 3133 | chunk of the demo file becomes visible. |
---|
| 3134 | Finds and removes chunk separators as specified by |
---|
[213e96] | 3135 | `singular-demo-chunk-regexp'. |
---|
[f50151] | 3136 | Leaves demo mode after showing last chunk. In that case runs hooks on |
---|
| 3137 | `singular-demo-mode-exit-hook'." |
---|
[bd4207] | 3138 | (let ((old-point-min (point-min))) |
---|
[570cea] | 3139 | (unwind-protect |
---|
| 3140 | (progn |
---|
| 3141 | (goto-char (point-max)) |
---|
| 3142 | (widen) |
---|
[5322a3] | 3143 | (if (re-search-forward singular-demo-chunk-regexp singular-demo-end 'limit) |
---|
[f50151] | 3144 | (if (match-beginning 1) |
---|
| 3145 | (delete-region (match-beginning 1) (match-end 1))) |
---|
| 3146 | ;; remove trailing white-space. We may not use |
---|
| 3147 | ;; `(skip-syntax-backward "-")' since newline is has no white |
---|
| 3148 | ;; space syntax. The solution down below should suffice in |
---|
| 3149 | ;; almost all cases ... |
---|
[71c107] | 3150 | (skip-chars-backward " \t\n\r\f") |
---|
[5322a3] | 3151 | (delete-region (point) singular-demo-end) |
---|
[f50151] | 3152 | (singular-demo-exit-internal))) |
---|
[570cea] | 3153 | |
---|
| 3154 | ;; this is unwind-protected |
---|
[213e96] | 3155 | (narrow-to-region old-point-min (point))))) |
---|
[570cea] | 3156 | |
---|
[f50151] | 3157 | (defun singular-demo-mode-init () |
---|
| 3158 | "Initialize variables belonging to Singular demo mode. |
---|
| 3159 | Creates some buffer-local variables and the buffer-local marker |
---|
| 3160 | `singular-demo-end'. |
---|
| 3161 | |
---|
| 3162 | This function is called at mode initialization time." |
---|
| 3163 | (make-local-variable 'singular-demo-mode) |
---|
| 3164 | (make-local-variable 'singular-demo-mode-old-name) |
---|
| 3165 | (make-local-variable 'singular-demo-mode-end) |
---|
| 3166 | (if (not (and (boundp 'singular-demo-end) |
---|
| 3167 | singular-demo-end)) |
---|
[71c107] | 3168 | (setq singular-demo-end (make-marker))) |
---|
| 3169 | (make-local-variable 'singular-demo-last-directory)) |
---|
[570cea] | 3170 | ;;}}} |
---|
| 3171 | |
---|
[81da6ba] | 3172 | ;;{{{ Some lengthy notes on input and output |
---|
[6d68ac] | 3173 | |
---|
[81da6ba] | 3174 | ;; NOT READY[so sorry]! |
---|
[6d68ac] | 3175 | |
---|
| 3176 | ;;}}} |
---|
| 3177 | |
---|
[f50151] | 3178 | ;;{{{ Last input and output section |
---|
| 3179 | (defun singular-last-input-section (&optional no-error) |
---|
| 3180 | "Return last input section. |
---|
| 3181 | Returns nil if optional argument NO-ERROR is non-nil and there is no |
---|
| 3182 | last input section defined, throws an error otherwise." |
---|
| 3183 | (let ((last-input-start (marker-position singular-last-input-section-start)) |
---|
| 3184 | (last-input-end (marker-position singular-current-output-section-start))) |
---|
| 3185 | (cond ((and last-input-start last-input-end) |
---|
| 3186 | (singular-section-create (singular-simple-sec-at last-input-start) 'input |
---|
| 3187 | last-input-start last-input-end)) |
---|
| 3188 | (no-error nil) |
---|
| 3189 | (t (error "No last input section defined"))))) |
---|
| 3190 | |
---|
| 3191 | (defun singular-current-output-section (&optional no-error) |
---|
| 3192 | "Return current output section. |
---|
| 3193 | Returns nil if optional argument NO-ERROR is non-nil and there is no |
---|
| 3194 | current output section defined, throws an error otherwise." |
---|
| 3195 | (let ((current-output-start (marker-position singular-current-output-section-start)) |
---|
| 3196 | (current-output-end (save-excursion |
---|
| 3197 | (save-restriction |
---|
| 3198 | (widen) |
---|
| 3199 | (goto-char (singular-process-mark)) |
---|
[71c107] | 3200 | (singular-prompt-skip-backward) |
---|
[f50151] | 3201 | (and (bolp) (point)))))) |
---|
| 3202 | (cond ((and current-output-start current-output-end) |
---|
| 3203 | (singular-section-create (singular-simple-sec-at current-output-start) 'output |
---|
| 3204 | current-output-start current-output-end)) |
---|
| 3205 | (no-error nil) |
---|
| 3206 | (t (error "No current output section defined"))))) |
---|
| 3207 | |
---|
| 3208 | (defun singular-last-output-section (&optional no-error) |
---|
| 3209 | "Return last output section. |
---|
| 3210 | Returns nil if optional argument NO-ERROR is non-nil and there is no |
---|
| 3211 | last output section defined, throws an error otherwise." |
---|
| 3212 | (let ((last-output-start (marker-position singular-last-output-section-start)) |
---|
| 3213 | (last-output-end (marker-position singular-last-input-section-start))) |
---|
| 3214 | (cond ((and last-output-start last-output-end) |
---|
| 3215 | (singular-section-create (singular-simple-sec-at last-output-start) 'output |
---|
| 3216 | last-output-start last-output-end)) |
---|
| 3217 | (no-error nil) |
---|
| 3218 | (t (error "No last output section defined"))))) |
---|
| 3219 | |
---|
| 3220 | (defun singular-latest-output-section (&optional no-error) |
---|
| 3221 | "Return latest output section. |
---|
| 3222 | This is the current output section if it is defined, otherwise the |
---|
| 3223 | last output section. |
---|
| 3224 | Returns nil if optional argument NO-ERROR is non-nil and there is no |
---|
| 3225 | latest output section defined, throws an error otherwise." |
---|
| 3226 | (or (singular-current-output-section t) |
---|
| 3227 | (singular-last-output-section t) |
---|
| 3228 | (if no-error |
---|
| 3229 | nil |
---|
| 3230 | (error "No latest output section defined")))) |
---|
| 3231 | ;;}}} |
---|
| 3232 | |
---|
[81da6ba] | 3233 | ;;{{{ Sending input |
---|
| 3234 | (defvar singular-pre-input-filter-functions nil |
---|
| 3235 | "Functions to call before input is sent to process. |
---|
| 3236 | These functions get one argument, a string containing the text which |
---|
| 3237 | is to be sent to process. The functions should return either nil |
---|
| 3238 | or a string. In the latter case the returned string replaces the |
---|
| 3239 | string to be sent to process. |
---|
| 3240 | |
---|
| 3241 | This is a buffer-local variable, not a buffer-local hook! |
---|
| 3242 | |
---|
| 3243 | `singular-run-hook-with-arg-and-value' is used to run the functions in |
---|
| 3244 | the list.") |
---|
| 3245 | |
---|
| 3246 | (defvar singular-post-input-filter-functions nil |
---|
| 3247 | "Functions to call after input is sent to process. |
---|
| 3248 | These functions get two arguments BEG and END. |
---|
| 3249 | If `singular-input-filter' has been called with a string as argument |
---|
| 3250 | BEG and END gives the position of this string after insertion into the |
---|
| 3251 | buffer. |
---|
| 3252 | If `singular-input-filter' has been called with a position as argument |
---|
| 3253 | BEG and END equal process mark and that position, resp. |
---|
| 3254 | The functions may assume that no narrowing is in effect and may change |
---|
| 3255 | point at will. |
---|
| 3256 | |
---|
| 3257 | This hook is buffer-local.") |
---|
| 3258 | |
---|
| 3259 | (defvar singular-current-input-section-start nil |
---|
| 3260 | "Marker to the start of the current input section. |
---|
| 3261 | This marker points nowhere on startup or if there is no current input |
---|
| 3262 | section. |
---|
| 3263 | |
---|
| 3264 | This variable is buffer-local.") |
---|
| 3265 | |
---|
| 3266 | (defvar singular-last-input-section-start nil |
---|
| 3267 | "Marker to the start of the last input section. |
---|
| 3268 | This marker points nowhere on startup. |
---|
| 3269 | |
---|
| 3270 | This variable is buffer-local.") |
---|
| 3271 | |
---|
| 3272 | (defun singular-input-filter-init (pos) |
---|
| 3273 | "Initialize all variables concerning input. |
---|
| 3274 | POS is the position of the process mark." |
---|
| 3275 | ;; localize variables not yet localized in `singular-interactive-mode' |
---|
| 3276 | (make-local-variable 'singular-current-input-section-start) |
---|
| 3277 | (make-local-variable 'singular-last-input-section-start) |
---|
| 3278 | |
---|
| 3279 | ;; initialize markers |
---|
| 3280 | (if (not (markerp singular-current-input-section-start)) |
---|
| 3281 | (setq singular-current-input-section-start (make-marker))) |
---|
| 3282 | (if (not (markerp singular-last-input-section-start)) |
---|
| 3283 | (setq singular-last-input-section-start (make-marker)))) |
---|
| 3284 | |
---|
| 3285 | (defun singular-send-string (process string) |
---|
| 3286 | "Send newline terminated STRING to to process PROCESS. |
---|
| 3287 | Runs the hooks on `singular-pre-input-filter-functions' in the buffer |
---|
| 3288 | associated to PROCESS. The functions get the non-terminated string." |
---|
| 3289 | (let ((process-buffer (process-buffer process))) |
---|
| 3290 | |
---|
| 3291 | ;; check whether buffer is still alive |
---|
| 3292 | (if (and process-buffer (buffer-name process-buffer)) |
---|
| 3293 | (save-excursion |
---|
| 3294 | (set-buffer process-buffer) |
---|
| 3295 | (send-string |
---|
| 3296 | process |
---|
| 3297 | (concat (singular-run-hook-with-arg-and-value |
---|
| 3298 | singular-pre-input-filter-functions string) |
---|
| 3299 | "\n")))))) |
---|
| 3300 | |
---|
| 3301 | (defun singular-input-filter (process string-or-pos) |
---|
| 3302 | "Insert/update input from user in buffer associated to PROCESS. |
---|
| 3303 | Inserts STRING-OR-POS followed by a newline at process mark if it is a |
---|
| 3304 | string. |
---|
| 3305 | Assumes that the input is already inserted and that it is placed |
---|
| 3306 | between process mark and STRING-OR-POS if the latter is a position. |
---|
| 3307 | Inserts a newline after STRING-OR-POS. |
---|
| 3308 | |
---|
| 3309 | Takes care off: |
---|
| 3310 | - current buffer as well as point and restriction in buffer associated |
---|
| 3311 | with process, even against non-local exits. |
---|
| 3312 | Updates: |
---|
| 3313 | - process mark; |
---|
| 3314 | - current and last sections; |
---|
| 3315 | - simple sections; |
---|
| 3316 | - mode line. |
---|
| 3317 | |
---|
| 3318 | Runs the hooks on `singular-pre-input-filter-functions' and |
---|
| 3319 | `singular-post-input-filter-functions'. |
---|
| 3320 | |
---|
| 3321 | For a more detailed descriptions of the input filter, the markers it |
---|
| 3322 | sets, and input filter functions refer to the section \"Some lengthy |
---|
| 3323 | notes on input and output\" in singular.el." |
---|
| 3324 | (let ((process-buffer (process-buffer process))) |
---|
| 3325 | |
---|
| 3326 | ;; check whether buffer is still alive |
---|
| 3327 | (if (and process-buffer (buffer-name process-buffer)) |
---|
| 3328 | (let ((old-buffer (current-buffer)) |
---|
| 3329 | (old-pmark (marker-position (process-mark process))) |
---|
| 3330 | old-point old-point-min old-point-max) |
---|
| 3331 | (unwind-protect |
---|
| 3332 | (let (simple-sec-start) |
---|
| 3333 | (set-buffer process-buffer) |
---|
| 3334 | ;; the following lines are not protected since the |
---|
| 3335 | ;; unwind-forms refer the variables being set here |
---|
| 3336 | (setq old-point (point-marker) |
---|
| 3337 | old-point-min (point-min-marker) |
---|
| 3338 | old-point-max (point-max-marker) |
---|
| 3339 | |
---|
| 3340 | ;; get end of last simple section (equals start of |
---|
| 3341 | ;; current) |
---|
[f50151] | 3342 | simple-sec-start (singular-simple-sec-last-end-position)) |
---|
[81da6ba] | 3343 | |
---|
| 3344 | ;; prepare for insertion |
---|
| 3345 | (widen) |
---|
| 3346 | (set-marker-insertion-type old-point t) |
---|
| 3347 | (set-marker-insertion-type old-point-max t) |
---|
| 3348 | |
---|
| 3349 | ;; insert string at process mark and advance process |
---|
| 3350 | ;; mark after insertion. If it not a string simply |
---|
| 3351 | ;; jump to desired position and insrt a newline. |
---|
| 3352 | (if (stringp string-or-pos) |
---|
| 3353 | (progn |
---|
| 3354 | (goto-char old-pmark) |
---|
| 3355 | (insert string-or-pos)) |
---|
| 3356 | (goto-char string-or-pos)) |
---|
| 3357 | (insert ?\n) |
---|
| 3358 | (set-marker (process-mark process) (point)) |
---|
| 3359 | |
---|
| 3360 | ;; create new simple section and update section markers |
---|
| 3361 | (cond |
---|
| 3362 | ((eq (singular-simple-sec-create 'input (point)) 'empty) |
---|
| 3363 | nil) |
---|
| 3364 | ;; a new simple section has been created ... |
---|
| 3365 | ((null (marker-position singular-current-input-section-start)) |
---|
| 3366 | ;; ... and even a new input section has been created! |
---|
| 3367 | (set-marker singular-current-input-section-start |
---|
| 3368 | simple-sec-start) |
---|
| 3369 | (set-marker singular-last-output-section-start |
---|
| 3370 | singular-current-output-section-start) |
---|
| 3371 | (set-marker singular-current-output-section-start nil))) |
---|
| 3372 | |
---|
| 3373 | ;; run post-output hooks and force mode-line update |
---|
| 3374 | (run-hook-with-args 'singular-post-input-filter-functions |
---|
| 3375 | old-pmark (point))) |
---|
| 3376 | |
---|
| 3377 | ;; restore buffer, restrictions and point |
---|
| 3378 | (narrow-to-region old-point-min old-point-max) |
---|
| 3379 | (set-marker old-point-min nil) |
---|
| 3380 | (set-marker old-point-max nil) |
---|
| 3381 | (goto-char old-point) |
---|
| 3382 | (set-marker old-point nil) |
---|
| 3383 | (set-buffer old-buffer)))))) |
---|
[71c107] | 3384 | ;;}}} |
---|
| 3385 | |
---|
| 3386 | ;;{{{ Sending input interactive |
---|
| 3387 | (defcustom singular-move-on-send 'eob |
---|
[0b70a3] | 3388 | "*Where to move point before sending input to Singular. |
---|
[71c107] | 3389 | Should be one of: |
---|
| 3390 | `eob' which means to move point to end of buffer, |
---|
| 3391 | `eol' which means to move point to end of line, or |
---|
| 3392 | nil which means to not move point at all." |
---|
| 3393 | :type '(choice (const :tag "End of buffer" eob) |
---|
| 3394 | (const :tag "End of line" eol) |
---|
| 3395 | (const :tag "Do not move" nil)) |
---|
| 3396 | :initialize 'custom-initialize-default |
---|
| 3397 | :group 'singular-interactive-miscellaneous) |
---|
| 3398 | |
---|
[213e96] | 3399 | (defun singular-get-old-input (get-section) |
---|
[71c107] | 3400 | "Get and return old input. |
---|
| 3401 | Retrivies on a per-section base if GET-SECTION is non-nil, otherwise on a |
---|
| 3402 | per-line base." |
---|
[213e96] | 3403 | (if get-section |
---|
| 3404 | ;; get input from input section |
---|
| 3405 | (let ((section (singular-section-at (point)))) |
---|
| 3406 | (if (eq (singular-section-type section) 'input) |
---|
[71c107] | 3407 | (singular-white-space-strip (singular-section-to-string section) t) |
---|
[213e96] | 3408 | (error "Not on an input section"))) |
---|
| 3409 | ;; get input from line |
---|
| 3410 | (save-excursion |
---|
| 3411 | (beginning-of-line) |
---|
[f50151] | 3412 | (singular-prompt-skip-forward) |
---|
[213e96] | 3413 | (let ((old-point (point))) |
---|
| 3414 | (end-of-line) |
---|
| 3415 | (buffer-substring old-point (point)))))) |
---|
| 3416 | |
---|
[71c107] | 3417 | (defun singular-send-or-copy-input (get-section) |
---|
| 3418 | "Send input to Singular. |
---|
| 3419 | |
---|
| 3420 | The behavior of this function very much depends on the current position of |
---|
| 3421 | point relative to the process mark, that is, the position, where Singular |
---|
| 3422 | expects next input. |
---|
| 3423 | |
---|
| 3424 | If point is located before process mark, old input is copied to the process |
---|
| 3425 | mark. With prefix argument, the whole input section point currently is in |
---|
| 3426 | is copied, without prefix argument only the current line. One should note |
---|
| 3427 | that the input is *not* sent to Singular, it is only copied to the process |
---|
| 3428 | mark. Another time entering \\[singular-send-or-copy-input] sends it to Singular. |
---|
| 3429 | |
---|
| 3430 | If point is located after process mark, point is moved as determined by the |
---|
| 3431 | `singular-move-on-send' variable: either it is moved to the end of the |
---|
| 3432 | current line, or to the end of the buffer, or it is not moved at all. The |
---|
| 3433 | default is to move point to the end of the buffer which most closely |
---|
| 3434 | resembles regular terminal behaviour. At last, the text of the region |
---|
| 3435 | between process mark and point is sent to Singular. |
---|
| 3436 | |
---|
| 3437 | Any input to Singular is stored in an input history where it may be |
---|
| 3438 | retrieved with \\[comint-previous-input] or \\[comint-next-input], respectively. For more information on the input |
---|
| 3439 | history one should refer to the documentation of |
---|
| 3440 | `singular-interactive-mode'. |
---|
| 3441 | |
---|
| 3442 | If Singular demo mode is active and point is at process mark and if that |
---|
| 3443 | position is at the end of the buffer the next chunk of the demo file is |
---|
| 3444 | displayed. One should refer to the documentation of `singular-demo-load' |
---|
| 3445 | for more information on Singular demo mode. |
---|
| 3446 | |
---|
| 3447 | The Singular process should be running." |
---|
[570cea] | 3448 | (interactive "P") |
---|
[71c107] | 3449 | (let ((process (singular-process)) |
---|
| 3450 | (pmark (singular-process-mark))) |
---|
[570cea] | 3451 | (cond |
---|
| 3452 | (;; check for demo mode and show next chunk if necessary |
---|
| 3453 | (and singular-demo-mode |
---|
[71c107] | 3454 | (= (point) pmark) |
---|
| 3455 | (= pmark (point-max))) |
---|
[5322a3] | 3456 | (singular-demo-show-next-chunk)) |
---|
[570cea] | 3457 | |
---|
| 3458 | (;; get old input |
---|
| 3459 | (< (point) pmark) |
---|
[71c107] | 3460 | (let ((old-input (singular-get-old-input get-section))) |
---|
[81da6ba] | 3461 | (goto-char pmark) |
---|
[213e96] | 3462 | (insert old-input))) |
---|
[570cea] | 3463 | |
---|
[71c107] | 3464 | (;; send input from pmark to point |
---|
[570cea] | 3465 | t |
---|
[71c107] | 3466 | ;; print message if demo mode is active. We print it before we do |
---|
| 3467 | ;; anything else so that the message will not hide any further |
---|
| 3468 | ;; (error) messages. |
---|
[5fb2b98] | 3469 | (and singular-demo-mode |
---|
| 3470 | singular-demo-print-messages |
---|
| 3471 | (message "Hit RET to continue demo")) |
---|
| 3472 | |
---|
[71c107] | 3473 | ;; go to desired position |
---|
| 3474 | (cond ((eq singular-move-on-send 'eol) |
---|
| 3475 | (end-of-line)) |
---|
| 3476 | ((eq singular-move-on-send 'eob) |
---|
| 3477 | (goto-char (point-max)))) |
---|
[570cea] | 3478 | |
---|
[81da6ba] | 3479 | (let* ((input (buffer-substring pmark (point)))) |
---|
[f50151] | 3480 | ;; insert string into history |
---|
| 3481 | (singular-history-insert input) |
---|
| 3482 | ;; send string to process |
---|
[81da6ba] | 3483 | (singular-send-string process input) |
---|
[f50151] | 3484 | ;; "insert" it into buffer |
---|
[81da6ba] | 3485 | (singular-input-filter process (point))))))) |
---|
| 3486 | ;;}}} |
---|
| 3487 | |
---|
| 3488 | ;;{{{ Receiving output |
---|
| 3489 | (defvar singular-pre-output-filter-functions nil |
---|
| 3490 | "Functions to call before output is inserted into the buffer. |
---|
| 3491 | These functions get one argument, a string containing the text sent |
---|
| 3492 | from process. The functions should return either nil or a string. |
---|
| 3493 | In the latter case the returned string replaces the string sent from |
---|
| 3494 | process. |
---|
| 3495 | |
---|
| 3496 | This is a buffer-local variable, not a buffer-local hook! |
---|
| 3497 | |
---|
| 3498 | `singular-run-hook-with-arg-and-value' is used to run the functions in |
---|
| 3499 | this list.") |
---|
[570cea] | 3500 | |
---|
[81da6ba] | 3501 | (defvar singular-post-output-filter-functions nil |
---|
| 3502 | "Functions to call after output is inserted into the buffer. |
---|
| 3503 | These functions get three arguments BEG, END, and SIMPLE-SEC-START. |
---|
| 3504 | The region between BEG and END is what has been inserted into the |
---|
| 3505 | buffer. |
---|
| 3506 | SIMPLE-SEC-START is the start of the simple section which has been |
---|
| 3507 | created on insertion or nil if no simple section has been created. |
---|
| 3508 | The functions may assume that no narrowing is in effect and may change |
---|
| 3509 | point at will. |
---|
| 3510 | |
---|
| 3511 | This hook is buffer-local.") |
---|
| 3512 | |
---|
| 3513 | (defvar singular-current-output-section-start nil |
---|
| 3514 | "Marker to the start of the current output section. |
---|
| 3515 | This marker points nowhere on startup or if there is no current output |
---|
| 3516 | section. |
---|
| 3517 | |
---|
| 3518 | This variable is buffer-local.") |
---|
| 3519 | |
---|
| 3520 | (defvar singular-last-output-section-start nil |
---|
| 3521 | "Marker to the start of the last output section. |
---|
| 3522 | This marker points nowhere on startup. |
---|
| 3523 | |
---|
| 3524 | This variable is buffer-local.") |
---|
| 3525 | |
---|
| 3526 | (defun singular-output-filter-init (pos) |
---|
| 3527 | "Initialize all variables concerning output including process mark. |
---|
| 3528 | Set process mark to POS." |
---|
| 3529 | |
---|
| 3530 | ;; localize variables not yet localized in `singular-interactive-mode' |
---|
| 3531 | (make-local-variable 'singular-current-output-section-start) |
---|
| 3532 | (make-local-variable 'singular-last-output-section-start) |
---|
| 3533 | |
---|
| 3534 | ;; initialize markers |
---|
| 3535 | (if (not (markerp singular-current-output-section-start)) |
---|
| 3536 | (setq singular-current-output-section-start (make-marker))) |
---|
| 3537 | (if (not (markerp singular-last-output-section-start)) |
---|
| 3538 | (setq singular-last-output-section-start (make-marker))) |
---|
| 3539 | (set-marker (singular-process-mark) pos)) |
---|
[845cb34] | 3540 | |
---|
| 3541 | (defun singular-output-filter (process string) |
---|
| 3542 | "Insert STRING containing output from PROCESS into its associated buffer. |
---|
| 3543 | Takes care off: |
---|
[81da6ba] | 3544 | - current buffer as well as point and restriction in buffer associated |
---|
| 3545 | with process, even against non-local exits. |
---|
[845cb34] | 3546 | Updates: |
---|
| 3547 | - process mark; |
---|
[81da6ba] | 3548 | - current and last sections; |
---|
[845cb34] | 3549 | - simple sections; |
---|
| 3550 | - mode line. |
---|
[81da6ba] | 3551 | Runs the hooks on `singular-pre-output-filter-functions' and |
---|
| 3552 | `singular-post-output-filter-functions'. |
---|
[845cb34] | 3553 | |
---|
| 3554 | For a more detailed descriptions of the output filter, the markers it |
---|
| 3555 | sets, and output filter functions refer to the section \"Some lengthy |
---|
[81da6ba] | 3556 | notes on input and output\" in singular.el." |
---|
| 3557 | (let ((process-buffer (process-buffer process))) |
---|
[845cb34] | 3558 | |
---|
| 3559 | ;; check whether buffer is still alive |
---|
| 3560 | (if (and process-buffer (buffer-name process-buffer)) |
---|
[81da6ba] | 3561 | (let ((old-buffer (current-buffer)) |
---|
| 3562 | (old-pmark (marker-position (process-mark process))) |
---|
| 3563 | old-point old-point-min old-point-max) |
---|
| 3564 | (unwind-protect |
---|
| 3565 | (let (simple-sec-start) |
---|
| 3566 | (set-buffer process-buffer) |
---|
| 3567 | ;; the following lines are not protected since the |
---|
| 3568 | ;; unwind-forms refer the variables being set here |
---|
| 3569 | (setq old-point (point-marker) |
---|
| 3570 | old-point-min (point-min-marker) |
---|
| 3571 | old-point-max (point-max-marker) |
---|
| 3572 | |
---|
| 3573 | ;; get end of last simple section (equals start of |
---|
| 3574 | ;; current) |
---|
[f50151] | 3575 | simple-sec-start (singular-simple-sec-last-end-position) |
---|
[81da6ba] | 3576 | |
---|
| 3577 | ;; get string to insert |
---|
| 3578 | string (singular-run-hook-with-arg-and-value |
---|
| 3579 | singular-pre-output-filter-functions |
---|
| 3580 | string)) |
---|
| 3581 | |
---|
| 3582 | ;; prepare for insertion |
---|
[845cb34] | 3583 | (widen) |
---|
[81da6ba] | 3584 | (set-marker-insertion-type old-point t) |
---|
| 3585 | (set-marker-insertion-type old-point-max t) |
---|
[845cb34] | 3586 | |
---|
[81da6ba] | 3587 | ;; insert string at process mark and advance process |
---|
| 3588 | ;; mark after insertion |
---|
| 3589 | (goto-char old-pmark) |
---|
| 3590 | (insert string) |
---|
| 3591 | (set-marker (process-mark process) (point)) |
---|
| 3592 | |
---|
| 3593 | ;; create new simple section and update section markers |
---|
| 3594 | (cond |
---|
| 3595 | ((eq (singular-simple-sec-create 'output (point)) 'empty) |
---|
| 3596 | (setq simple-sec-start nil)) |
---|
| 3597 | ;; a new simple section has been created ... |
---|
| 3598 | ((null (marker-position singular-current-output-section-start)) |
---|
| 3599 | ;; ... and even a new output section has been created! |
---|
| 3600 | (set-marker singular-current-output-section-start |
---|
| 3601 | simple-sec-start) |
---|
| 3602 | (set-marker singular-last-input-section-start |
---|
| 3603 | singular-current-input-section-start) |
---|
| 3604 | (set-marker singular-current-input-section-start nil))) |
---|
| 3605 | |
---|
| 3606 | ;; run post-output hooks and force mode-line update |
---|
| 3607 | (run-hook-with-args 'singular-post-output-filter-functions |
---|
| 3608 | old-pmark (point) simple-sec-start) |
---|
| 3609 | (force-mode-line-update)) |
---|
| 3610 | |
---|
| 3611 | ;; restore buffer, restrictions and point |
---|
| 3612 | (narrow-to-region old-point-min old-point-max) |
---|
| 3613 | (set-marker old-point-min nil) |
---|
| 3614 | (set-marker old-point-max nil) |
---|
| 3615 | (goto-char old-point) |
---|
| 3616 | (set-marker old-point nil) |
---|
| 3617 | (set-buffer old-buffer)))))) |
---|
[1735b4e] | 3618 | ;;}}} |
---|
| 3619 | |
---|
| 3620 | ;;{{{ Singular interactive mode |
---|
[4701a1a] | 3621 | (defun singular-interactive-mode () |
---|
| 3622 | "Major mode for interacting with Singular. |
---|
| 3623 | |
---|
[a0cde2] | 3624 | NOT READY [how to send input]! |
---|
[71c107] | 3625 | NOT READY [in particular: input history!] |
---|
[4701a1a] | 3626 | |
---|
| 3627 | NOT READY [multiple Singulars]! |
---|
| 3628 | |
---|
[af6461] | 3629 | \\{singular-interactive-mode-map} |
---|
[71c107] | 3630 | |
---|
| 3631 | |
---|
| 3632 | For \"backward compatibility\" with the terminal version of Singular there |
---|
| 3633 | is some extra magic built into Singular interactive mode which catches help |
---|
| 3634 | commands issued at the command prompt and executes this function instead. |
---|
| 3635 | However, this magic is really not too magic and easily may be fooled. If |
---|
| 3636 | this magic if fooled Singular prints some error message starting like this: |
---|
| 3637 | |
---|
| 3638 | // ** Your help command could not be executed. ... |
---|
| 3639 | |
---|
| 3640 | However, the most common case should be recognized: If one issues a help |
---|
| 3641 | command to a non-busy Singular, where the help command comes on one line |
---|
| 3642 | and is properly terminated with a semicolon. Like that: |
---|
| 3643 | |
---|
| 3644 | help ring; |
---|
| 3645 | |
---|
| 3646 | Customization: Entry to this mode runs the hooks on |
---|
| 3647 | `singular-interactive-mode-hook'. |
---|
[af6461] | 3648 | |
---|
[4701a1a] | 3649 | NOT READY [much more to come. See shell.el.]!" |
---|
| 3650 | (interactive) |
---|
| 3651 | |
---|
[f50151] | 3652 | ;; uh-oh, we have to set `comint-input-ring-size' before we call |
---|
| 3653 | ;; `comint-mode' |
---|
| 3654 | (singular-history-init) |
---|
[02cc28] | 3655 | |
---|
[6d68ac] | 3656 | ;; run comint mode and do basic mode setup |
---|
[f50151] | 3657 | (let (comint-mode-hook) |
---|
[dd7371b] | 3658 | (comint-mode) |
---|
| 3659 | (singular-comint-init)) |
---|
[4701a1a] | 3660 | (setq major-mode 'singular-interactive-mode) |
---|
[af6461] | 3661 | (setq mode-name "Singular Interaction") |
---|
[99e62ad] | 3662 | |
---|
[71c107] | 3663 | ;; some other initialization found in no folding |
---|
[ef85f4] | 3664 | (setq comment-start "// ") |
---|
| 3665 | (setq comment-start-skip "// *") |
---|
| 3666 | (setq comment-end "") |
---|
[4701a1a] | 3667 | |
---|
[71c107] | 3668 | ;; initialize singular input and output filters. This should be done |
---|
| 3669 | ;; first as the filters are accessed in the following initialization |
---|
| 3670 | ;; functions. NOT READY [should be moved to the respective foldings] |
---|
[81da6ba] | 3671 | (make-local-variable 'singular-pre-input-filter-functions) |
---|
| 3672 | (make-local-hook 'singular-post-input-filter-functions) |
---|
| 3673 | (make-local-variable 'singular-pre-output-filter-functions) |
---|
| 3674 | (make-local-hook 'singular-post-output-filter-functions) |
---|
[570cea] | 3675 | |
---|
[71c107] | 3676 | (singular-interactive-mode-map-init) |
---|
| 3677 | (singular-mode-syntax-table-init) |
---|
| 3678 | (singular-interactive-mode-menu-init) |
---|
| 3679 | (singular-demo-mode-init) |
---|
[f50151] | 3680 | (singular-folding-init) |
---|
| 3681 | (singular-help-init) |
---|
[71c107] | 3682 | (singular-prompt-init) |
---|
[dd7371b] | 3683 | (singular-exec-init) |
---|
[81da6ba] | 3684 | |
---|
[71c107] | 3685 | ;; Font Lock mode initialization for Emacs. For XEmacs, it is done at |
---|
| 3686 | ;; singular.el loading time. |
---|
[ef85f4] | 3687 | (cond |
---|
| 3688 | ;; Emacs |
---|
| 3689 | ((eq singular-emacs-flavor 'emacs) |
---|
[f50151] | 3690 | (singular-interactive-font-lock-init))) |
---|
[ef85f4] | 3691 | |
---|
[71c107] | 3692 | ;; debugging filter initialization |
---|
| 3693 | (singular-debug 'interactive-filter |
---|
| 3694 | (singular-debug-filter-init)) |
---|
| 3695 | |
---|
[af6461] | 3696 | (run-hooks 'singular-interactive-mode-hook)) |
---|
[4701a1a] | 3697 | ;;}}} |
---|
| 3698 | |
---|
[1735b4e] | 3699 | ;;{{{ Starting singular |
---|
[9ad353] | 3700 | (defcustom singular-same-window t |
---|
| 3701 | "*Specifies how to open the window for Singular sessions. |
---|
| 3702 | If this variable equals t, Singular comes up in the selected window. |
---|
| 3703 | If this variable equals nil, Singular comes up in another window. |
---|
| 3704 | If this variable equals neither t nor nil, the standard Emacs behaviour to |
---|
| 3705 | open the window is adopted (which very much depends on the settings of |
---|
| 3706 | `same-window-buffer-names')." |
---|
| 3707 | :initialize 'custom-initialize-default |
---|
| 3708 | :type '(choice (const :tag "This window" t) |
---|
| 3709 | (const :tag "Other window" nil) |
---|
| 3710 | (const :tag "Default" default)) |
---|
| 3711 | :group 'singular-interactive-miscellaneous) |
---|
| 3712 | |
---|
[aecc57] | 3713 | (defcustom singular-start-file "~/.emacs_singularrc" |
---|
[b7b6c7f] | 3714 | "*Name of start-up file to pass to Singular. |
---|
[a0cde2] | 3715 | If the file named by this variable exists it is given as initial input |
---|
| 3716 | to any Singular process being started. Note that this may lose due to |
---|
[aecc57] | 3717 | a timing error if Singular discards input when it starts up." |
---|
| 3718 | :type 'file |
---|
| 3719 | :initialize 'custom-initialize-default |
---|
| 3720 | :group 'singular-interactive-miscellaneous) |
---|
[4701a1a] | 3721 | |
---|
[aecc57] | 3722 | (defcustom singular-executable-default "Singular" |
---|
[b7b6c7f] | 3723 | "*Default name of Singular executable. |
---|
[aecc57] | 3724 | Used by `singular' when new Singular processes are started. |
---|
| 3725 | If the name is given without path the executable is searched using the |
---|
| 3726 | `PATH' environment variable." |
---|
| 3727 | :type 'file |
---|
| 3728 | :initialize 'custom-initialize-default |
---|
| 3729 | :group 'singular-interactive-miscellaneous) |
---|
[4701a1a] | 3730 | |
---|
[c100d9] | 3731 | (defvar singular-executable-last singular-executable-default |
---|
[aecc57] | 3732 | "Singular executable name of the last Singular command used. |
---|
[4701a1a] | 3733 | |
---|
[aecc57] | 3734 | This variable is buffer-local.") |
---|
| 3735 | |
---|
| 3736 | (defcustom singular-directory-default nil |
---|
[b7b6c7f] | 3737 | "*Default working directory of Singular buffer. |
---|
[aecc57] | 3738 | Should be either nil (which means do not set the default directory) or an |
---|
| 3739 | existing directory." |
---|
| 3740 | :type '(choice (const nil) (directory :value "~/")) |
---|
| 3741 | :initialize 'custom-initialize-default |
---|
| 3742 | :group 'singular-interactive-miscellaneous) |
---|
[c100d9] | 3743 | |
---|
| 3744 | (defvar singular-directory-last singular-directory-default |
---|
[aecc57] | 3745 | "Working directory of last Singular command used. |
---|
| 3746 | |
---|
| 3747 | This variable is buffer-local.") |
---|
[c100d9] | 3748 | |
---|
| 3749 | ;; no singular-directory-history here. Usual file history is used. |
---|
| 3750 | |
---|
[aecc57] | 3751 | (defcustom singular-switches-default '() |
---|
[b7b6c7f] | 3752 | "*List of default switches for Singular processes. |
---|
[aecc57] | 3753 | Should be a list of strings, one string for each switch. |
---|
| 3754 | Used by `singular' when new Singular processes are started." |
---|
| 3755 | :type '(repeat string) |
---|
| 3756 | :initialize 'custom-initialize-default |
---|
| 3757 | :group 'singular-interactive-miscellaneous) |
---|
[c100d9] | 3758 | |
---|
| 3759 | (defvar singular-switches-last singular-switches-default |
---|
[aecc57] | 3760 | "Switches of last Singular command used. |
---|
| 3761 | |
---|
| 3762 | This variable is buffer-local.") |
---|
[c100d9] | 3763 | |
---|
| 3764 | (defvar singular-switches-history nil |
---|
[aecc57] | 3765 | "History list of Singular switches.") |
---|
[c100d9] | 3766 | |
---|
[ab733d] | 3767 | (defvar singular-switches-magic '("-t" "--exec" "if (system(\"version\") > 1304){system(\"--emacs\", 1);};") |
---|
[c100d9] | 3768 | "Additional magic switches for Singular process. |
---|
| 3769 | List of switch-strings which are automagically added when new Singular |
---|
[2690cd] | 3770 | processes are started, one string for each command line argument. |
---|
| 3771 | This list should at least contain the options \"--emacs\" and \"-t\". If |
---|
[ab733d] | 3772 | you are running a Singular with version < 1.2 , remove option \"--exec\" |
---|
[2690cd] | 3773 | from the list.") |
---|
[c100d9] | 3774 | |
---|
[aecc57] | 3775 | (defcustom singular-name-default "singular" |
---|
[b7b6c7f] | 3776 | "*Default process name for Singular process. |
---|
[aecc57] | 3777 | Used by `singular' when new Singular processes are started. |
---|
| 3778 | This string surrounded by \"*\" will also be the buffer name." |
---|
| 3779 | :type 'string |
---|
| 3780 | :initialize 'custom-initialize-default |
---|
| 3781 | :group 'singular-interactive-miscellaneous) |
---|
[4701a1a] | 3782 | |
---|
[c100d9] | 3783 | (defvar singular-name-last singular-name-default |
---|
[9ad353] | 3784 | "process name of the last Singular command used. |
---|
[aecc57] | 3785 | |
---|
| 3786 | This variable is buffer-local.") |
---|
[c100d9] | 3787 | |
---|
| 3788 | (defvar singular-name-history nil |
---|
[aecc57] | 3789 | "History list of Singular process names.") |
---|
[c100d9] | 3790 | |
---|
| 3791 | (defun singular-exec-init () |
---|
| 3792 | "Initialize defaults for starting Singular. |
---|
| 3793 | |
---|
| 3794 | This function is called at mode initialization time." |
---|
| 3795 | (singular-debug 'interactive (message "Initializing exec")) |
---|
| 3796 | (set (make-local-variable 'singular-executable-last) |
---|
| 3797 | singular-executable-default) |
---|
| 3798 | (set (make-local-variable 'singular-directory-last) |
---|
| 3799 | singular-directory-default) |
---|
| 3800 | (set (make-local-variable 'singular-name-last) |
---|
| 3801 | singular-name-default) |
---|
| 3802 | (set (make-local-variable 'singular-switches-last) |
---|
[fffd22] | 3803 | singular-switches-default) |
---|
| 3804 | (set (make-local-variable 'singular-exit-insert-killed-marker) |
---|
| 3805 | nil) |
---|
| 3806 | (set (make-local-variable 'singular-exit-cleanup-done) |
---|
| 3807 | nil)) |
---|
[c100d9] | 3808 | |
---|
[fffd22] | 3809 | (defvar singular-exit-cleanup-done nil |
---|
| 3810 | "Switch indicating if cleanup after Singular exit is already done. |
---|
| 3811 | Initial value is nil. Is set to t by `singular-exit-cleanup' and to nil by |
---|
| 3812 | `singular-exit-sentinel'. |
---|
| 3813 | |
---|
| 3814 | This variable is buffer-local.") |
---|
| 3815 | |
---|
| 3816 | (defun singular-exit-cleanup () |
---|
| 3817 | "Clean up after termination of Singular. |
---|
[aecc57] | 3818 | Writes back input ring after regular termination of Singular if process |
---|
| 3819 | buffer is still alive, deinstalls the library menu und calls several other |
---|
[fffd22] | 3820 | exit procedures. |
---|
| 3821 | Assumes that the current buffer is a Singular buffer. |
---|
| 3822 | Sets the variable `singular-exit-cleanup-done' to t. |
---|
| 3823 | |
---|
| 3824 | This function is called by `singular-interrupt-singular' or by |
---|
| 3825 | `singular-exit-sentinel'." |
---|
| 3826 | (singular-debug 'interactive |
---|
| 3827 | (message "exit-cleanup called")) |
---|
[6e3bdda] | 3828 | (singular-demo-exit t) |
---|
[fffd22] | 3829 | (singular-scan-header-exit) |
---|
| 3830 | (singular-menu-deinstall-libraries) |
---|
| 3831 | (singular-history-write) |
---|
| 3832 | (setq singular-exit-cleanup-done t)) |
---|
| 3833 | |
---|
| 3834 | (defun singular-exit-sentinel (process message) |
---|
| 3835 | "Clean up after termination of Singular. |
---|
[8d3763] | 3836 | Calls `singular-exit-cleanup' if `singular-exit-cleanup-done' is nil." |
---|
[4701a1a] | 3837 | (save-excursion |
---|
| 3838 | (singular-debug 'interactive |
---|
[845cb34] | 3839 | (message "Sentinel: %s" (substring message 0 -1))) |
---|
[fffd22] | 3840 | |
---|
| 3841 | (if (string-match "finished\\|exited\\|killed" message) |
---|
[4701a1a] | 3842 | (let ((process-buffer (process-buffer process))) |
---|
[8d3763] | 3843 | (if (and (not singular-exit-cleanup-done) |
---|
| 3844 | process-buffer |
---|
| 3845 | (buffer-name process-buffer) |
---|
| 3846 | (set-buffer process-buffer)) |
---|
| 3847 | (singular-exit-cleanup)))) |
---|
[fffd22] | 3848 | (setq singular-exit-cleanup-done nil))) |
---|
| 3849 | |
---|
| 3850 | (defun singular-interrupt-singular () |
---|
| 3851 | "Delete the Singular process running in the current buffer. |
---|
[8d3763] | 3852 | Calls `singular-exit-cleanup' and deletes the Singular process. |
---|
| 3853 | Inserts a string indicating that the Singular process is killed." |
---|
| 3854 | (let* ((process (singular-process)) |
---|
| 3855 | (mark (marker-position (process-mark process)))) |
---|
[fffd22] | 3856 | (singular-exit-cleanup) |
---|
[8d3763] | 3857 | (delete-process process) |
---|
| 3858 | (save-excursion |
---|
[6e3bdda] | 3859 | ;; Because of timing problems it would be better if |
---|
| 3860 | ;; singular-exit-sentinel would insert this string (see Version 1.41) |
---|
| 3861 | ;; but this is not possible for XEmacs: The function (process-mark) |
---|
| 3862 | ;; called within singular-exit-sentinel returns a mark with no |
---|
| 3863 | ;; associated buffer! |
---|
[8d3763] | 3864 | (goto-char mark) |
---|
| 3865 | (insert "// ** Singular process killed **\n")))) |
---|
[4701a1a] | 3866 | |
---|
[845cb34] | 3867 | (defun singular-exec (buffer name executable start-file switches) |
---|
| 3868 | "Start a new Singular process NAME in BUFFER, running EXECUTABLE. |
---|
| 3869 | EXECUTABLE should be a string denoting an executable program. |
---|
| 3870 | SWITCHES should be a list of strings that are passed as command line |
---|
| 3871 | switches. START-FILE should be the name of a file which contents is |
---|
| 3872 | sent to the process. |
---|
| 3873 | |
---|
| 3874 | Deletes any old processes running in that buffer. |
---|
| 3875 | Moves point to the end of BUFFER. |
---|
| 3876 | Initializes all important markers and the simple sections. |
---|
[f50151] | 3877 | Runs the hooks on `singular-exec-hook'. |
---|
[845cb34] | 3878 | Returns BUFFER." |
---|
[570cea] | 3879 | (let ((old-buffer (current-buffer))) |
---|
[845cb34] | 3880 | (unwind-protect |
---|
| 3881 | (progn |
---|
| 3882 | (set-buffer buffer) |
---|
| 3883 | |
---|
| 3884 | ;; delete any old processes |
---|
| 3885 | (let ((process (get-buffer-process buffer))) |
---|
| 3886 | (if process (delete-process process))) |
---|
| 3887 | |
---|
| 3888 | ;; create new process |
---|
| 3889 | (singular-debug 'interactive (message "Starting new Singular")) |
---|
| 3890 | (let ((process (comint-exec-1 name buffer executable switches))) |
---|
| 3891 | |
---|
| 3892 | ;; set process filter and sentinel |
---|
| 3893 | (set-process-filter process 'singular-output-filter) |
---|
| 3894 | (set-process-sentinel process 'singular-exit-sentinel) |
---|
| 3895 | (make-local-variable 'comint-ptyp) |
---|
| 3896 | (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe. |
---|
| 3897 | |
---|
[81da6ba] | 3898 | ;; go to the end of the buffer, initialize I/O and simple |
---|
| 3899 | ;; sections |
---|
[845cb34] | 3900 | (goto-char (point-max)) |
---|
[81da6ba] | 3901 | (singular-input-filter-init (point)) |
---|
| 3902 | (singular-output-filter-init (point)) |
---|
[845cb34] | 3903 | (singular-simple-sec-init (point)) |
---|
[c100d9] | 3904 | |
---|
[aecc57] | 3905 | ;; completion should be initialized before scan header! |
---|
[c100d9] | 3906 | (singular-completion-init) |
---|
[aecc57] | 3907 | (singular-scan-header-init) |
---|
[845cb34] | 3908 | |
---|
[570cea] | 3909 | ;; feed process with start file and read input ring. Take |
---|
| 3910 | ;; care about the undo information. |
---|
[845cb34] | 3911 | (if start-file |
---|
[570cea] | 3912 | (let ((buffer-undo-list t) start-string) |
---|
[845cb34] | 3913 | (singular-debug 'interactive (message "Feeding start file")) |
---|
| 3914 | (sleep-for 1) ; try to avoid timing errors |
---|
| 3915 | (insert-file-contents start-file) |
---|
[570cea] | 3916 | (setq start-string (buffer-substring (point) (point-max))) |
---|
[845cb34] | 3917 | (delete-region (point) (point-max)) |
---|
[570cea] | 3918 | (send-string process start-string))) |
---|
[f50151] | 3919 | |
---|
| 3920 | ;; read history if present |
---|
| 3921 | (singular-history-read) |
---|
[845cb34] | 3922 | |
---|
| 3923 | ;; execute hooks |
---|
| 3924 | (run-hooks 'singular-exec-hook)) |
---|
| 3925 | |
---|
| 3926 | buffer) |
---|
| 3927 | ;; this code is unwide-protected |
---|
[570cea] | 3928 | (set-buffer old-buffer)))) |
---|
[845cb34] | 3929 | |
---|
[b7b6c7f] | 3930 | |
---|
| 3931 | ;; TODO: Documentation! |
---|
[845cb34] | 3932 | ;; Note: |
---|
| 3933 | ;; |
---|
| 3934 | ;; In contrast to shell.el, `singular' does not run |
---|
| 3935 | ;; `singular-interactive-mode' every time a new Singular process is |
---|
| 3936 | ;; started, but only when a new buffer is created. This behaviour seems |
---|
| 3937 | ;; more intuitive w.r.t. local variables and hooks. |
---|
| 3938 | |
---|
[c100d9] | 3939 | (defun singular-internal (executable directory switches name) |
---|
| 3940 | "Run an inferior Singular process, with I/O through an Emacs buffer. |
---|
[845cb34] | 3941 | |
---|
[c100d9] | 3942 | Appends `singular-switches-magic' to switches. |
---|
[aecc57] | 3943 | Sets default-directory if directory is not-nil. |
---|
[c100d9] | 3944 | Sets singular-*-last values." |
---|
| 3945 | (singular-debug 'interactive |
---|
| 3946 | (message "singular-internal: %s %s %s %s" |
---|
| 3947 | executable directory name switches)) |
---|
| 3948 | (let* ((buffer-name (singular-process-name-to-buffer-name name)) |
---|
[6d68ac] | 3949 | ;; buffer associated with Singular, nil if there is none |
---|
[af6461] | 3950 | (buffer (get-buffer buffer-name))) |
---|
[c100d9] | 3951 | |
---|
[eedab2] | 3952 | ;; make sure directory contains a "/" at the end |
---|
| 3953 | (setq directory |
---|
| 3954 | (concat directory |
---|
| 3955 | ;; we check for trailing slash and backslash |
---|
| 3956 | ;; but unconditionally insert a slash. |
---|
| 3957 | ;; Hopefully that works on NT, too. |
---|
| 3958 | (if (memq (aref directory |
---|
| 3959 | (1- (length directory))) |
---|
| 3960 | '(?/ ?\\)) |
---|
| 3961 | "" "/"))) |
---|
| 3962 | |
---|
[af6461] | 3963 | (if (not buffer) |
---|
| 3964 | (progn |
---|
[845cb34] | 3965 | ;; create new buffer and call `singular-interactive-mode' |
---|
[af6461] | 3966 | (singular-debug 'interactive (message "Creating new buffer")) |
---|
| 3967 | (setq buffer (get-buffer-create buffer-name)) |
---|
| 3968 | (set-buffer buffer) |
---|
[aecc57] | 3969 | (and directory |
---|
| 3970 | (setq default-directory directory)) |
---|
[c100d9] | 3971 | |
---|
[af6461] | 3972 | (singular-debug 'interactive (message "Calling `singular-interactive-mode'")) |
---|
| 3973 | (singular-interactive-mode))) |
---|
[c100d9] | 3974 | |
---|
[af6461] | 3975 | (if (not (comint-check-proc buffer)) |
---|
[845cb34] | 3976 | ;; create new process if there is none |
---|
| 3977 | (singular-exec buffer name executable |
---|
| 3978 | (if (file-exists-p singular-start-file) |
---|
| 3979 | singular-start-file) |
---|
[c100d9] | 3980 | (append switches singular-switches-magic))) |
---|
| 3981 | |
---|
[af6461] | 3982 | ;; pop to buffer |
---|
[4701a1a] | 3983 | (singular-debug 'interactive (message "Calling `pop-to-buffer'")) |
---|
[9ad353] | 3984 | (singular-pop-to-buffer singular-same-window buffer)) |
---|
[c100d9] | 3985 | |
---|
| 3986 | ;; Set buffer local singular-*-last-values |
---|
| 3987 | (setq singular-executable-last executable) |
---|
| 3988 | (setq singular-directory-last directory) |
---|
| 3989 | (setq singular-switches-last switches) |
---|
| 3990 | (setq singular-name-last name) |
---|
| 3991 | ;; Set global values, too |
---|
| 3992 | (set-default 'singular-executable-last executable) |
---|
| 3993 | (set-default 'singular-directory-last directory) |
---|
| 3994 | (set-default 'singular-switches-last switches) |
---|
| 3995 | (set-default 'singular-name-last name)) |
---|
[044d8a] | 3996 | |
---|
| 3997 | (defun singular-generate-new-buffer-name (name) |
---|
[99e62ad] | 3998 | "Generate a unique buffer name for a singular interactive buffer. |
---|
| 3999 | The string NAME is the desired name for the singular interactive |
---|
| 4000 | buffer, without surrounding stars. |
---|
| 4001 | The string returned is surrounded by stars. |
---|
| 4002 | |
---|
| 4003 | If no buffer with name \"*NAME*\" exists, return \"*NAME*\". |
---|
| 4004 | Otherwise check for buffer called \"*NAME<n>*\" where n is a |
---|
| 4005 | increasing number and return \"*NAME<n>*\" if no such buffer |
---|
| 4006 | exists." |
---|
| 4007 | (let ((new-name (singular-process-name-to-buffer-name name)) |
---|
[044d8a] | 4008 | (count 2)) |
---|
| 4009 | (while (get-buffer new-name) |
---|
| 4010 | (setq new-name (singular-process-name-to-buffer-name |
---|
| 4011 | (concat name "<" (format "%d" count) ">"))) |
---|
| 4012 | (setq count (1+ count))) |
---|
| 4013 | new-name)) |
---|
[99e62ad] | 4014 | |
---|
[c100d9] | 4015 | (defun singular () |
---|
[2690cd] | 4016 | "Run an inferior Singular process using default arguments. |
---|
| 4017 | Starts a Singular process, with I/O through an Emacs buffer, using the |
---|
| 4018 | values of `singular-executable-default', `singular-directory-default', |
---|
| 4019 | `singular-switches-default', and `singular-name-default'. |
---|
| 4020 | |
---|
| 4021 | For more information on starting a Singular process and on the arguments |
---|
| 4022 | see the documentation of `singular-other'. To restart a previously started |
---|
| 4023 | Singular process use `singular-restart'. |
---|
| 4024 | |
---|
| 4025 | Every time `singular' starts a new Singular process it runs the hooks |
---|
| 4026 | on `singular-exec-hook'. |
---|
| 4027 | |
---|
| 4028 | Type \\[describe-mode] in the Singular buffer for a list of commands." |
---|
[c100d9] | 4029 | (interactive) |
---|
| 4030 | (singular-internal singular-executable-default |
---|
| 4031 | singular-directory-default |
---|
| 4032 | singular-switches-default |
---|
| 4033 | singular-name-default)) |
---|
| 4034 | |
---|
| 4035 | (defun singular-restart () |
---|
[2690cd] | 4036 | "Run an inferior Singular process using the last arguments used. |
---|
| 4037 | Starts a Singular process, with I/O through an Emacs buffer, using the |
---|
| 4038 | previously used arguments. |
---|
| 4039 | If called within a Singular buffer, uses the arguments of the most recent |
---|
[fffd22] | 4040 | Singular process started in this buffer. If there is a Singular process |
---|
| 4041 | running in this buffer, it is deleted without warning! |
---|
[2690cd] | 4042 | If called outside a Singular buffer, uses the arguments of the most recent |
---|
[fffd22] | 4043 | Singular process started in any Singular buffer (and does not delete any |
---|
| 4044 | Singular process). |
---|
[2690cd] | 4045 | If no last values are available, uses the default values (see documentation |
---|
| 4046 | of `singular'). |
---|
| 4047 | |
---|
| 4048 | For more information on starting a Singular process and on the arguments |
---|
| 4049 | see the documentation of `singular-other'. |
---|
| 4050 | |
---|
| 4051 | Every time `singular-restarts' starts a new Singular process it runs the |
---|
| 4052 | hooks on `singular-exec-hook'. |
---|
| 4053 | |
---|
| 4054 | Type \\[describe-mode] in the Singular buffer for a list of commands." |
---|
[c100d9] | 4055 | (interactive) |
---|
[fffd22] | 4056 | |
---|
| 4057 | (let ((process (singular-process t))) |
---|
| 4058 | (and (eq (get-buffer (singular-process-name-to-buffer-name |
---|
| 4059 | singular-name-default)) |
---|
| 4060 | (current-buffer)) |
---|
| 4061 | process |
---|
| 4062 | (singular-interrupt-singular))) |
---|
| 4063 | |
---|
[c100d9] | 4064 | (singular-internal singular-executable-last |
---|
| 4065 | singular-directory-last |
---|
| 4066 | singular-switches-last |
---|
| 4067 | singular-name-last)) |
---|
| 4068 | |
---|
[9ad353] | 4069 | (defun singular-other (executable directory switches name) |
---|
[2690cd] | 4070 | "Run an inferior Singular process. |
---|
| 4071 | Starts a Singular process, with I/O through an Emacs buffer. |
---|
| 4072 | |
---|
| 4073 | If called interactively, the user is asked in the minibuffer area for an |
---|
| 4074 | existing executable (with or without path), an exisiting directory or nil |
---|
| 4075 | (if non-nil, sets the buffers default directory to this directory), the |
---|
| 4076 | complete command line arguments to be passed to Singular (as a single |
---|
| 4077 | string) and the buffer name of the singular buffer, which is surrounded by |
---|
| 4078 | \"*\", if not already. (The process name of the singular process is then |
---|
| 4079 | given by the buffer name with the surrounding stars stripped.) |
---|
| 4080 | |
---|
| 4081 | If called non-interactiveley, EXECUTABLE is the name of an existing |
---|
| 4082 | Singular executable (with or without path), DIRECTORY is the name of an |
---|
| 4083 | existing directory or nil. If non-nil, sets the buffers default directory |
---|
| 4084 | to DIRECTORY. SWITCHES is a list of strings where each string contains one |
---|
| 4085 | command line argument which is passed to Singular, and NAME is the process |
---|
| 4086 | name of the Singular process (that is, the singular buffer name is given by |
---|
| 4087 | NAME surrounded by \"*\"). |
---|
| 4088 | |
---|
| 4089 | If buffer exists but Singular is not running, starts new Singular. |
---|
| 4090 | If buffer exists and Singular is running, just switches to buffer. |
---|
| 4091 | If a file `~/.emacs_singularrc' exists, it is given as initial input. |
---|
| 4092 | Note that this may lose due to a timing error if Singular discards |
---|
| 4093 | input when it starts up. |
---|
| 4094 | |
---|
| 4095 | If a new buffer is created it is put in Singular interactive mode, |
---|
| 4096 | giving commands for sending input and handling output of Singular. See |
---|
| 4097 | `singular-interactive-mode'. |
---|
| 4098 | |
---|
| 4099 | Every time `singular-other' starts a new Singular process it runs the hooks |
---|
| 4100 | on `singular-exec-hook'. |
---|
| 4101 | |
---|
| 4102 | Type \\[describe-mode] in the Singular buffer for a list of commands." |
---|
| 4103 | (interactive |
---|
| 4104 | (let* ((exec (read-file-name "Singular executable: ")) |
---|
[c100d9] | 4105 | ;; Remark: Do NOT call `expand-file-name' after the |
---|
| 4106 | ;; above read-file-name! It has to be possible to enter a command |
---|
| 4107 | ;; without path which should be searched for in $PATH. |
---|
| 4108 | ;; `start-process' is intelligent enough to start commands with |
---|
| 4109 | ;; not-expanded name. |
---|
[2690cd] | 4110 | (dir (file-name-directory (read-file-name "Default directory: " |
---|
| 4111 | nil |
---|
| 4112 | (or singular-directory-default |
---|
| 4113 | default-directory) |
---|
| 4114 | t))) |
---|
| 4115 | (switch "") |
---|
| 4116 | (bufname (singular-generate-new-buffer-name |
---|
| 4117 | (downcase (file-name-nondirectory exec))))) |
---|
| 4118 | |
---|
| 4119 | ;; Get command line arguments and append magic switches |
---|
| 4120 | ;; TODO: Think about default value: Up to now: |
---|
| 4121 | ;; Use singular-switches-default as init value for read-from-minibuffer |
---|
| 4122 | (let ((switches-default singular-switches-default)) |
---|
| 4123 | (while switches-default |
---|
| 4124 | (setq switch (concat switch (car switches-default) " ")) |
---|
| 4125 | (setq switches-default (cdr switches-default)))) |
---|
| 4126 | ;; note: magic switches are appended by `singular-internal' |
---|
| 4127 | (setq switch (split-string (read-from-minibuffer "Singular options: " |
---|
| 4128 | switch nil nil |
---|
| 4129 | singular-switches-history) |
---|
| 4130 | " ")) |
---|
| 4131 | |
---|
| 4132 | ;; Generate new buffer name |
---|
| 4133 | (let (done) |
---|
| 4134 | (while (not done) |
---|
| 4135 | (setq bufname (read-from-minibuffer "Singular buffer name: " bufname)) |
---|
| 4136 | (setq done (or (not (get-buffer bufname)) |
---|
| 4137 | (y-or-n-p "Buffer exists. Switch to that buffer? "))))) |
---|
| 4138 | (if (string-match "^\\*\\(.*\\)\\*$" bufname) |
---|
| 4139 | (setq bufname (substring bufname (match-beginning 1) (match-end 1)))) |
---|
| 4140 | (list exec dir switch bufname))) |
---|
| 4141 | |
---|
| 4142 | (singular-internal executable directory switches name)) |
---|
[c100d9] | 4143 | |
---|
[fffd22] | 4144 | (defun singular-exit-singular (&optional kill-singular-buffer) |
---|
| 4145 | "Delete Singular process and kill Singular buffer. |
---|
| 4146 | Deletes the buffers Singular process without warning and writes back the input |
---|
| 4147 | history to file. |
---|
| 4148 | If called with prefix argument, kills the Singular buffer." |
---|
| 4149 | (interactive "P") |
---|
| 4150 | (singular-debug 'interactive |
---|
| 4151 | (message "exit singular called")) |
---|
| 4152 | |
---|
| 4153 | (singular-interrupt-singular) |
---|
| 4154 | (if kill-singular-buffer |
---|
| 4155 | (kill-buffer (current-buffer)))) |
---|
[4701a1a] | 4156 | ;;}}} |
---|
[a0cde2] | 4157 | ;;}}} |
---|
[4701a1a] | 4158 | |
---|
| 4159 | (provide 'singular) |
---|
| 4160 | |
---|
[f50151] | 4161 | ;;; Local Variables: |
---|
| 4162 | ;;; fill-column: 75 |
---|
| 4163 | ;;; End: |
---|
| 4164 | |
---|
[4701a1a] | 4165 | ;;; singular.el ends here. |
---|