@@ -2127,39 +2127,45 @@ the cached value will be updated automatically."
2127
2127
(defun clojure--find-ns-in-direction (direction )
2128
2128
" Return the nearest namespace in a specific DIRECTION.
2129
2129
DIRECTION is `forward' or `backward' ."
2130
- (ignore-errors
2131
- (let ((candidate)
2132
- (fn (if (eq direction 'forward )
2133
- #'search-forward-regexp
2134
- #'search-backward-regexp )))
2135
- (while (and (not candidate)
2136
- (funcall fn clojure-namespace-regexp nil t ))
2137
- (let ((end (match-end 0 )))
2138
- (save-excursion
2139
- (save-match-data
2140
- (goto-char end)
2141
- (clojure-forward-logical-sexp)
2142
- (unless (or (clojure--in-string-p) (clojure--in-comment-p))
2143
- (setq candidate (string-remove-prefix " '" (thing-at-point 'symbol ))))))))
2144
- candidate)))
2145
-
2146
- (defun clojure-find-ns ()
2147
- " Return the namespace of the current Clojure buffer.
2130
+ (let ((candidate)
2131
+ (fn (if (eq direction 'forward )
2132
+ #'search-forward-regexp
2133
+ #'search-backward-regexp )))
2134
+ (while (and (not candidate)
2135
+ (funcall fn clojure-namespace-regexp nil t ))
2136
+ (let ((end (match-end 0 )))
2137
+ (save-excursion
2138
+ (save-match-data
2139
+ (goto-char end)
2140
+ (clojure-forward-logical-sexp)
2141
+ (unless (or (clojure--in-string-p) (clojure--in-comment-p))
2142
+ (setq candidate (string-remove-prefix " '" (thing-at-point 'symbol ))))))))
2143
+ candidate))
2144
+
2145
+ (defun clojure-find-ns (&optional favor-nil )
2146
+ " Return the namespace of the current Clojure buffer, honor `FAVOR-NIL' .
2148
2147
Return the namespace closest to point and above it. If there are
2149
2148
no namespaces above point, return the first one in the buffer.
2150
2149
2150
+ If `FAVOR-NIL' is t, errors during ns form parsing will be swallowed,
2151
+ and nil will be returned instead of letting this function fail.
2152
+
2151
2153
The results will be cached if `clojure-cache-ns' is set to t."
2152
2154
(if (and clojure-cache-ns clojure-cached-ns)
2153
2155
clojure-cached-ns
2154
- (let ((ns (save-excursion
2155
- (save-restriction
2156
- (widen )
2157
-
2158
- ; ; Move to top-level to avoid searching from inside ns
2159
- (ignore-errors (while t (up-list nil t t )))
2160
-
2161
- (or (clojure--find-ns-in-direction 'backward )
2162
- (clojure--find-ns-in-direction 'forward ))))))
2156
+ (let* ((f (lambda (direction )
2157
+ (if favor-nil
2158
+ (ignore-errors (clojure--find-ns-in-direction direction))
2159
+ (clojure--find-ns-in-direction direction))))
2160
+ ((ns (save-excursion
2161
+ (save-restriction
2162
+ (widen )
2163
+
2164
+ ; ; Move to top-level to avoid searching from inside ns
2165
+ (ignore-errors (while t (up-list nil t t )))
2166
+
2167
+ (or (funcall f 'backward )
2168
+ (funcall f 'forward )))))))
2163
2169
(setq clojure-cached-ns ns)
2164
2170
ns)))
2165
2171
0 commit comments