                        line
                        linesbefore
                        linesafter)]
-          (if (:firstmatch settings)
-            [result]
-            (concat [result] (search-multiline-string-for-pattern s m
-              endmatchindex startlineindices endlineindices settings)))))
+          (if
+            (and
+              (or
+                (= (:linesbefore settings) 0)
+                (linesbefore-match linesbefore settings))
+              (or
+                (= (:linesafter settings) 0)
+                (linesafter-match linesafter settings)))
+            (if (:firstmatch settings)
+              [result]
+              (concat [result] (search-multiline-string-for-pattern s m
+                endmatchindex startlineindices endlineindices settings)))
+            [])))
       [])))
 
 (defn search-multiline-string [s settings]

        search-results (search-binary-string contents settings)
        with-file-results (map #(assoc-in % [:file] f) search-results)]
    (doseq [r with-file-results] (save-search-result r))))

(defn get-newline-indices [s]
  (map first 

        search-results (search-multiline-string contents settings)
        with-file-results (map #(assoc-in % [:file] f) search-results)]
    (doseq [r with-file-results] (save-search-result r))))

(defn search-line-for-pattern
  ([linenum line linesbefore linesafter p settings]
