Skip to content

Commit

Permalink
Support find-func to find lisp symbols defined in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
declantsien committed Apr 11, 2024
1 parent c939fca commit 8ed558c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
28 changes: 24 additions & 4 deletions crates/lisp-doc/docfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ type AddGlobalFn = ::std::option::Option<
unsafe extern "C" fn(c_int, *const c_char, c_int, *const c_char) -> *const (),
>;

#[macro_export]
macro_rules! printf {
($str:expr) => {{
let c_str = CString::new($str).unwrap();
let str_ptr = c_str.as_ptr() as *const ::libc::c_char;
unsafe {
libc::printf(str_ptr);
}
}};
($fmtstr:expr, $($arg:expr),*) => {{
let formatted = format!($fmtstr, $($arg),*);
let c_str = CString::new(formatted).unwrap();
let str_ptr = c_str.as_ptr() as *const ::libc::c_char;
unsafe {
libc::printf(str_ptr);
}
}};
}

/// This function is called by lib-src/scan_file and runs with make-docfile
/// in src/Makefile.
/// We have to ensure that all necessary rust paths will be considered to
Expand Down Expand Up @@ -218,9 +237,11 @@ pub fn scan_rust_file1(
docstring_usage.push(')');
}
// Print contents for docfile to stdout
print!(
printf!(
"\x1fF{}\n{}\n(fn{}",
attr_props.name, docstring, docstring_usage
attr_props.name,
docstring,
docstring_usage
);
}
} else if line.starts_with("def_lisp_sym!(") {
Expand Down Expand Up @@ -263,11 +284,10 @@ pub fn scan_rust_file1(
}
} else {
let lisp_name = &caps[3];
print!("\x1fV{}\n{}", lisp_name, docstring)
printf!("\x1fV{}\n{}", lisp_name, docstring)
}
}
}
}
stdout().flush()?;
Ok(())
}
27 changes: 17 additions & 10 deletions lib-src/make-docfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct global *add_global (int type, char const *name, int value, char const *sv

typedef struct global * (*add_global_fn) (int, char const *, int, char const *);

/* Implemented in remacs_lib. */
/* Implemented in Rust crate lisp-doc. */
void scan_rust_file (char *filename, int generate_globals, add_global_fn add_global);

#include <unistd.h>
Expand Down Expand Up @@ -220,17 +220,26 @@ main (int argc, char **argv)
return EXIT_SUCCESS;
}

static bool
is_rust_file (char *filename)
{
ptrdiff_t len = strlen (filename);
return (len > 3 && !strcmp (filename + len - 3, ".rs"));
}

/* Add a source file name boundary marker in the output file. */
static void
put_filename (char *filename)
{
char *tmp;
if (!is_rust_file (filename)) {
char *tmp;

for (tmp = filename; *tmp; tmp++)
{
if (IS_DIRECTORY_SEP (*tmp))
filename = tmp + 1;
}
for (tmp = filename; *tmp; tmp++)
{
if (IS_DIRECTORY_SEP (*tmp))
filename = tmp + 1;
}
}

printf ("\037S%s\n", filename);
}
Expand All @@ -241,11 +250,9 @@ put_filename (char *filename)
static void
scan_file (char *filename)
{
ptrdiff_t len = strlen (filename);

if (!generate_globals)
put_filename (filename);
if (len > 3 && !strcmp (filename + len - 3, ".rs"))
if (is_rust_file (filename))
scan_rust_file (filename, generate_globals, add_global);
else
scan_c_file (filename, "r");
Expand Down
18 changes: 11 additions & 7 deletions lisp/emacs-lisp/find-func.el
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,16 @@ TYPE should be nil to find a function, or `defvar' to find a variable."
(goto-char (point-min))
(unless (re-search-forward
(if type
(concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
(regexp-quote (symbol-name fun-or-var))
"\"")
(concat "DEFUN[ \t\n]*([ \t\n]*\""
(regexp-quote (subr-name (advice--cd*r fun-or-var)))
"\""))
(if (string-equal (file-name-extension file) "rs")
(regexp-quote (replace-regexp-in-string "-" "_" (symbol-name fun-or-var)))
(concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
(regexp-quote (symbol-name fun-or-var))
"\""))
(if (string-equal (file-name-extension file) "rs")
(regexp-quote (replace-regexp-in-string "-" "_" (subr-name (advice--cd*r fun-or-var))))
(concat "DEFUN[ \t\n]*([ \t\n]*\""
(regexp-quote (subr-name (advice--cd*r fun-or-var)))
"\"")))
nil t)
(error "Can't find source for %s" fun-or-var))
(cons (current-buffer) (match-beginning 0))))
Expand Down Expand Up @@ -406,7 +410,7 @@ The search is done in the source for library LIBRARY."
;; that defines something else.
(while (and (symbolp symbol) (get symbol 'definition-name))
(setq symbol (get symbol 'definition-name)))
(if (string-match "\\`src/\\(.*\\.\\(c\\|m\\)\\)\\'" library)
(if (string-match "\\`src/\\(.*\\.\\(c\\|m\\|rs\\)\\)\\'" library)
(find-function-C-source symbol (match-string 1 library) type)
(when (string-match "\\.el\\(c\\)\\'" library)
(setq library (substring library 0 (match-beginning 1))))
Expand Down
6 changes: 4 additions & 2 deletions lisp/help-fns.el
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ If we can't find the file name, nil is returned."
(throw 'loop nil)
(re-search-backward "\^_S\\(.*\\)")
(let ((file (match-string 1)))
(if (member file build-files)
(if (or (member file build-files)
(string-equal (file-name-extension file) "rs"))
(throw 'loop file)
(goto-char pnt)))))))))
(if (not file)
Expand All @@ -345,7 +346,8 @@ If we can't find the file name, nil is returned."
(setq file (replace-match ".m" t t file 1))
(if (string-match "\\.\\(o\\|obj\\)\\'" file)
(setq file (replace-match ".c" t t file))))
(if (string-match "\\.\\(c\\|m\\)\\'" file)
(if (or (string-match "\\.\\(c\\|m\\)\\'" file)
(string-equal (file-name-extension file) "rs"))
(concat "src/" file)
file))))))

Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,11 @@ $(libsrc)/make-docfile$(EXEEXT) $(libsrc)/make-fingerprint$(EXEEXT): \

buildobj.h: Makefile
$(AM_V_GEN)for i in $(ALLOBJS); do \
echo "$$i" | sed 's,.*/,,; s/\.obj$$/\.o/; s/^/"/; s/$$/",/' \
echo "$$i" | sed 's,.*/,,; s/\.obj$$/\.o/; s/^/"/; s/$$/",/'\
|| exit; \
done >$@.tmp
$(AM_V_GEN)for i in $(RUST_CRATES_SOURCES); do \
echo "\"$$i\"," >> $@.tmp;done; \
$(AM_V_at)mv $@.tmp $@

GLOBAL_SOURCES = $(base_obj:.o=.c) $(NS_OBJC_OBJ:.o=.m) $(RUST_CRATES_SOURCES)
Expand Down
2 changes: 1 addition & 1 deletion src/pdumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3740,7 +3740,7 @@ dump_check_dump_off (struct dump_context *ctx, dump_off dump_offset)
static void
dump_check_emacs_off (dump_off emacs_off)
{
eassert (labs (emacs_off) <= 60 * 1024 * 1024);
eassert (labs (emacs_off) <= 65 * 1024 * 1024);
}

static struct dump_reloc
Expand Down

0 comments on commit 8ed558c

Please sign in to comment.