get symbol-name without uppercase - lisp

Is it possible in Common Lisp to get a symbol-name without the uppercase result?
(symbol-name 'aAbB)
;; => "AABB"
(OTHER_FUNCTION? 'aAbB)
;; => "aAbB"
I would like to use a symbol name as a string but case-sensitive.

Your symbol is actually all uppercase, because the reader already upcases it. In order to prevent that, you can either use a different readtable-case or escape the symbol, using either enclosing pipe symbols: '|aAbB| or a backslash for the next character: '\aA\bB.

There is quite a full answer on this question: Why is Common Lisp case insensitive
"The readtable objects has an attribute, readtable-case, that controls how the reader interns and evaluates the symbols read. you can setf readtable-case to :upcase(default), :downcase, :preserve, :invert.
By default, the readtable-case is set to :upcase, which causes all symbols to be converted to upcase."

Related

Noweb does not cross-reference Perl identifiers delimited on the left by #

Consider this Noweb source file named quux.nw:
\documentclass{article}
\usepackage{noweb}
\usepackage[colorlinks]{hyperref}
\begin{document}
<<quux.pl>>=
my #foo ;
my $bar ;
my %baz ;
# %def foo bar baz
\end{document}
and compiled using the commands:
$ noweb quux.nw
$ latexmk -pdf quux.tex
The identifiers bar and baz are properly highlighted as identifiers and cross referenced in the PDF output. The identifier foo is not.
It's my understanding that Noweb has a very simple heuristic for recognizing identifiers. foo should be recognizable as an identifier because, like bar and baz, it begins with an alphanumeric, is delimited on the left by a symbol (at-sign), and is delimited on the right by a delimiter (whitespace).
I considered the possibility that the at-sign was being interpreted by Noweb as an escape and tried doubling it, but that (i) did not solve the problem, and (ii) introduced the syntax error my ##foo into quux.pl. This makes sense because according to the fine manual, a double at-sign is only treated specially in columns 1–2.
Noweb treats # as alphanumeric, with the rationale that it “helps LaTeX”. I did not find anything about this in the Noweb manual. This is documented only in the Noweb source file finduses.nw, line 24, in Noweb version 2.12.
Apparently, when writing your own LaTeX package, any macro you define has public scope. To write “private” macros, the trick is to temporarily reclass the # as a letter at the top of the package, incorporate an # into the name of each “private” macro, and restore the class of # at the bottom of the package. The macro remains public, but is impossible to call because the name gets broken up into multiple lexemes. (A user can still call such a macro by reclassing # as a letter before the call, but if they do that, they assume the risk.)
So yes, # should be included as an alphanumeric character when the code block contains a LaTeX package.
The full list of symbols treated as alphanumeric by Noweb is:
_ ' # #
The _ is treated as an identifier character in many programming languages, so Noweb is right to treat it as alphanumeric.
The # is treated as alphanumeric to “avoid false hits on C preprocessor directives”.
No explanation is given for treating the ' as alphanumeric.
Ideally, Noweb would support separate character class schemes for each source language. But as I understand it, Noweb has only the one global character class scheme, and no support for changing it (other than modifying the source).
Fortunately, Perl has alternate syntaxes for array identifiers that work around this limitation. Instead of #foo you can write #{foo} or even # foo and it will work.

Lisp return String of Symbol name

Basically I'm looking for a function that does the opposite of the following.
(intern "CAR")
This question is related. In Common Lisp, is there a function that returns a symbol from a given string?
The operator you are looking for is string (see the manual):
(string (intern "CAR"))
returns "CAR".
Either string or symbol-name would work to get the name of a symbol.
If you know specifically that you're passing n a symbol, symbol-name might allow both a compiler to generate better code as well as signal to a human reader that the argument is expected to be a symbol.

How do i convert a string to a quoted variable

Lets say i want to get the documentation for a function, I'd say
(documentation 'foo 'function)
but what if I only had foo and function as strings? E.g. "foo" and "function".
What would I have to do to them to make them usable as parameters to the documentation call?
[Side note: I'm using clisp, but I doubt that matters.]
Use FIND-SYMBOL, not INTERN. If you want to find documentation for an existing function, finding a symbol is enough. INTERN also creates symbols.
CL-USER > (find-symbol "SIN" "COMMON-LISP")
SIN
:EXTERNAL
Note that Common Lisp symbols are uppercase internally be default. Thus you need to use an uppercase string to find the corresponding symbol in the corresponding package.
Also note that there actually isn't something like a 'quoted variable'. You want to convert a string to a symbol.
Use INTERN to convert a string to a symbol. Make sure you uppercase the strings because, unlike with symbols, the reader will not do it for you:
(tested in SBCL):
* (documentation 'mapcar 'function)
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."
* (documentation (intern "MAPCAR") (intern "FUNCTION"))
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."

What do these symbols mean in Emacs Lisp?

When I read some elisp code, I found something like:
(\,(* 2 \#1))
\,(format "%s %s id%d %s" \1 \2 (+1 \#) \3)
#'(bla bla)
What does the symbol like "\,", "#", "#'" mean? Which session should I look into for those kind of things?
\, is special in replacements when using query-replace-regexp. It means "evaluate the following elisp expression, and use the resulting value in the replacement".
n.b. It's not special elsewhere (that I'm aware of), so that should be the usage you've seen.
\# is also special in the replacement string, and is substituted with the number of replacements made thus far. (i.e. an incrementing counter).
\#N (where N is a number) is a variant of \N which treats the group in question as a number rather than a string, which is useful when the expression you're evaluating requires a number.
So (\,(* 2 \#1)) would be a replacement which evaluates the expression (* 2 \#1), multiplying the number matched by the first group of the regexp by 2 to produce some value N, such that the final replacement is (N).
You can find these detailed in the manual.
C-hig (emacs) RET followed by a search for the syntax in question. e.g. C-s \, with a repeated C-s if the search fails (as it will) to find a match in the subsequent nodes.
#'... is short-hand for (function ...) which is a variant of '... / (quote...) which indicates that the quoted object is a function.
As this is elisp syntax, you find it in the elisp manual:
C-hig (elisp) RET
You can either use C-s #' or in this case it's indexed, so I #' RET also works.
(In general check the index first, and then use isearch.)
For info on backquotes, see http://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html.
# starts the reader syntax, for instance #' is a reader alias for function.
For more info see http://definitelyaplug.b0.cx/post/emacs-reader/
The #' is a short hand for using functions, for more details see here: http://www.gnu.org/software/emacs/manual/html_node/elisp/Anonymous-Functions.html
Backslash \ has two functions: it quotes the special characters (including ‘\’), and it introduces additional special constructs. More here: https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexps.html#Regexps

Is it possible to change an emacs syntax table based on context?

I'm working on improving an emacs major mode for UnrealScript. One of the (many) quirks is that it allows syntax like this for specifying tooltips in the Unreal editor:
var() int MyEditorVar <Foo=Bar|Tooltip=My tooltip text isn't quoted>;
The angle brackets after the variable declaration denote a pipe-separated list of Key=Value metadata pairs, and the metadata is not quoted but can contain quote marks -- a pipe (|) or right angle bracket (>) denotes the end.
Is there a way I can get the emacs syntax table to recognize this context-dependent syntax in a useful way? I'd like everything except for pipes and right angle brackets to be highlighted in some way inside of these variable metadata declarations, but otherwise retain their normal highlighting.
Right now, the single quote character is set up to be a quote delimiter (syntax designator "), so font-lock-mode interprets such a quote as starting a quoted string, which it's not in this very specific instance, so it mishighlights everything until it finds another supposedly matching single quote.
You'll need to setup a syntax-propertize-function which lets you apply different syntax designators to different characters in the buffer, depending on their context.
Grep for syntax-propertize-function in Emacs's lisp directory to see various examples (from simple to pretty complex ones).
You'll probably want to mark the "=" chars after your "Foo" and after your "Tooltip" as "generic string delimiter", then do the same with the corresponding terminating "|" and ">". An alternative could be to mark the char before the ">" as a (closing) generic string delimiter, so that you can then mark the "<" and ">" as open&close parens.