I am dabbling in Emacs Lisp and I am trying to write the following function:
(defun buffer-file-name-body ()
(last (split-string (buffer-file-name) "/")))
What I am trying to achieve is to extract just the file name and extension from the full path given by (buffer-file-name). However, this implementation returns a list of one item ("scratch.el") ... I tried several things such as passing the result of (last) through (string) but that raises an error... Google did not return anything useful when I searched for Emacs List convert list to string. How to I do this?
It sounds to me like what you want is (file-name-nondirectory (buffer-file-name)) which returns the simple file name, sans any directory information, as a string.
Related
The org-cdlatex-mode helps to insert math latex symbols/tempelates in org-mode.
I would like to add some extra symbols/accents to it. I realized that the math accents are stored in the variable cdlatex-math-modify-alist-comb and if I add to that list in the scrach buffer, I will get the extra short key for the new symbol, for example running:
(add-to-list 'cdlatex-math-modify-alist-comb '(?o "\\operatorname" nil t t nil))
works as expected. However if I add the same line in the init file, the new key won't be added to the list and emacs complains with warning that the list variable is free!
I probably need to add to the list after it has been loaded, however I couldn't find any source of how to do it.
The variable that needs to be set in your case is cdlatex-math-modify-alist. The documentation of that variable says: Any entries in this variable will be added to the default.
The workaround I did is to create a function that adds them every time I call my math minor mode
(define-minor-mode org-math-mode
"Some config to write math on `org-mode'."
:lighter "org-math-mode"
(org-fragtog-mode 1)
(org-cdlatex-mode 1)
(lauremacs-cdlatex-add-math-symbols))
(defun lauremacs-cdlatex-add-math-symbols ()
(add-multiple-into-list
'cdlatex-math-symbol-alist-comb
'(
(?. "\\cdot" "\\dots")
(?\; "\\;")
(?C "" "\\mathbb{C}" "\\arccos")
(?N "\\nabla" "\\mathbb{N}" "\\exp")
(?Q "\\Theta" "\\mathbb{Q}")
(?R "\\Re" "\\mathbb{R}")
(?Z "" "\\mathbb{Z}")
)))
where add to multiple is just a helper function that uses add-to-list
(defun add-multiple-into-list (lst items)
"Add each item from ITEMS into LST."
(throw-unless (symbolp lst) "List should be a symbol.")
(dolist (item items)
(add-to-list lst item)))
This might lead to a very big 'cdlatex-math-symbol-alist-comb but it didn't bothered me yet, a solution to this is adding to list if element is not there or something like this
Just edit cdlatex.el and compile it. For example, I add sum with this piece of code:
("sum" "Insert \\sum_{i=1}{n}"
"\\sum_{i=1}{n}?" cdlatex-position-cursor nil nil t)
and it works.
I am trying to retrieve the output which-function-mode as a string in Emacs. I am going to create a keyboard binding shortcut that copies it to my clipboard.
It seems like this (which-func-mode ("" which-func-format " ")) is used to insert which-function into Emacs header line. However, I can't seem to get this output as a string or retrieve it in any way. There doesn't seem to be any documentation on the mode.
Any ideas for how to get the output of which-func-mode?
(which-function)
Return current function name based on point.
so to copy it to the clipboard, try
(defun my-copy-function-name ()
"Put name of function at point to kill-ring."
(interactive)
(kill-new (which-function)))
I'm running a command with compile so I can link from error messages to the associated source, but I need to transform a chunk of the content of matched lines to get the file to link to. (The line shows a clojure namespace, like foo-bar.quux, which needs to be transformed into foo_bar/quux.clj.)
The documentation of compilation-error-regexp-alist says, in part,
Each elt has the form (REGEXP FILE [LINE COLUMN TYPE HYPERLINK HIGHLIGHT...]). ... FILE can also have the form (FILE FORMAT...), where the FORMATs (e.g. \"%s.c\") will be applied in turn to the recognized file
name, until a file of that name is found. Or FILE can also be a function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME). In the former case, FILENAME may be relative or absolute.
When I add entries to compilation-error-regexp-alist-alist and compilation-error-regexp-alist with a function in FILE position, my function is called with no arguments. How do I get the matched line inside that function?
Opening compile.el and then searching with C-s (funcall jumped me to:
(setq file (if (functionp file) (funcall file)
(match-string-no-properties file))))
which seems to be the relevant spot and shows that the functions is indeed called with no arguments and that the match-data is still very much valid, so you can extract the file name with (match-string <the-file-sub-group>).
Is there a nice and friendly set of searchable documentation for ANSI Common Lisp anywhere? Preferably one that can be downloaded for use offline. I've Google but can only find static HTML pages that basically mean you need to know exactly what you're looking for.
I'm after something like http://erldocs.com/, where I can type something like "string" or "list" and all the matching functions come up instantly, for me to click on and browse easily.
Man pages are no use, since you need to know the exact function you need, while the searchable style allows you to discover functions without knowing them beforehand.
CLHS: Symbol Index
l1sp.org
In Slime, type C-cC-dh, a few chars of your search term, and then Tab to get a completion list.
Just type (apropos "term") in repl.
A downloadable version of the CLHS is available in info format:
ftp://gnu.org/gnu/gcl/gcl.info.tgz
Here's a handy emacs function to lookup the symbol under the point in the using F1:
(defun clhs-info ()
(interactive)
(ignore-errors
(info (concatenate 'string "(gcl) " (thing-at-point 'symbol)))))
(add-hook 'lisp-mode-hook
(lambda ()
(define-key lisp-mode-map [f1] 'clhs-info)))
You can do partial matching using Info-index rather than concatenation as well.
I want to add two numbers that are to be entered by the user in Lisp. How can I do that? I used the read command twice but it shows an error.
I recommend parsing numbers from user input with the parse-number library. Using read instead gives the user direct access to the Lisp reader, which you do not want.
Something like this should work:
(defun prompt-number (prompt &optional (prompt-stream *standard-output*)
(read-stream *standard-input*))
(format prompt-stream "~a: " prompt)
(parse-number:parse-number (read-line read-stream)))