I am trying to set the current line number to a variable in Elisp but keep getting a void-variable error!
The code is:
(setq x what-line)
I'd also like to set the total number of lines in the buffer to a variable as well, but get the same error?!
(setq x (line-number-at-pos)
y (line-number-at-pos (point-max)))
How to find out about this kind of thing? Try M-x find-function RET what-line RET to see the source code of what-line. Reading simple.el (the file in which what-line is defined) is a good way to get familiar with elementary Elisp programming.
(setq x (what-line))
The line-number-at-pos function mentioned in a previous answer only considers the accessible portion of the buffer. If the buffer is "narrowed" it won't count the hidden lines, so this can be rather confusing.
If you read the code for the what-line function you can see how it deals with narrowed buffers (indeed what-line works by calling line-number-at-pos).
Related
My emacs stops working when I try to evaluate a piece of lisp code in the scratch buffer (with eval-last-sexp) or the minibuffer, with the above error. I cannot recover from this, I have to kill the emacs process.
The function is:
(defun add-to-list (val list-of-numbers)
(mapcar #'(lambda (num) (+ val num))
list-of-numbers))
I have tried on a fresh spacemacs installation to exclude that there is something wrong with my configuration file but I get the same problem.
This seems like a well formed function and the function name displays in the minibuffer after evaluation, so it seems to be evaluated fine. But then the above error occurs.
Can somebody explain based on this information?
add-to-list is a Emacs build-in function. Maybe the redefinition is here the source of your problem?
What happens if you choose another function name?
I noticed that there is no
format directive which would
call force-output/finish-output.
Why?
It does seem to be useful in user interaction, cf.
Lisp format and force-output.
E.g., ~= could translate to finish-output, and ~:= to force-output.
I don't think clear-output makes much sense in this context, but we
might map ~#= to it for completeness.
PS. Cf. CLISP RFE.
Summary from comp.lang.lisp:
An explanation from Steven Haflich
The language defines no portable way to extend the set of format
directives (other then ~/.../) but that's not really the issue here.
The real problem is that it is not well defined to call finish-output or similar functions at arbitrary places during printing.
If pretty-printing is in progress, the stream received by a
pprint-dispatch or print-object method may be an encapsulating stream --
one that delays output temporarily until it can make decisions about
white space and line breaks. (There are also potential problems if
finish-output were called inside a ~< justification, but that directive
is a hairball!) What would one expect finish-output to do if called
inside a pretty print operation? I don't think it's well defined.
The problem isn't particular to format, of course, but a directive for
finish-output from format would just add another sharp edge to the
language. finish-output etc. are only safe to call when completely
outside an actual or implied call to cl:write. Call it as a function
at an appropriate point in your code (where you know execution isn't
inside a nested write) so the intention is clear and you don't mess up
printer internals.
A suggestion from Rob Warnock
Actually, no changes to format are needed. Just add this function somewhere in the COMMON-LISP-USER package:
(defun fo (stream arg colon-p atsign-p &rest params)
(declare (ignore arg params))
(cond
(colon-p (force-output stream))
(atsign-p (clear-output stream))
(t (finish-output stream))))
Then:
(progn
(format t "enter var: ~/fo/" nil)
(read))
enter var: 456
456
The problems with this (portable!) approach are
verbosity (~/fo/ instead of ~=)
need to consume a format argument (nil in the example above)
Short story: Given a position value in the buffer say, 12345, how to take the cursor to the position directly
Long story: when i debug my emacs initial boot messages, it prints an error message as,
eval-buffer(#...........................) ; Reading at buffer position 19352
The fact, no line numbers are printed & only the position value is there makes my navigation tough. any clues, to make my cursor to jump to the position 19352?
Simply
(goto-char 19352)
See documentation
To enter Lisp code interactively, the eval prompt is M-:
You want to use that using the command goto-char, which by default is bound to M-g c. So you can do
M-g c 19352 RET
or, if you forget the binding,
M-x goto-char RET 19352 RET
Is it possible to get a list of functions defined in an emacs-lisp file? I found this sort of related answer: How do I get a list of Emacs lisp non-interactive functions?, but it involves a map over all of the atoms defined, not just what is in a file.
If the file in question has already been loaded, then you can modify the code in the question you link to filter out the symbols defined in other files:
(let ((funclist ()))
(mapatoms
(lambda (x)
(when (and (fboundp x) ; does x name a function?
(let ((f (symbol-file x)))
(and f (string= (file-name-base f) "my-file.el"))))
(push x funclist))))
funclist)
If the file has not been loaded, you would have to scan it with scan-sexps and find defun forms.
You might, however, prefer to use etags or imenu instead of scanning the file yourself.
Maybe a faster way is to look for the file in load-history, which will then give you the list of variables and functions defined therein.
Not sure if your asking for a non interactive approach.
With M-x occur ENT (defun.* ENT you get a buffer with more or less all function-definitions found in (current-buffer).
The quick&dirty way: extract all defuns via regex. It works instantly on a buffer with 5000 lines.
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) " (buffer-string)))
This returns a list of function names that are defined via defun in the current open buffer. buffer-string returns content of a current buffer in a string, -map and s-match-string-all are taken from dash and s third party libraries (their GitHub pages explain how to install them), cadr returns a 2nd element of a list.
-map is analogous to Emacs built-in mapcar, it applies a function to each element of a list and returns a new list, s-match-string-all returns all possible regex matches in a string, parentheses in a regex denote a group (read more how to form Emacs regular expressions from EmacsWiki).
If you run it in eval-expression (Alt+:), it will just throw it into echo area, but that's not what you need. So below are variations that work with custom buffer or file. with-current-buffer allows to temporarily switch a buffer, while some code does actions inside it, f-read is a file reading function form another third-party library f.
(defun list-defined-functions (buffer)
(with-current-buffer buffer
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
(buffer-string)))))
(defun list-defined-functions-in-file (file)
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
(f-read file))))
Read Emacs Lisp manual and try to come up with whatever is useful for you.
How can I change the input port in racket?
That is, suppose I create a new input port:
(define my-port (open-input-string "this is a test"))
How can I make it so that (current-input-port) returns my-port now?
To add to Chris' answer; the current input port is what's known as a "parameter", which is very approximately a dynamically scoped setting/variable. In general, it's cleaner and more conservative to set the current input port only temporarily, using 'parameterize'. Like this:
(parameterize ([current-input-port my-port])
... do some stuff ...
)
Evaluating this code will cause the input-port to be set for your body code and any code that it calls, but won't "bleed over" into code that's evaluated outside; it will also undo the change on an exceptional or continuation-based exit.
(current-input-port my-port)
Don't do this at the racket REPL! This will cause all subsequent REPL input to come from that source. (It's okay to run inside DrRacket, however, even in the DrRacket REPL.)