syntax.lisp 21.3 KB
Newer Older
ram's avatar
ram committed
1 2 3
;;; -*- Log: hemlock.log; Package: Hemlock-Internals -*-
;;;
;;; **********************************************************************
4 5 6 7
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
8
  "$Header: src/hemlock/syntax.lisp $")
9
;;;
ram's avatar
ram committed
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
;;; **********************************************************************
;;;
;;; Hemlock syntax table routines.
;;;
;;; Written by Rob MacLachlan.
;;;

(in-package "HEMLOCK-INTERNALS")

(export '(character-attribute-name
	  defattribute character-attribute-documentation character-attribute
	  character-attribute-hooks character-attribute-p shadow-attribute
	  unshadow-attribute find-attribute reverse-find-attribute))

;;;; Character attribute caching.
;;;
;;;    In order to permit the %SP-Find-Character-With-Attribute sub-primitive
;;; to be used for a fast implementation of find-attribute and
;;; reverse-find-attribute, there must be some way of translating 
;;; attribute/test-function pairs into a attribute vector and a mask.
;;;    What we do is maintain a eq-hash-cache of attribute/test-function
;;; pairs.  If the desired pair is not in the cache then we reclaim an old
;;; attribute bit in the bucket we hashed to and stuff it by calling the
;;; test function on the value of the attribute for all characters.

(defvar *character-attribute-cache* ()
  "This is the cache used to translate attribute/test-function pairs to
  attribute-vector/mask pairs for find-attribute and reverse-find-attribute.")

(eval-when (compile eval)
(defconstant character-attribute-cache-size 13
  "The number of buckets in the *character-attribute-cache*.")
(defconstant character-attribute-bucket-size 3
  "The number of bits to use in each bucket of the
  *character-attribute-cache*.")
); eval-when (compile eval)

;;;    In addition, since a common pattern in code which uses find-attribute
;;; is to repeatedly call it with the same function and attribute, we
;;; remember the last attribute/test-function pair that was used, and check
;;; if it is the same pair beforehand, thus often avoiding the hastable lookup.
;;;
(defvar *last-find-attribute-attribute* ()
  "The attribute which we last did a find-attribute on.")
(defvar *last-find-attribute-function* ()
  "The last test-function used for find-attribute.")
(defvar *last-find-attribute-vector* ()
  "The %SP-Find-Character-With-Attribute vector corresponding to the last
  attribute/function pair used for find-attribute.")
(defvar *last-find-attribute-mask* ()
  "The the mask to use with *last-find-attribute-vector* to do a search
  for the last attribute/test-function pair.")
(defvar *last-find-attribute-end-wins* ()
  "The the value of End-Wins for the last attribute/test-function pair.")


(defvar *character-attributes* (make-hash-table :test #'eq)
  "A hash table which translates character attributes to their values.")
(defvar *last-character-attribute-requested* nil
  "The last character attribute which was asked for, Do Not Bind.")
(defvar *value-of-last-character-attribute-requested* nil
  "The value of the most recent character attribute, Do Not Bind.")

73
(declaim (special *character-attribute-names*))
ram's avatar
ram committed
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


;;; Each bucket contains a list of character-attribute-bucket-size
;;; bit-descriptors.
;;;
(defstruct (bit-descriptor)
  function		      ; The test on the attribute.
  attribute		      ; The attribute this is a test of.
  (mask 0 :type fixnum)	      ; The mask for the corresponding bit.
  vector		      ; The vector the bit is in.
  end-wins)		      ; Is this test true of buffer ends?

;;;
;;; In a descriptor for an unused bit, the function is nil, preventing a
;;; hit.  Whenever we change the value of an attribute for some character,
;;; we need to flush the cache of any entries for that attribute.  Currently
;;; we do this by mapping down the list of all bit descriptors.  Note that
;;; we don't have to worry about GC, since this is just a hint.
;;;
(defvar *all-bit-descriptors* () "The list of all the bit descriptors.")

(eval-when (compile eval)
(defmacro allocate-bit (vec bit-num)
  `(progn
    (when (= ,bit-num 8)
      (setq ,bit-num 0  ,vec (make-array 256 :element-type '(mod 256))))
    (car (push (make-bit-descriptor
		:vector ,vec
		:mask (ash 1 (prog1 ,bit-num (incf ,bit-num))))
	       *all-bit-descriptors*)))))
;;;    
(defun %init-syntax-table ()
  (let ((tab (make-array character-attribute-cache-size))
	(bit-num 8) vec)
    (setq *character-attribute-cache* tab)
    (dotimes (c character-attribute-cache-size)
      (setf (svref tab c)
	    (do ((i 0 (1+ i))
		 (res ()))
		((= i character-attribute-bucket-size) res)
	      (push (allocate-bit vec bit-num) res))))))

(eval-when (compile eval)
(defmacro hash-it (attribute function)
  `(abs (rem (logxor (ash (lisp::%sp-make-fixnum ,attribute) -3)
		     (lisp::%sp-make-fixnum ,function))
	     character-attribute-cache-size)))

;;; CACHED-ATTRIBUTE-LOOKUP  --  Internal
;;;
;;;    Sets Vector and Mask such that they can be used as arguments
;;; to %sp-find-character-with-attribute to effect a search with attribute 
;;; Attribute and test Function.  If the function and attribute
;;; are the same as the last ones then we just set them to that, otherwise
;;; we do the hash-cache lookup and update the *last-find-attribute-<mumble>*
;;;
(defmacro cached-attribute-lookup (attribute function vector mask end-wins)
  `(if (and (eq ,function *last-find-attribute-function*)
	    (eq ,attribute *last-find-attribute-attribute*))
       (setq ,vector *last-find-attribute-vector*
	     ,mask *last-find-attribute-mask*
	     ,end-wins *last-find-attribute-end-wins*)
       (let ((bit (svref *character-attribute-cache*
			 (hash-it ,attribute ,function))))
	 ,(do ((res `(multiple-value-setq (,vector ,mask ,end-wins)
		       (new-cache-attribute ,attribute ,function))
		    `(let ((b (car bit)))
		       (cond
			((and (eq (bit-descriptor-function b)
				  ,function)
			      (eq (bit-descriptor-attribute b)
				  ,attribute))
			 (setq ,vector (bit-descriptor-vector b)
			       ,mask (bit-descriptor-mask b)
			       ,end-wins (bit-descriptor-end-wins b)))
			(t
			 (setq bit (cdr bit)) ,res))))
	       (count 0 (1+ count)))
	      ((= count character-attribute-bucket-size) res))
	 (setq *last-find-attribute-attribute* ,attribute
	       *last-find-attribute-function* ,function
	       *last-find-attribute-vector* ,vector
	       *last-find-attribute-mask* ,mask
	       *last-find-attribute-end-wins* ,end-wins))))
); eval-when (compile eval)

;;; NEW-CACHE-ATTRIBUTE  --  Internal
;;;
;;;    Pick out an old attribute to punt out of the cache and put in the
;;; new one.  We pick a bit off of the end of the bucket and pull it around
;;; to the beginning to get a degree of LRU'ness.
;;;
(defun new-cache-attribute (attribute function)
  (let* ((hash (hash-it attribute function))
168 169 170
	 (values (or (gethash attribute *character-attributes*)
		     (error "~S is not a defined character attribute."
			    attribute)))
ram's avatar
ram committed
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
	 (bucket (svref *character-attribute-cache* hash))
	 (bit (nthcdr (- character-attribute-bucket-size 2) bucket))
	 (end-wins (funcall function (attribute-descriptor-end-value values))))
    (shiftf bit (cdr bit) nil)
    (setf (svref *character-attribute-cache* hash) bit
	  (cdr bit) bucket  bit (car bit))
    (setf (bit-descriptor-attribute bit) attribute
	  (bit-descriptor-function bit) function
	  (bit-descriptor-end-wins bit) end-wins)
    (setq values (attribute-descriptor-vector values))
    (do ((mask (bit-descriptor-mask bit))
	 (fun (bit-descriptor-function bit))
	 (vec (bit-descriptor-vector bit))
	 (i 0 (1+ i)))
	((= i syntax-char-code-limit) (values vec mask end-wins))
      (declare (type (simple-array (mod 256)) vec))
      (if (funcall fun (aref (the simple-array values) i))
	  (setf (aref vec i) (logior (aref vec i) mask))
	  (setf (aref vec i) (logandc2 (aref vec i) mask))))))

(defun %print-attribute-descriptor (object stream depth)
  (declare (ignore depth))
  (format stream "#<Hemlock Attribute-Descriptor ~S>"
	  (attribute-descriptor-name object)))

;;; DEFATTRIBUTE  --  Public
;;;
;;;    Make a new vector of some type and enter it in the table.
;;;
(defun defattribute (name documentation &optional (type '(mod 2))
			  (initial-value 0))
  "Define a new Hemlock character attribute with named Name with
  the supplied Documentation, Type and Initial-Value.  Type
  defaults to (mod 2) and Initial-Value defaults to 0."
  (setq name (coerce name 'simple-string))
  (let* ((attribute (string-to-keyword name))
	 (new (make-attribute-descriptor
	       :vector (make-array syntax-char-code-limit
				   :element-type type
				   :initial-element initial-value)
	       :name name
	       :keyword attribute
	       :documentation documentation
	       :end-value initial-value)))
    (when (gethash attribute *character-attributes*)
      (warn "Character Attribute ~S is being redefined." name))
    (setf (getstring name *character-attribute-names*) attribute)
    (setf (gethash attribute *character-attributes*) new))
  name)

;;; WITH-ATTRIBUTE  --  Internal
;;;
;;;    Bind obj to the attribute descriptor corresponding to symbol,
;;; giving error if it is not a defined attribute.
;;;
(eval-when (compile eval)
(defmacro with-attribute (symbol &body forms)
  `(let ((obj (gethash ,symbol *character-attributes*)))
     (unless obj
       (error "~S is not a defined character attribute." ,symbol))
     ,@forms))
); eval-when (compile eval)

(defun character-attribute-name (attribute)
  "Return the string-name of the character-attribute Attribute."
  (with-attribute attribute
    (attribute-descriptor-name obj)))

(defun character-attribute-documentation (attribute)
  "Return the documentation for the character-attribute Attribute."
  (with-attribute attribute
    (attribute-descriptor-documentation obj)))

(defun character-attribute-hooks (attribute)
  "Return the hook-list for the character-attribute Attribute.  This can
  be set with Setf."
  (with-attribute attribute
    (attribute-descriptor-hooks obj)))

(defun %set-character-attribute-hooks (attribute new-value)
  (with-attribute attribute
    (setf (attribute-descriptor-hooks obj) new-value)))

254
(declaim (special *last-character-attribute-requested*
ram's avatar
ram committed
255 256 257 258 259 260
		    *value-of-last-character-attribute-requested*))

;;; CHARACTER-ATTRIBUTE  --  Public
;;;
;;;    Return the value of a character attribute for some character.
;;;
261
(declaim (inline character-attribute))
ram's avatar
ram committed
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
(defun character-attribute (attribute character)
  "Return the value of the the character-attribute Attribute for Character.
  If Character is Nil then return the end-value."
  (if (and (eq attribute *last-character-attribute-requested*) character)
      (aref (the simple-array *value-of-last-character-attribute-requested*)
	    (syntax-char-code character))
      (sub-character-attribute attribute character)))
;;;
(defun sub-character-attribute (attribute character)
  (with-attribute attribute
    (setq *last-character-attribute-requested* attribute)
    (setq *value-of-last-character-attribute-requested*
	  (attribute-descriptor-vector obj))
    (if character
	(aref (the simple-array *value-of-last-character-attribute-requested*)
	      (syntax-char-code character))
	(attribute-descriptor-end-value obj))))

;;; CHARACTER-ATTRIBUTE-P
;;;
;;;    Look up attribute in table.
;;;
(defun character-attribute-p (symbol)
  "Return true if Symbol is the symbol-name of a character-attribute, Nil
  otherwise."
  (not (null (gethash symbol *character-attributes*))))


;;; %SET-CHARACTER-ATTRIBUTE  --  Internal
;;;
;;;    Set the value of a character attribute.
;;;
(defun %set-character-attribute (attribute character new-value)
  (with-attribute attribute
    (invoke-hook ed::character-attribute-hook attribute character new-value)
    (invoke-hook (attribute-descriptor-hooks obj) attribute character new-value)
    (cond
     ;;
     ;; Setting the value for a real character.
     (character
      (let ((value (attribute-descriptor-vector obj))
	    (code (syntax-char-code character)))
	(declare (type (simple-array *) value))
	(dolist (bit *all-bit-descriptors*)
	  (when (eq (bit-descriptor-attribute bit) attribute)
	    (let ((vec (bit-descriptor-vector bit)))
	      (declare (type (simple-array (mod 256)) vec))
	      (setf (aref vec code)
		    (if (funcall (bit-descriptor-function bit) new-value)
			(logior (bit-descriptor-mask bit) (aref vec code))
			(logandc1 (bit-descriptor-mask bit) (aref vec code)))))))
	(setf (aref value code) new-value)))
     ;;
     ;; Setting the magical end-value.
     (t
      (setf (attribute-descriptor-end-value obj) new-value)
      (dolist (bit *all-bit-descriptors*)
	(when (eq (bit-descriptor-attribute bit) attribute)
	  (setf (bit-descriptor-end-wins bit)
		(funcall (bit-descriptor-function bit) new-value))))
      new-value))))

(eval-when (compile eval)
;;; swap-one-attribute  --  Internal
;;;
;;;    Install the mode-local values described by Vals for Attribute, whose
;;; representation vector is Value.
;;;
 (defmacro swap-one-attribute (attribute value vals hooks)
  `(progn
    ;; Fix up any cached attribute vectors.
    (dolist (bit *all-bit-descriptors*)
      (when (eq ,attribute (bit-descriptor-attribute bit))
	(let ((fun (bit-descriptor-function bit))
	      (vec (bit-descriptor-vector bit))
	      (mask (bit-descriptor-mask bit)))
	  (declare (type (simple-array (mod 256)) vec)
		   (fixnum mask))
	  (dolist (char ,vals)
	    (setf (aref vec (car char))
		  (if (funcall fun (cdr char))
		      (logior mask (aref vec (car char)))
		      (logandc1 mask (aref vec (car char)))))))))
    ;; Invoke the attribute-hook.
    (dolist (hook ,hooks)
      (dolist (char ,vals)
	(funcall hook ,attribute (code-char (car char)) (cdr char))))
    ;; Fix up the value vector.
    (dolist (char ,vals)
      (rotatef (aref ,value (car char)) (cdr char)))))
); eval-when (compile eval)


;;; SWAP-CHAR-ATTRIBUTES  --  Internal
;;;
;;;    Swap the current values of character attributes and the ones
;;;specified by "mode".  This is used in Set-Major-Mode.
;;;
(defun swap-char-attributes (mode)
  (dolist (attribute (mode-object-character-attributes mode))
    (let* ((obj (car attribute))
	   (sym (attribute-descriptor-keyword obj))
	   (value (attribute-descriptor-vector obj))
	   (hooks (attribute-descriptor-hooks obj)))
      (declare (simple-array value))
      (swap-one-attribute sym value (cdr attribute) hooks))))



371
(declaim (special *mode-names* *current-buffer*))
ram's avatar
ram committed
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

;;; SHADOW-ATTRIBUTE  --  Public
;;;
;;;    Stick mode character attribute information in the mode object.
;;;
(defun shadow-attribute (attribute character value mode)
  "Make a mode specific character attribute value.  The value of
  Attribute for Character when we are in Mode will be Value."
  (let ((desc (gethash attribute *character-attributes*))
	(obj (getstring mode *mode-names*)))
    (unless desc
      (error "~S is not a defined Character Attribute." attribute))
    (unless obj (error "~S is not a defined Mode." mode))
    (let* ((current (assq desc (mode-object-character-attributes obj)))
	   (code (syntax-char-code character))
	   (hooks (attribute-descriptor-hooks desc))
	   (vec (attribute-descriptor-vector desc))
	   (cons (cons code value)))
      (declare (simple-array vec))
      (if current
	  (let ((old (assq code (cdr current))))
	    (if old
		(setf (cdr old) value  cons old)
		(push cons (cdr current))))
	  (push (list desc cons)
		(mode-object-character-attributes obj)))
      (when (memq obj (buffer-mode-objects *current-buffer*))
	(let ((vals (list cons)))
	  (swap-one-attribute attribute vec vals hooks)))
      (invoke-hook ed::shadow-attribute-hook attribute character value mode)))
  attribute)

;;; UNSHADOW-ATTRIBUTE  --  Public
;;;
;;;    Nuke a mode character attribute.
;;;
(defun unshadow-attribute (attribute character mode)
  "Make the value of Attribte for Character no longer shadowed in Mode."
  (let ((desc (gethash attribute *character-attributes*))
	(obj (getstring mode *mode-names*)))
    (unless desc
      (error "~S is not a defined Character Attribute." attribute))
    (unless obj
      (error "~S is not a defined Mode." mode))
    (invoke-hook ed::shadow-attribute-hook mode attribute character)
    (let* ((value (attribute-descriptor-vector desc))
	   (hooks (attribute-descriptor-hooks desc))
	   (current (assq desc (mode-object-character-attributes obj)))
	   (char (assq (syntax-char-code character) (cdr current))))
      (declare (simple-array value))
      (unless char
	(error "Character Attribute ~S is not defined for character ~S ~
	       in Mode ~S." attribute character mode))
      (when (memq obj (buffer-mode-objects *current-buffer*))
	(let ((vals (list char)))
	  (swap-one-attribute attribute value vals hooks)))
      (setf (cdr current) (delete char (the list (cdr current))))))
  attribute)


;;; NOT-ZEROP, the default test function for find-attribute etc.
;;;
(defun not-zerop (n)
  (not (zerop n)))

;;; find-attribute  --  Public
;;;
;;;    Do hairy cache lookup to find a find-character-with-attribute style
;;; vector that we can use to do the search.
;;;
(eval-when (compile eval)
(defmacro normal-find-attribute (line start result vector mask)
  `(let ((chars (line-chars ,line)))
     (setq ,result (%sp-find-character-with-attribute
		   chars ,start (strlen chars) ,vector ,mask))))
;;;
(defmacro cache-find-attribute (start result vector mask)
  `(let ((gap (- right-open-pos left-open-pos)))
     (declare (fixnum gap))
     (cond
      ((>= ,start left-open-pos)
       (setq ,result
	     (%sp-find-character-with-attribute
	      open-chars (+ ,start gap) line-cache-length ,vector ,mask))
       (when ,result (decf ,result gap)))
      ((setq ,result (%sp-find-character-with-attribute
		      open-chars ,start left-open-pos ,vector ,mask)))
      (t
       (setq ,result
	     (%sp-find-character-with-attribute
	      open-chars right-open-pos line-cache-length ,vector ,mask))
       (when ,result (decf ,result gap))))))
); eval-when (compile eval)
;;;
(defun find-attribute (mark attribute &optional (test #'not-zerop))
  "Find the next character whose attribute value satisfies test."
  (let ((charpos (mark-charpos mark))
469 470 471
	(line (mark-line mark))
	(mask 0)
	vector end-wins)
ram's avatar
ram committed
472 473
    (declare (type (or (simple-array (mod 256)) null) vector) (fixnum mask)
	     (type (or fixnum null) charpos))
ram's avatar
ram committed
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
    (cached-attribute-lookup attribute test vector mask end-wins)
    (cond
     ((cond
       ((eq line open-line)
	(when (cache-find-attribute charpos charpos vector mask)
	  (setf (mark-charpos mark) charpos) mark))
       (t
	(when (normal-find-attribute line charpos charpos vector mask)
	  (setf (mark-charpos mark) charpos) mark))))
     ;; Newlines win and there is one.
     ((and (not (zerop (logand mask (aref vector (char-code #\newline)))))
	   (line-next line))
      (move-to-position mark (line-length line) line))
     ;; We can ignore newlines.
     (t
      (do (prev)
	  (())
	(setq prev line  line (line-next line))
	(cond
	 ((null line)
	  (if end-wins
	      (return (line-end mark prev))
	      (return nil)))
	 ((eq line open-line)
	  (when (cache-find-attribute 0 charpos vector mask)
	    (return (move-to-position mark charpos line))))
	 (t
	  (when (normal-find-attribute line 0 charpos vector mask)
	    (return (move-to-position mark charpos line))))))))))


;;; REVERSE-FIND-ATTRIBUTE  --  Public
;;;
;;;    Line find-attribute, only goes backwards.
;;;
(eval-when (compile eval)
(defmacro rev-normal-find-attribute (line start result vector mask)
  `(let ((chars (line-chars ,line)))
     (setq ,result (%sp-reverse-find-character-with-attribute
		    chars 0 ,(or start '(strlen chars)) ,vector ,mask))))
;;;
(defmacro rev-cache-find-attribute (start result vector mask)
  `(let ((gap (- right-open-pos left-open-pos)))
     (declare (fixnum gap))
     (cond
      ,@(when start
	  `(((<= ,start left-open-pos)
	     (setq ,result
		   (%sp-reverse-find-character-with-attribute
		    open-chars 0 ,start ,vector ,mask)))))
      ((setq ,result (%sp-reverse-find-character-with-attribute
		      open-chars right-open-pos
		      ,(if start `(+ ,start gap) 'line-cache-length)
		      ,vector ,mask))
       (decf ,result gap))
      (t
       (setq ,result
	     (%sp-reverse-find-character-with-attribute
	      open-chars 0 left-open-pos ,vector ,mask))))))

); eval-when (compile eval)
;;;
(defun reverse-find-attribute (mark attribute &optional (test #'not-zerop))
  "Find the previous character whose attribute value satisfies test."
  (let* ((charpos (mark-charpos mark))
	 (line (mark-line mark)) vector mask end-wins)
540
    (declare (type (or (simple-array (mod 256)) null) vector)
ram's avatar
ram committed
541
	     (type (or fixnum null) charpos))
ram's avatar
ram committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    (cached-attribute-lookup attribute test vector mask end-wins)
    (cond 
     ((cond
       ((eq line open-line)
	(when (rev-cache-find-attribute charpos charpos vector mask)
	  (setf (mark-charpos mark) (1+ charpos)) mark))
       (t
	(when (rev-normal-find-attribute line charpos charpos vector mask)
	  (setf (mark-charpos mark) (1+ charpos)) mark))))
     ;; Newlines win and there is one.
     ((and (line-previous line)
	   (not (zerop (logand mask (aref vector (char-code #\newline))))))
      (move-to-position mark 0 line))
     (t
      (do (next)
	  (())
	(setq next line  line (line-previous line))
	(cond
	 ((null line)
	  (if end-wins
	      (return (line-start mark next))
	      (return nil)))
	 ((eq line open-line)
	  (when (rev-cache-find-attribute nil charpos vector mask)
	    (return (move-to-position mark (1+ charpos) line))))
	 (t
	  (when (rev-normal-find-attribute line nil charpos vector mask)
	    (return (move-to-position mark (1+ charpos) line))))))))))