Changeset 771


Ignore:
Timestamp:
Apr 13, 2004, 12:16:13 PM (21 years ago)
Author:
Gary Byers
Message:

Lots-o-changes: double-click, paste, etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/examples/cocoa-editor.lisp

    r764 r771  
    223223;;; number of preceding lines.
    224224(defun mark-absolute-position (mark)
    225   (let* ((pos (hi::mark-charpos mark)))
     225  (let* ((pos (hi::mark-charpos mark))
     226         (hi::*buffer-gap-context* (hi::buffer-gap-context (hi::line-%buffer
     227                                                            (hi::mark-line mark)))))
    226228    (do* ((line (hi::line-previous (hi::mark-line mark))
    227229                (hi::line-previous line)))
     
    320322  (not (eql (slot-value self 'edit-count) 0)))
    321323
     324
     325(define-objc-method (((:struct :<NSR>ange r) :double-click-at-index (:unsigned index))
     326                     hemlock-text-storage)
     327  (block HANDLED
     328    (let* ((cache (hemlock-buffer-string-cache (send self 'string)))
     329           (buffer (if cache (buffer-cache-buffer cache))))
     330      (when (and buffer (string= (hi::buffer-major-mode buffer) "Lisp"))
     331        (let* ((hi::*buffer-gap-context* (hi::buffer-gap-context buffer)))
     332          (hi::with-mark ((m1 (hi::buffer-point buffer)))
     333            (move-hemlock-mark-to-absolute-position m1 cache index)
     334            (hemlock::pre-command-parse-check m1)
     335            (when (hemlock::valid-spot m1 nil)
     336              (cond ((eql (hi::next-character m1) #\()
     337                     (hi::with-mark ((m2 m1))
     338                       (when (hemlock::list-offset m2 1)
     339                         (setf (pref r :<NSR>ange.location) index
     340                               (pref r :<NSR>ange.length)
     341                               (- (mark-absolute-position m2) index))
     342                         (return-from HANDLED nil))))
     343                    ((eql (hi::previous-character m1) #\))
     344                     (hi::with-mark ((m2 m1))
     345                       (when (hemlock::list-offset m2 -1)
     346                         (#_NSLog #@"Length = %d"
     347                                  :unsigned
     348                                  (- (1- index) (mark-absolute-position m2)))
     349                         (setf (pref r :<NSR>ange.location)
     350                               (mark-absolute-position m2)
     351                               (pref r :<NSR>ange.length)
     352                               (- (1- index) (mark-absolute-position m2)))
     353                         (return-from HANDLED nil)))))))))
     354      ;; No early exit, so call next-method
     355      (objc-message-send-super-stret r (super) "doubleClickAtIndex:"
     356                                     :unsigned index
     357                                     :void))))
     358           
     359
     360     
     361
     362
     363   
     364   
     365
    322366(defun textstorage-note-insertion-at-position (self pos n)
    323367  (send self
     
    343387          :change-in-length (- n))
    344388    (let* ((display (hemlock-buffer-string-cache (send self 'string))))
    345             (reset-buffer-cache display)
    346             (update-line-cache-for-index display pos))))
     389      (reset-buffer-cache display)
     390      (update-line-cache-for-index display pos))))
    347391
    348392(define-objc-method ((:void :note-modification params) hemlock-text-storage)
     
    411455    (svref *styles* 0)))
    412456
    413 ;;; The range's origin should probably be the buffer's point; if
    414 ;;; the range has non-zero length, we probably need to think about
    415 ;;; things harder.
    416457(define-objc-method ((:void :replace-characters-in-range (:<NSR>ange r)
    417458                            :with-string string)
    418459                     hemlock-text-storage)
    419   (declare (ignorable r string))
    420   #+debug
    421   (#_NSLog #@"replace-characters-in-range (%d %d) with-string %@"
    422            :unsigned (pref r :<NSR>ange.location)
    423            :unsigned (pref r :<NSR>ange.length)
    424            :id string))
     460    (let* ((cache (hemlock-buffer-string-cache (send self 'string)))
     461           (buffer (if cache (buffer-cache-buffer cache)))
     462           (hi::*buffer-gap-context* (hi::buffer-gap-context buffer))
     463           (location (pref r :<NSR>ange.location))
     464           (length (pref r :<NSR>ange.length))
     465           (mark (hi::buffer-%mark buffer))
     466           (point (hi::buffer-point buffer)))
     467      (cond ((> length 0)
     468             (move-hemlock-mark-to-absolute-position mark cache location)
     469             (move-hemlock-mark-to-absolute-position point cache (+ location length))
     470             (hemlock::%buffer-activate-region buffer))
     471            (t
     472             (move-hemlock-mark-to-absolute-position point cache location)))
     473      (hi::insert-string point (lisp-string-from-nsstring string))))
     474
    425475
    426476;;; I'm not sure if we want the text system to be able to change
     
    545595    (when buffer
    546596      (let* ((q (hemlock-frame-event-queue (send self 'window))))
    547         (hi::enqueue-key-event q (nsevent-to-key-event event)))))
    548   ;; Probably not the right place for this, but needs to happen
    549   ;; -somewhere-, and needs to happen in the event thread.
    550  
    551   )
     597        (hi::enqueue-key-event q (nsevent-to-key-event event))))))
    552598
    553599(defun enqueue-buffer-operation (buffer thunk)
     
    555601    (let* ((q (hemlock-frame-event-queue (send w 'window)))
    556602           (op (hi::make-buffer-operation :thunk thunk)))
    557       (hi::enqueue-key-event q op))))
     603      (hi::event-queue-insert q op))))
    558604
    559605 
    560606;;; Process a key-down NSEvent in a Hemlock text view by translating it
    561607;;; into a Hemlock key event and passing it into the Hemlock command
    562 ;;; interpreter.  The underlying buffer becomes Hemlock's current buffer
    563 ;;; and the containing pane becomes Hemlock's current window when the
    564 ;;; command is processed.  Use the frame's command state object.
     608;;; interpreter.
    565609
    566610(define-objc-method ((:void :key-down event)
     
    574618                            :still-selecting (:<BOOL> still-selecting))
    575619                     hemlock-text-view)
     620    #+debug
     621  (#_NSLog #@"Set selected range called: location = %d, length = %d, affinity = %d, still-selecting = %d"
     622           :int (pref r :<NSR>ange.location)
     623           :int (pref r :<NSR>ange.length)
     624           :<NSS>election<A>ffinity affinity
     625           :<BOOL> (if still-selecting #$YES #$NO))
    576626  (unless (send (send self 'text-storage) 'editing-in-progress)
    577627    (let* ((d (hemlock-buffer-string-cache (send self 'string)))
    578          (point (hemlock::buffer-point (buffer-cache-buffer d)))
    579          (location (pref r :<NSR>ange.location))
    580          (len (pref r :<NSR>ange.length)))
    581     (when (eql len 0)
    582       #+debug
    583       (#_NSLog #@"Moving point to absolute position %d" :int location)
    584       (move-hemlock-mark-to-absolute-position point d location))))
     628           (buffer (buffer-cache-buffer d))
     629           (hi::*buffer-gap-context* (hi::buffer-gap-context buffer))
     630           (point (hi::buffer-point buffer))
     631           (location (pref r :<NSR>ange.location))
     632           (len (pref r :<NSR>ange.length)))
     633      (cond ((eql len 0)
     634             #+debug
     635             (#_NSLog #@"Moving point to absolute position %d" :int location)
     636             (setf (hi::buffer-region-active buffer) nil)
     637             (move-hemlock-mark-to-absolute-position point d location))
     638            (t
     639             ;; We don't get much information about which end of the
     640             ;; selection the mark's at and which end point is at, so
     641             ;; we have to sort of guess.  In every case I've ever seen,
     642             ;; selection via the mouse generates a sequence of calls to
     643             ;; this method whose parameters look like:
     644             ;; a: range: {n0,0} still-selecting: false  [ rarely repeats ]
     645             ;; b: range: {n0,0) still-selecting: true   [ rarely repeats ]
     646             ;; c: range: {n1,m} still-selecting: true   [ often repeats ]
     647             ;; d: range: {n1,m} still-selecting: false  [ rarely repeats ]
     648             ;;
     649             ;; (Sadly, "affinity" doesn't tell us anything interesting.
     650             ;; We've handled a and b in the clause above; after handling
     651             ;; b, point references buffer position n0 and the
     652             ;; region is inactive.
     653             ;; Let's ignore c, and wait until the selection's stabilized.
     654             ;; Make a new mark, a copy of point (position n0).
     655             ;; At step d (here), we should have either
     656             ;; d1) n1=n0.  Mark stays at n0, point moves to n0+m.
     657             ;; d2) n1+m=n0.  Mark stays at n0, point moves to n0-m.
     658             ;; If neither d1 nor d2 apply, arbitrarily assume forward
     659             ;; selection: mark at n1, point at n1+m.
     660             ;; In all cases, activate Hemlock selection.
     661             (unless still-selecting
     662                (let* ((pointpos (mark-absolute-position point))
     663                       (selection-end (+ location len))
     664                       (mark (hi::copy-mark point :right-inserting)))
     665                   (cond ((eql pointpos location)
     666                          (move-hemlock-mark-to-absolute-position point
     667                                                                  d
     668                                                                  selection-end))
     669                         ((eql pointpos selection-end)
     670                          (move-hemlock-mark-to-absolute-position point
     671                                                                  d
     672                                                                  location))
     673                         (t
     674                          (move-hemlock-mark-to-absolute-position mark
     675                                                                  d
     676                                                                  location)
     677                          (move-hemlock-mark-to-absolute-position point
     678                                                                  d
     679                                                                  selection-end)))
     680                   (hemlock::%buffer-push-buffer-mark buffer mark t)))))))
    585681  (send-super :set-selected-range r
    586682              :affinity affinity
     
    15091605         (hi::*buffer-gap-context* (hi::buffer-gap-context buffer))
    15101606         (point (hi::buffer-point buffer))
    1511          (pos (mark-absolute-position point))
     1607         (pointpos (mark-absolute-position point))
     1608         (location pointpos)
    15121609         (len 0))
     1610    (when (hemlock::%buffer-region-active-p buffer)
     1611      (let* ((mark (hi::buffer-%mark buffer)))
     1612        (when mark
     1613          (let* ((markpos (mark-absolute-position mark)))
     1614            (if (< markpos pointpos)
     1615              (setq location markpos len (- pointpos markpos))
     1616              (if (< pointpos markpos)
     1617                (setq location pointpos len (- markpos pointpos))))))))
    15131618    #+debug
    15141619    (#_NSLog #@"update Hemlock selection: charpos = %d, abspos = %d"
     
    15181623     #'(lambda (tv)
    15191624         (send tv
    1520                :update-selection pos
     1625               :update-selection location
    15211626               :length len
    15221627               :affinity #$NSSelectionAffinityUpstream)))))
Note: See TracChangeset for help on using the changeset viewer.