reddit / emacs

How to automatically enable completions for just written functions and variables?

EmilySeville7cfg · 2022-07-30 00:40
For instance I create a sample script with the following function: ``` (defun hello-world() (message "Hello world!")) ``` On the next line I put opening parenthesis `(`, type `hello-` and press `C-M-i` but `No match` message is shown. If I return back and evaluate function explicitly via `C-X C-E` autocomplete works for `hello-` string. But it is not convenient. For instance in Sublime Text when writing in Python I just need to declare some function to be able complete it's name. Is it possible to do in ELisp? I don't want to reevaluate all lisp code every time I want use any declared thing in it.
EternalMigrainity · 2022-07-30 05:53
`M-/` is your friend then, it is general purpose.
EmilySeville7cfg · 2022-07-30 09:21
But it doesn't analize code as I understood... I can make syntax error: (wrong and `M-/` will suggest `wrong` word later. I don't want such behavior. I wanna smart code analysis if it possible.
augustss · 2022-07-30 13:36
Yes! M-/ is your friend. I use it all the time.
_viz_ · 2022-07-30 13:48
That's how the completion machinery in Elisp buffers work. If you don't like it, then either rewrite it or use M-/ (and I don't understand your complaint anyway since in both cases, you will have to read the identifiers offered to you). If there's a imenu backend out there, then maybe some of this pain will be relieved but I doubt it would be performant or perfect since you either will have to remake the index alist every time the completions are requested or it could be cached in which case there's always a chance it won't be up-to-date or in some cases, the index name simply won't be good enough to offer as a completion candidate. [ I faced a lot of this when writing an imenu backend for xref. ]
Phil-Hudson · 2022-07-30 15:10
Nobody has mentioned what to me is the obvious step to take: as soon as you are happy that function `hello-world` is complete, eval the defun (`C-M-x` with point inside the body, `C-x e` with point outside the last paren). That makes `hello-world` "live" in the Lisp machine that is Emacs.
Phil-Hudson · 2022-07-30 15:14
If you are using `auto-complete-mode`, then you should make sure that `ac-source-words-in-same-mode-buffers` is added to `ac-sources`.
JDRiverRun · 2022-07-30 15:35
I use [cape to merge dabbrev and elisp completions together](https://imgur.com/IYM7ulF), after trimming keywords from elisp completion (if not already a keyword), and trimming dabbrev results ending in punctuation. Then not-yet-evaluated functions show up alongside others. Not the same as static analysis, but usually good enough, until I M-x eval-buffer or emacs-lisp-byte-compile-and-load. Cape is very good at tweaking/merging CAPFs.
github-alphapapa · 2022-07-30 16:42
Even more powerful when rebound to `hippie-expand`, which even works on filenames and paths.
SlowValue · 2022-07-30 20:15
Maybe getting the differences between Elisp and Python can enlighten you, why it works as it works: When you program Python (or C or C++ or Java and many other languages), your program is the only thing in memory (virtual memory space, which is separated from all other programs running on your operating system). You can load and compile your program and the compiler|interpreter only has to load those dependencies, you specified in your source files. That's it. While on Elisp (or Common Lisp) you operate on a living instance (an *image* with other programs|packages in it) of your Lisp environment. This *image* gets initialized before you inject your own functions. On Emacs this *image* consists of an pre built basic image, which is created, when Emacs got compiled and all the stuff in your init.el. So, initializing that *image* is much more costlier (in regards to CPU time and memory) than invoking a Python or C program. Elisp and Common Lisp also feature something, which other programming languages don't: it is possible to redefine functions and structures within a running program|*image*. Those features enables those Lisps to drop the compile-**wait**-restart working pattern, which is considered a good thing by many developers. That is the reason you should do `C-M-x` (and friends) after you finished defining parts of your program, to transfer them into the live lisp *image*. It is thinkable that programing in Elisp, operates as you know it from python, but you would probably need a separate running Emacs instance. I don't think someone already implemented that workflow, because imho it is mostly inferior.
kundry_wag · 2022-07-31 04:42
Cool. Do you mind sharing your setup for this?