   (factor-listener-mode))
 
 (defun factor-refresh-all ()
+  "Reload source files and documentation for all loaded
+vocabularies which have been modified on disk."
   (interactive)
   (comint-send-string "*factor*" "refresh-all\n"))
 


;;; Factor listener mode

(define-derived-mode factor-listener-mode comint-mode "Factor Listener")

(define-key factor-listener-mode-map [f8] 'factor-refresh-all)

(defun run-factor ()
  (interactive)
  (switch-to-buffer
   (make-comint-in-buffer "factor" nil (expand-file-name factor-binary) nil

            (setq iw (current-indentation))))))
    iw))

(defsubst factor--ppss-brackets-depth ()
  (nth 0 (syntax-ppss)))

(defsubst factor--ppss-brackets-start ()
  (nth 1 (syntax-ppss)))

(defsubst factor--line-indent (pos)
  (save-excursion (goto-char pos) (current-indentation)))

(defconst factor--regex-closing-paren "[])}]")
(defsubst factor--at-closing-paren-p ()
  (looking-at factor--regex-closing-paren))

(defsubst factor--at-first-char-p ()
  (= (- (point) (line-beginning-position)) (current-indentation)))

(defconst factor--regex-single-liner
  (format "^%s" (regexp-opt '("USE:" "IN:" "PRIVATE>" "<PRIVATE"))))

(defun factor--at-end-of-def ()
  (or (looking-at ".*;[ \t]*$")
      (looking-at factor--regex-single-liner)))

(defun factor--indent-in-brackets ()
  (save-excursion
    (beginning-of-line)
    (when (or (and (re-search-forward factor--regex-closing-paren
                                      (line-end-position) t)
                   (not (backward-char)))
               (> (factor--ppss-brackets-depth) 0))
      (let ((op (factor--ppss-brackets-start)))
        (when (> (line-number-at-pos) (line-number-at-pos op))
          (if (factor--at-closing-paren-p)
              (factor--line-indent op)
            (+ (factor--line-indent op) factor-indent-width)))))))

(defun factor--indent-definition ()
  (save-excursion
    (beginning-of-line)
    (when (looking-at "\\([^ ]\\|^\\)+:") 0)))

(defun factor--indent-continuation ()
  (save-excursion
    (forward-line -1)
    (beginning-of-line)
    (if (bobp) 0
      (if (looking-at "^[ \t]*$")
          (factor--indent-continuation)
        (if (factor--at-end-of-def)
            (- (current-indentation) factor-indent-width)
          (if (factor--indent-definition)
              (+ (current-indentation) factor-indent-width)
            (current-indentation)))))))

(defun factor--calculate-indentation ()
  "Calculate Factor indentation for line at point."
  (or (and (bobp) 0)
      (factor--indent-definition)
      (factor--indent-in-brackets)
      (factor--indent-continuation)
      0))

(defun factor-indent-line ()
  "Indent current line as Factor code"
