Changeset 7355


Ignore:
Timestamp:
Oct 5, 2007, 4:06:33 PM (12 years ago)
Author:
gz
Message:

Account for grep's line numbers being 1-based.

Add Hemlock variable "Grep Search Comments" to allow ignoring comments, but for now the only comments that are recognized are whole-line semi-colon comments. Pathetic but more useful than nothing at all...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/cocoa-ide/cocoa-grep.lisp

    r7124 r7355  
    4646      (hi::buffer-end point))))
    4747
    48 (defun request-edit-grep-line (line)
     48(defun parse-grep-line (line)
    4949  (let* ((pos1 (position #\: line))
    5050         (pos2 (and pos1 (position #\: line :start (1+ pos1))))
     
    5454         (file (and num (subseq line 0 pos1))))
    5555    (when file
     56      (values file (1- num)))))
     57 
     58(defun request-edit-grep-line (line)
     59  (multiple-value-bind (file line-num) (parse-grep-line line)
     60    (when file
    5661      (let* ((request (make-instance 'cocoa-edit-grep-line-request
    5762                                     :with-file (assign-id-map-id *edit-definition-id-map* file)
    58                                      :line num)))
     63                                     :line line-num)))
    5964        (#/performSelectorOnMainThread:withObject:waitUntilDone:
    6065         (#/sharedDocumentController ns:ns-document-controller)
     
    6267         request
    6368         t)))))
     69
     70(defun grep-comment-line-p (line)
     71  (multiple-value-bind (file line-num) (parse-grep-line line)
     72    (with-open-file (stream file)
     73      (loop while (> line-num 0)
     74        for ch = (read-char stream nil nil)
     75        when (null ch) do (return nil)
     76        do (when (member ch '(#\Return #\Linefeed))
     77             (decf line-num)
     78             (when (and (eql ch #\Return)
     79                        (eql (peek-char nil stream nil nil) #\Linefeed))
     80               (read-char stream))))
     81      (when (eql line-num 0)
     82        (loop as ch = (read-char stream nil nil)
     83          while (and ch (whitespacep ch) (not (member ch '(#\Return #\Linefeed))))
     84          finally (return (eql ch #\;)))))))
     85
     86(defun grep-remove-comment-lines (lines)
     87  (remove-if #'grep-comment-line-p lines))
    6488
    6589(defun split-grep-lines (output)
     
    6993    when (< start pos) collect (subseq output start pos)
    7094    while (< pos end)))
     95
    7196
    7297(defun grep (pattern directory &key ignore-case (include "*.lisp") (exclude "*~.lisp"))
     
    87112        (let ((output (get-output-stream-string stream)))
    88113          (if (and (eq :exited status) (or (= exit-code 0) (= exit-code 2)))
    89               (make-instance 'sequence-window-controller
    90                              :sequence (split-grep-lines output)
    91                              :result-callback #'request-edit-grep-line
    92                              :display #'princ
    93                              :title (format nil "~a in ~a" pattern directory))
     114              (let ((lines (split-grep-lines output)))
     115                (unless (hi:value hemlock::grep-search-comments)
     116                  (setq lines (grep-remove-comment-lines lines)))
     117                (make-instance 'sequence-window-controller
     118                               :sequence lines
     119                               :result-callback #'request-edit-grep-line
     120                               :display #'princ
     121                               :title (format nil "~a in ~a" pattern directory)))
    94122              (hi:editor-error "Error in grep status ~s code ~s: ~a" status exit-code output)))))))
    95123
     
    98126  "The directory searched by \"Grep\".  NIL means to use the directory of the buffer."
    99127  :value nil)
     128
     129(hi:defhvar "Grep Search Comments"
     130  "If true (the default) grep will find results anywhere.  NIL means to ignore results
     131   within comments.  For now only recognizes as comments lines which start with semi-colon."
     132  :value t)
    100133
    101134(hi:defcommand "Grep" (p)
Note: See TracChangeset for help on using the changeset viewer.