elsmith 0.1.1

An Emacs Lisp language server with Lean-verified protocol and editing rules
Documentation
;;; extract.el --- Emacs environment catalog for elsmith  -*- lexical-binding: t -*-

(defun elsmith--null (value)
  (or value :null))

(defun elsmith--text (value)
  (when (and (stringp value) (not (multibyte-string-p value)))
    (setq value (decode-coding-string value 'utf-8)))
  (if (and (stringp value) (string-match-p "[\x3fff80-\x3fffff]" value))
      (concat (seq-remove (lambda (char) (>= char #x3fff80)) value))
    value))

(defun elsmith--lazy-doc (ref buffers)
  (when (and (consp ref) (stringp (car ref)) (integerp (cdr ref)))
    (let* ((file (expand-file-name (car ref) lisp-directory))
           (buffer (or (gethash file buffers)
                       (when (file-readable-p file)
                         (let ((new (generate-new-buffer " *elsmith-doc*" t)))
                           (with-current-buffer new
                             (set-buffer-multibyte nil)
                             (insert-file-contents-literally file))
                           (puthash file new buffers))))))
      (when buffer
        (with-current-buffer buffer
          (let* ((start (1+ (abs (cdr ref))))
                 (end (and (<= start (point-max))
                           (save-excursion
                             (goto-char start)
                             (search-forward "\037" nil t)))))
            (when end
              (decode-coding-string
               (replace-regexp-in-string
                "\001\\([01_]\\)"
                (lambda (match)
                  (pcase (aref match 1) (?1 "\001") (?0 "\000") (_ "\037")))
                (buffer-substring-no-properties start (1- end)) t t)
               'utf-8))))))))

(defun elsmith--usage (symbol doc autoload)
  (let ((split (and (stringp doc) (ignore-errors (help-split-fundoc doc symbol)))))
    (if (or (car split) autoload)
        (cons (car split) (if split (cdr split) doc))
      (let ((arglist (ignore-errors (help-function-arglist symbol t))))
        (cons (and (listp arglist)
                   (format "%S" (help--make-usage symbol arglist)))
              doc)))))

(let ((functions nil)
      (variables nil)
      (subrs nil)
      (c-variables nil)
      (deferred nil)
      (defuns (make-hash-table :test #'eq))
      (defvars (make-hash-table :test #'eq))
      (buffers (make-hash-table :test #'equal))
      (before features))
  (dolist (entry load-history)
    (let ((file (car entry)))
      (when (stringp file)
        (dolist (item (cdr entry))
          (cond ((symbolp item) (unless (gethash item defvars) (puthash item file defvars)))
                ((memq (car-safe item) '(defun t))
                 (let ((name (if (eq (car item) t) (cdr-safe item) (cdr item))))
                   (when (and (symbolp name) (not (gethash name defuns)))
                     (puthash name file defuns)))))))))
  (dolist (symbol (let (symbols) (mapatoms (lambda (symbol) (push symbol symbols))) symbols))
     (when (fboundp symbol)
       (let* ((raw (symbol-function symbol))
              (def (ignore-errors (indirect-function symbol)))
              (autoload (autoloadp def))
              (kind (cond ((special-form-p def) "special-form")
                          (autoload (if (memq (nth 4 def) '(macro t)) "macro" "function"))
                          ((macrop def) "macro")
                          ((subr-primitive-p def) "subr")
                          (t "function")))
              (property (get symbol 'function-documentation))
              (doc (elsmith--text
                    (cond (autoload
                           (let ((ref (nth 2 def)))
                             (if (stringp ref) ref (elsmith--lazy-doc ref buffers))))
                          ((stringp property) property)
                          ((and (consp property) (stringp (car property)))
                           (elsmith--lazy-doc property buffers))
                          (property nil)
                          (t (ignore-errors (documentation symbol t))))))
              (computed (and property (not doc) (not autoload)))
              (usage (if computed (list nil) (elsmith--usage symbol doc autoload)))
              (entry (list :name (symbol-name symbol)
                           :kind kind
                           :usage (elsmith--null (car usage))
                           :doc (elsmith--null (and (stringp (cdr usage)) (cdr usage)))
                           :file (elsmith--null
                                  (cond (autoload (nth 1 def))
                                        ((subr-primitive-p def) nil)
                                        (t (gethash symbol defuns))))
                           :autoload (if autoload t :false)
                           :alias (elsmith--null (and (symbolp raw) raw (symbol-name raw))))))
         (when (subr-primitive-p def)
           (push (cons entry def) subrs))
         (when computed
           (push (cons entry symbol) deferred))
         (push entry functions)))
     (when (or (boundp symbol) (get symbol 'variable-documentation))
       (let* ((doc (elsmith--text
                    (ignore-errors (documentation-property symbol 'variable-documentation t))))
              (file (gethash symbol defvars))
              (entry (list :name (symbol-name symbol)
                           :special (if (special-variable-p symbol) t :false)
                           :constant (if (or (keywordp symbol) (memq symbol '(nil t))) t :false)
                           :doc (elsmith--null (and (stringp doc) doc))
                           :file (elsmith--null file))))
         (when (and doc (not file))
           (push (cons entry symbol) c-variables))
         (push entry variables))))
  (maphash (lambda (_ buffer) (kill-buffer buffer)) buffers)
  (let ((loaded (butlast features (length before))))
    (require 'help-fns)
    (dolist (subr subrs)
      (plist-put (car subr) :file
                 (elsmith--null (ignore-errors (help-C-file-name (cdr subr) 'subr)))))
    (dolist (function deferred)
      (let ((usage (elsmith--usage (cdr function)
                                   (elsmith--text (ignore-errors (documentation (cdr function) t)))
                                   nil)))
        (plist-put (car function) :usage (elsmith--null (car usage)))
        (plist-put (car function) :doc (elsmith--null (and (stringp (cdr usage)) (cdr usage))))))
    (dolist (variable c-variables)
      (plist-put (car variable) :file
                 (elsmith--null (ignore-errors (help-C-file-name (cdr variable) 'var)))))
    (let ((coding-system-for-write 'no-conversion))
      (write-region
       (json-serialize
        (list :schema 2
              :version emacs-version
              :lispDirectory lisp-directory
              :dataDirectory data-directory
              :sourceDirectory (elsmith--null source-directory)
              :loadPath (vconcat (seq-filter #'stringp load-path))
              :features (vconcat (mapcar #'symbol-name before))
              :loadedDuringExtraction (vconcat (mapcar #'symbol-name loaded))
              :functions (vconcat functions)
              :variables (vconcat variables)))
       nil (expand-file-name "catalog.json") nil 'silent))))