Skip to content

Commit

Permalink
[imp] Try build dynamic module if not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
cireu committed Aug 4, 2019
1 parent 04e8c51 commit 31695d4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
21 changes: 2 additions & 19 deletions README.org
Expand Up @@ -19,25 +19,8 @@ they are powered by Rust so it's fast enough.
1. Setup Rust compile toolchain, you can check [[https://rustup.rs/][here]] for some information.
2. And you also need [[https://llvm.org/][LLVM]].
3. Clone the repository and put it under your Emacs's =load-path=
4. Compile the module, using

#+BEGIN_SRC sh
cargo build --release
ln -sv target/release/libfuz_core.so fuz-core.so
#+END_SRC

For MacOS users, dynamic library is suffixed with =.dylib=, you may need to
change the command to

#+BEGIN_SRC sh
ln -sv target/release/libfuz_core.dylib fuz-core.so
#+END_SRC

Windows users need to run this command in CMD with admin access.

#+BEGIN_SRC batch
mklink fuz-core.dll target/release/fuz_core.dll
#+END_SRC
4. Load =fuz.el=. =fuz= will try to compile the dynamic module when failed to
load one.

* Usage

Expand Down
58 changes: 45 additions & 13 deletions fuz.el
Expand Up @@ -39,8 +39,33 @@

;;; Code:

(require 'fuz-core)
(require 'fuz-extra)
(defun fuz-build-and-load-dymod! ()
"Build and load dyamic module."
(unless (executable-find "cargo")
(error "Rust package manager \"cargo\" not found!"))
(let* ((default-directory (file-name-directory (locate-library "fuz")))
(dll-name (cl-case system-type
((windows-nt ms-dos cygwin) "fuz_core.dll")
(darwin "libfuz_core.dylib")
(t "libfuz_core.so")))
(target-name (cl-case system-type
((windows-nt ms-dos cygwin) "fuz-core.dll")
(t "fuz-core.so")))
(dll-path (expand-file-name (format "target/release/%s" dll-name)))
(target-path (expand-file-name target-name))
(buf (generate-new-buffer "*fuz compilation*")))
(pop-to-buffer buf)
(let ((errno (shell-command "cargo build --release" buf)))
(if (= errno 0)
(progn
(make-symbolic-link dll-path target-path)
(load target-path nil t)
(message "Successfully build dynamic module."))
(error "Failed to compile dynamic modules, check buffer \"%s\" for detailed information."
(buffer-name buf))))))

(unless (require 'fuz-core nil t)
(fuz-build-and-load-dymod!))

(eval-when-compile
(require 'subr-x)
Expand All @@ -49,14 +74,28 @@
(unless (>= emacs-major-version 26)
(unless (fboundp 'if-let*) (defalias 'if-let* #'if-let))))

(require 'fuz-extra)

;;; Export function aliases

(declare-function fuz-core-calc-score-clangd "fuz-core")
(declare-function fuz-core-calc-score-skim "fuz-core")
(declare-function fuz-core-find-indices-clangd "fuz-core")
(declare-function fuz-core-find-indices-skim "fuz-core")

(defalias 'fuz-calc-score-clangd 'fuz-core-calc-score-clangd)
(defalias 'fuz-calc-score-skim 'fuz-core-calc-score-skim)
(defalias 'fuz-find-indices-clangd 'fuz-core-find-indices-clangd)
(defalias 'fuz-find-indices-skim 'fuz-core-find-indices-skim)

(defsubst fuz-fuzzy-match-skim (pattern str)
"Match STR against PATTERN, using skim's algorithm.
Sign: (-> Str Str (Option (Listof Long)))
Return (SCORE . (INDICES)) if matched, otherwise return `nil'."
(if-let* ((total-score (fuz-core-calc-score-skim pattern str)))
(cons total-score (fuz-core-find-indices-skim pattern str))
(if-let* ((total-score (fuz-calc-score-skim pattern str)))
(cons total-score (fuz-find-indices-skim pattern str))
nil))

(defsubst fuz-fuzzy-match-clangd (pattern str)
Expand All @@ -65,17 +104,10 @@ Return (SCORE . (INDICES)) if matched, otherwise return `nil'."
Sign: (-> Str Str (Option (Listof Long)))
Return (SCORE . (INDICES)) if matched, otherwise return `nil'."
(if-let* ((total-score (fuz-core-calc-score-clangd pattern str)))
(cons total-score (fuz-core-find-indices-clangd pattern str))
(if-let* ((total-score (fuz-calc-score-clangd pattern str)))
(cons total-score (fuz-find-indices-clangd pattern str))
nil))

;;; Export function aliases

(defalias 'fuz-calc-score-clangd 'fuz-core-calc-score-clangd)
(defalias 'fuz-calc-score-skim 'fuz-core-calc-score-skim)
(defalias 'fuz-find-indices-clangd 'fuz-core-find-indices-clangd)
(defalias 'fuz-find-indices-skim 'fuz-core-find-indices-skim)

(provide 'fuz)

;; Local Variables:
Expand Down

0 comments on commit 31695d4

Please sign in to comment.