/usr/share/common-lisp/source/mcclim/Drei/motion.lisp is in cl-mcclim 0.9.6.dfsg.cvs20100315-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | ;;; -*- Mode: Lisp; Package: DREI-MOTION; -*-
;;; (c) copyright 2006 by
;;; Taylor R. Campbell (campbell@mumble.net)
;;; (c) copyright 2006 by
;;; Troels Henriksen (athas@sigkill.dk)
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;; Drei Motion
;;; A basic motion function is a function named FORWARD-ONE-<unit>' or
;;; BACKWARD-ONE-<unit> of the signature (<mark> <syntax>) that
;;; returns true if any motion happened or false if a limit was
;;; reached.
;;;
;;; A general motion function is a function named FORWARD-<unit> or
;;; BACKWARD-<unit> of the signature (<mark> <syntax> &OPTIONAL
;;; (<count> 1) (<limit-action> #'ERROR-LIMIT-ACTION)) that returns
;;; true if it could move forward or backward over the requested
;;; number of units, <count>, which may be positive or negative; and
;;; calls the limit action if it could not, or returns nil if the
;;; limit action is nil.
;;;
;;; A limit action is a function usually named <mumble>-LIMIT-ACTION
;;; of the signature (<mark> <original-offset> <remaining-units>
;;; <unit> <syntax>) that is called whenever a general motion function
;;; cannot complete the motion. <Mark> is the mark the object in
;;; motion; <original-offset> is the original offset of the mark,
;;; before any motion; <remaining-units> is the number of units left
;;; until the motion would be complete; <unit> is a string naming the
;;; unit; and <syntax> is the syntax instance passed to the motion
;;; function.
;;;
;;; A motion command is a CLIM command named Forward <unit> or
;;; Backward <unit> which can take a numeric prefix argument and moves
;;; the point over the requested number, or 1, of units, by calling
;;; the general motion function FORWARD-<unit> or BACKWARD-<unit>.
;;;
;;; Given the basic motion functions FORWARD-ONE-<unit> and
;;; BACKWARD-ONE-<unit>,
;;;
;;; (DEFINE-MOTION-FNS <unit>)
;;;
;;; defines the general motion functions FORWARD-<unit> and
;;; BACKWARD-<unit>.
;;;
;;; NOTE: FORWARD-OBJECT and BACKWARD-OBJECT, by virtue of their
;;; low-level status and placement in the buffer protocol (see
;;; buffer.lisp) do not obey this protocol, in that they have no
;;; syntax argument. Therefore, all <frob>-OBJECT functions and
;;; commands lack this argument as well (FIXME? We could shadow the
;;; definition from the buffer protocol and just ignore the syntax
;;; argument). There are no FORWARD-ONE-OBJECT or BACKWARD-ONE-OBJECT
;;; functions.
(in-package :drei-motion)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Functions to move mark around based on the syntax:
(defun forward-to-word-boundary (mark syntax)
"Move the mark forward to the beginning of the next word."
(loop until (end-of-buffer-p mark)
until (word-constituentp syntax (object-after mark))
do (incf (offset mark))))
(defun backward-to-word-boundary (mark syntax)
"Move the mark backward to the end of the previous word."
(loop until (beginning-of-buffer-p mark)
until (word-constituentp syntax (object-before mark))
do (decf (offset mark))))
(defun beep-limit-action (mark original-offset remaining unit syntax)
"This limit action will beep at the user."
(declare (ignore mark original-offset remaining unit syntax))
(clim:beep)
nil)
(defun revert-limit-action (mark original-offset remaining unit syntax)
"This limit action will try to restore the mark state from
before the attempted action. Note that this will not restore any
destructive actions that have been performed, it will only
restore the position of `mark'."
(declare (ignore remaining unit syntax))
(setf (offset mark) original-offset)
nil)
(define-condition motion-limit-error (error)
((mark :initarg :mark)
(original-offset :initarg :original-offset)
(unit :initarg :unit)
(remaining :initarg :remaining)
(syntax :initarg :syntax))
(:documentation
"This error condition signifies that a motion cannot be performed.")
(:report (lambda (condition stream)
(format stream "Motion by ~A reached limit."
(slot-value condition 'UNIT)))))
(defun error-limit-action (mark original-offset remaining unit syntax)
"This limit action will signal an error of type
`motion-limit-error'."
(error 'MOTION-LIMIT-ERROR
:mark mark
:original-offset original-offset
:remaining remaining
:unit unit
:syntax syntax))
(defmacro define-motion-fns (unit &key plural)
(labels ((concat (&rest strings)
(apply #'concatenate 'string (mapcar #'string strings)))
(symbol (&rest strings)
(intern (apply #'concat strings))))
(let ((forward-one (symbol "FORWARD-ONE-" unit))
(backward-one (symbol "BACKWARD-ONE-" unit))
(forward (symbol "FORWARD-" unit))
(backward (symbol "BACKWARD-" unit))
(unit-name (string-downcase unit)))
(let ((plural (or plural (concat unit-name "s"))))
`(progn
(defgeneric ,forward
(mark syntax &optional count limit-action)
(:documentation
,(concat "Move MARK forward by COUNT " plural ".")))
(defgeneric ,backward
(mark syntax &optional count limit-action)
(:documentation
,(concat "Move MARK backward by COUNT " plural ".")))
(defmethod ,forward (mark syntax &optional
(count 1)
(limit-action #'error-limit-action))
(let ((offset (offset mark)))
(dotimes (i count t)
(if (not (,forward-one mark syntax))
(return (and limit-action
(funcall limit-action
mark
offset
(- count i)
,unit-name
syntax)))))))
(defmethod ,backward (mark syntax &optional
(count 1)
(limit-action #'error-limit-action))
(let ((offset (offset mark)))
(dotimes (i count t)
(if (not (,backward-one mark syntax))
(return (and limit-action
(funcall limit-action
mark
offset
(- i count)
,unit-name
syntax)))))))
(defmethod ,forward :around (mark syntax &optional
(count 1)
(limit-action :error))
(cond ((minusp count)
(,backward mark syntax (- count) limit-action))
((plusp count)
(call-next-method))
(t t)))
(defmethod ,backward :around (mark syntax &optional
(count 1)
(limit-action :error))
(cond ((minusp count)
(,forward mark syntax (- count) limit-action))
((plusp count)
(call-next-method))
(t t))))))))
(defun make-diligent-motor (motor fiddler)
"Create and return a diligent motor with a default limit action
of `beep-limit-action'. `Motor' and `fiddler' will take turns
being called until either `motor' succeeds or `fiddler' fails."
(labels ((make-limit-action (loser)
(labels ((limit-action
(mark original-offset remaining unit syntax)
(declare (ignore original-offset unit))
(and (funcall fiddler mark syntax 1 loser)
(funcall motor mark syntax
(if (minusp remaining)
(- -1 remaining)
(- remaining 1))
#'limit-action))))
#'limit-action))
(move (mark syntax &optional
(count 1)
(loser #'beep-limit-action))
(funcall motor mark syntax count
(make-limit-action loser))))
#'move))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Line start motion
(defgeneric forward-one-line-start (mark syntax)
(:documentation "Move MARK to the start of the next line."))
(defmethod forward-one-line-start (mark syntax)
(when (forward-object mark)
(loop until (beginning-of-line-p mark)
do (forward-object mark)
finally (return t))))
(defgeneric backward-one-line-start (mark syntax)
(:documentation "Move MARK to the end of the next line."))
(defmethod backward-one-line-start (mark syntax)
(when (backward-object mark)
(loop until (beginning-of-line-p mark)
do (backward-object mark)
finally (return t))))
(define-motion-fns line-start)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Line end motion
(defgeneric forward-one-line-end (mark syntax)
(:documentation "Move MARK to the end of the next line."))
(defmethod forward-one-line-end (mark syntax)
(when (forward-object mark)
(loop until (end-of-line-p mark)
do (forward-object mark)
finally (return t))))
(defgeneric backward-one-line-end (mark syntax)
(:documentation "Move MARK to the end of the previous line."))
(defmethod backward-one-line-end (mark syntax)
(when (backward-object mark)
(loop until (end-of-line-p mark)
do (backward-object mark)
finally (return t))))
(define-motion-fns line-end)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Word motion
(defgeneric forward-one-word (mark syntax)
(:documentation "Move MARK forward over the next word.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod forward-one-word (mark syntax)
(unless (end-of-buffer-p mark)
(forward-to-word-boundary mark syntax)
(loop until (end-of-buffer-p mark)
while (word-constituentp syntax (object-after mark))
do (forward-object mark)
finally (return t))))
(defgeneric backward-one-word (mark syntax)
(:documentation "Move MARK backward over the previous word.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod backward-one-word (mark syntax)
(unless (beginning-of-buffer-p mark)
(backward-to-word-boundary mark syntax)
(loop until (beginning-of-buffer-p mark)
while (word-constituentp syntax (object-before mark))
do (backward-object mark)
finally (return t))))
(define-motion-fns word)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Line motion
(defgeneric forward-one-line (mark syntax)
(:documentation
"Move MARK forward to the next line, preserving column.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod forward-one-line (mark syntax)
(let ((column (column-number mark)))
(end-of-line mark)
(handler-case (cond ((forward-object mark)
(setf (column-number mark) column)
t)
(t nil))
(motion-after-end ()))))
(defgeneric backward-one-line (mark syntax)
(:documentation
"Move MARK backward to the previous line, preserving column.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod backward-one-line (mark syntax)
(let ((column (column-number mark)))
(beginning-of-line mark)
(handler-case (cond ((backward-object mark)
(setf (column-number mark) column)
t)
(t nil))
(motion-before-beginning ()))))
(define-motion-fns line)
;; Faster version for special mark... I don't know whether it's ever
;; going to be used, but it was in the old motion code.
(defmethod backward-line ((mark p-line-mark-mixin) syntax
&optional (count 1)
(limit-action
#'error-limit-action))
(let* ((column (column-number mark))
(line (line-number mark))
(goto-line (- line count)))
(handler-case
(setf (offset mark)
(+ column
(buffer-line-offset (buffer mark) goto-line)))
(invalid-motion ()
(funcall limit-action mark
(offset mark) (- count line)
"line" syntax)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Page motion
(defgeneric forward-one-page (mark syntax)
(:documentation
"Move MARK forward over the next page.
Return T if successful, or NIL if the buffer limit was
reached."))
(defmethod forward-one-page (mark syntax)
(unless (end-of-buffer-p mark)
(forward-object mark 1)
(if (search-forward mark (coerce (page-delimiter syntax) 'vector))
(progn (backward-object mark (length (page-delimiter syntax)))
t)
(progn (end-of-buffer mark)
nil))))
(defgeneric backward-one-page (mark syntax)
(:documentation
"Move MARK backward to the previous page.
Return T if successful, or NIL if the buffer limit was
reached."))
(defmethod backward-one-page (mark syntax)
(unless (beginning-of-buffer-p mark)
(backward-object mark 1)
(if (search-backward mark (coerce (page-delimiter syntax) 'vector))
(progn (forward-object mark (length (page-delimiter syntax)))
t)
(progn (beginning-of-buffer mark)
t))))
(define-motion-fns page)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Expression motion
(defgeneric forward-one-expression (mark syntax)
(:documentation
"Move MARK forward over the next expression.
Return T if successful, or NIL if the buffer limit or the end of the
enclosing expression was reached."))
(defmethod forward-one-expression (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric backward-one-expression (mark syntax)
(:documentation
"Move MARK backward over the previous expression.
Return T if successful, or NIL if the buffer limit or the start of the
enclosing expression was reached."))
(defmethod backward-one-expression (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric forward-one-definition (mark syntax)
(:documentation
"Move MARK forward over the next definition.
Return T if successful, or NIL if the buffer limit was
reached."))
(defmethod forward-one-definition (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric backward-one-definition (mark syntax)
(:documentation
"Move MARK backward over the previous definition.
Return T if successful, or NIL if the buffer limit was
reached."))
(defmethod backward-one-definition (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric forward-one-up (mark syntax)
(:documentation
"Move MARK forward by one nesting level up.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod forward-one-up (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric backward-one-up (mark syntax)
(:documentation
"Move MARK backward by one nesting level up.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod backward-one-up (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric forward-one-down (mark syntax)
(:documentation
"Move MARK forward by one nesting level down.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod forward-one-down (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(defgeneric backward-one-down (mark syntax)
(:documentation
"Move MARK backward by one nesting level down.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod backward-one-down (mark syntax)
(declare (ignore mark syntax))
(error 'NO-SUCH-OPERATION))
(define-motion-fns expression)
(define-motion-fns definition)
(define-motion-fns up :plural "nesting levels up")
(define-motion-fns down :plural "nesting levels down")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Paragraph motion
(defgeneric backward-one-paragraph (mark syntax)
(:documentation
"Move MARK backward by one paragraph.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod backward-one-paragraph (mark syntax)
(unless (beginning-of-buffer-p mark)
(backward-object mark 1)
(if (search-backward mark (coerce (paragraph-delimiter syntax) 'vector))
(forward-object mark (length (paragraph-delimiter syntax)))
(beginning-of-buffer mark))
t))
(defgeneric forward-one-paragraph (mark syntax)
(:documentation
"Move MARK forward by one paragraph.
Return T if successful, or NIL if the buffer limit was reached."))
(defmethod forward-one-paragraph (mark syntax)
(unless (end-of-buffer-p mark)
(forward-object mark 1)
(if (search-forward mark (coerce (paragraph-delimiter syntax) 'vector))
(backward-object mark (length (paragraph-delimiter syntax)))
(end-of-buffer mark))
t))
(define-motion-fns paragraph)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; List motion
(defgeneric backward-one-list (mark syntax)
(:documentation
"Move MARK backward by one list.
Return T if successful, or NIL if the buffer limit was reached.")
(:method (mark syntax)
(error 'no-such-operation)))
(defgeneric forward-one-list (mark syntax)
(:documentation
"Move MARK forward by one list.
Return T if successful, or NIL if the buffer limit was reached.")
(:method (mark syntax)
(error 'no-such-operation)))
(define-motion-fns list)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Sentence motion
(defgeneric backward-one-sentence (mark syntax)
(:documentation
"Move MARK backward by one sentence.
Return T if successful, or NIL if the buffer limit was reached.")
(:method (mark syntax)
(error 'no-such-operation)))
(defgeneric forward-one-sentence (mark syntax)
(:documentation
"Move MARK forward by one sentence.
Return T if successful, or NIL if the buffer limit was reached.")
(:method (mark syntax)
(error 'no-such-operation)))
(define-motion-fns sentence)
;;; Paredit-like motion operations: move forward or backward across
;;; expressions, until the limits of the enclosing expression are
;;; reached; then move up a level.
(declaim (ftype function
forward-expression-or-up
backward-expression-or-up))
(setf (fdefinition 'FORWARD-EXPRESSION-OR-UP)
(make-diligent-motor #'forward-expression #'forward-up))
(setf (fdefinition 'BACKWARD-EXPRESSION-OR-UP)
(make-diligent-motor #'backward-expression #'backward-up))
|