How to select a region with a leading \$, and surround it with a yasnippet - emacs

Could anyone please suggest a method to surround a region that contains a leading \$ and surround it with a snippet. In latex-mode, I am frequently underlining or double-underlining monies due and the yasnippet being used removes the backslash. I'd like to be able to use the same snippet for all situations -- with or without a leading \$.
# -*- mode: snippet -*-
# contributor: lawlist
# key: underline_selected
# group: font
# name: underline_selected
# binding: C-I u s
# --
\uline{`yas/selected-text`}

There are few problems that cause the behavior described by the original poster, who uses a custom modified version of tex-mode.el, not AUCTeX.
First, the function yas--snippet-parse-create contains, among other codes, the following functions that do not play well with LaTeX escaped dollar signs:
(yas--protect-escapes nil `(?\\ ?` ?'))
(yas--protect-escapes)
(yas--restore-escapes)
(yas--delete-regions ys--dollar-regions)
Second, the variable yas--simple-mirror-regexp catches dollar amounts, in addition to the standard yasnippet fields such as $1. When the above-mentioned (yas--delete-regions yas--dollar-regions) is called by yas--snippet-parse-create, the result is an erroneous deletion. The author of this answer has modified the regexp to exclude a dollar-sign with a preceding backslash:
(setq yas--simple-mirror-regexp "[^\\]$\\([0-9]+\\)")
The author of this answer does not presently have a fix for yas--protect-escapes and yas--restore-escapes, and has merely commented them out in the meantime. [This would obviously be problematic for anyone doing programming, but appears to be sufficient for merely writing LaTeX documents.] An issue has been opened on Github and the author of this answer will update this thread if a solution is found there.

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.

How do I tell org-mode to disable headings in verbatim text?

How do I make org mode not interpret a line that begins with an asterisk as a headline? I have some verbatim text in my org mode document. Some of the lines begin with an asterisk. Org mode interprets these lines as headlines. I don't want that.
Here is the text with some context:
* 20160721 Headline for July 21, 2016
I created a git repository for rfc-tools. It's in
~/Documents/rfc-tools.
Renamed grep-rfc-index.sh to search-rfc-index.sh because it searches.
That it uses grep is irrelevant.
Wrote a README.md for the project. Here it is:
#+BEGIN_SRC text
----- BEGIN QUOTED TEXT -----
This is the README.md for rfc-tools, a collection of programs for
processing IETF RFCs.
* fetch-rfcs-by-title.sh downloads into the current directory the RFCs
whose titles contain the string given on the command line. Uses an
rfc-index file in the current directory. Prefers the PDF version of
RFCs but will obtain the text version if the PDF is not available.
* fetch-sip-rfcs.sh downloads RFCs that contain "Session Initiation"
in their titles into the current directory.
* search-rfc-index.sh searches an rfc-index file in the current
directory for the string given on the command line. The string can
contain spaces.
* join-titles.awk turns the contents of an rfc-index file into a
series of long lines. Each line begins with the RFC number, then a
space, then the rest of the entry from the rfc-index.
----- END QUOTED TEXT -----
#+END_SRC
I want the lines between "----- BEGIN QUOTED TEXT -----" and "----- END QUOTED TEXT -----" to be plain text and subordinate to the headline "20160721 Headline for July 21, 2016". Org mode interprets all lines that begin with an asterisk as top-level headlines.
By the way, the verbatim text is Markdown. I hope that doesn't matter.
worked for me:
#+BEGIN_SRC markdown
Try wrapping your text in one of the various special block tags. For example you could try putting your text inside these tags:
#+BEGIN_SRC text
...
#+END_SRC
Here is a screenshot of how the formatting turns out on my Emacs:
If that doesn't meet your needs, you could try:
#+BEGIN_EXAMPLE
...
#+END_EXAMPLE
Which will render everything inside the tags without markup and in a monospace font.
If that doesn't work either, you could try one of the other kinds of tags listed here.
Escape the * with a comma like this,*
Probably if you type C-c ' to enter a special edit and then exit, org will do that for you.
I think the answer is "You can't do that". I found a way to work around the problem using drawers. The org-mode manual explains that a drawer is a place to put text that you don't want to see all of the time.
A StackExchange user had a question about
getting a custom org drawer to open/close. It seems that for older versions of org-mode, you must tell org-mode the names of your drawers. E.g. If you have a drawer named "COMMANDS"
:COMMANDS:
ls
cat
grep
:END:
you must tell org-mode the name of the drawer using the +DRAWERS keyword:
#+DRAWERS COMMAND
and restart org-mode.
I found a solution:
Escape Character
You may sometimes want to write text that looks like Org syntax, but should really read as plain text. Org may use a specific escape character in some situations, i.e., a backslash in macros (see Macro Replacement) and links (see Link Format), or a comma in source and example blocks (see Literal Examples). In the general case, however, we suggest to use the zero width space. You can insert one with any of the following:
C-x 8 zero width space
C-x 8 200B
For example, in order to write ‘[[1,2]]’ as-is in your document, you may write instead
[X[1,2]]
where ‘X’ denotes the zero width space character.
How to remove zero width space:
sed -i "s/$(echo -ne '\u200b')//g" abc.txt

How can I emphasize or verbatim quote a comma in org mode?

I tried to make the comma *,* bold, but no success. I tried with verbatim =,=, but no success as well.
You can achieve what you want by adding the following to your .emacs:
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\r\n\"'")
(org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)
Explanation
The manual says that org-emphasis-regexp-components can be used to
fine tune what characters are allowed before and after the markup characters [...].
It is a list containing five entries. The third entry lists characters that are not allowed to immediately follow or precede markup characters. By default, , is one of them so in order to successfully apply formatting to this character we have to remove it from the list of characters disallowed before or after the markup characters. This is what the call to setcar does. The purpose of the second line is to rebuild the regular expression for emphasis based on the modified version of org-emphasis-regexp-components.
Sources
This answer to a related question
~"~ doesn't register as verbatim on the org-mode mailing list
C-h v org-emphasis-regexp-components RET
There's a similar problem and I've figured out a solution.
#itsjeyd's solution is right but not 100% correct. We need an extra (org-element--set-regexps).
The full code snippets:
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n\r")
(custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))
(org-element--set-regexps)

How to escape double quote?

In org mode, if I want to format text a monospace verbatim, i.e. ~...~, if it is inside quotes: ~"..."~, it is not formatted (left as is).
Also, are quotes a reserved symbol, if so, what do they mean? (they don't seem to affect the generated HTML / inside Emacs display).
The culprit in this case is the regular expression in org-emph-re org-verbatim-re, responsible for determining if a sequence of characters in the document is to be set verbatim or not.
org-verbatim-re is a variable defined in `org.el'.
Its value is
"\([ ('\"{]\|^\)\(\([=~]\)\([^
\n,\"']\|[^
\n,\"'].?\(?:\n.?\)\{0,1\}[^
\n,\"']\)\3\)\([- .,:!?;'\")}\]\|$\)"
quotes and double quotes are explicitly forbidden inside verbatim characters =~ by
[^
\n,\"']\|[^
\n,\"']
I found discussions dating back 3 years comming to the conclusion that you have to tinker with this regular expression and set the variable org-emph-re/org-verbatim-re to something that matches your wishes in your emacs setup (maybe a file local variable works as well). You can experiment by excluding double quotes from the excluding character classes and outside matches as in
"\([ ('{]\|^\)\(\([*/_=~+]\)\([^
\n,']\|[^
\n,'].?\(?:\n.?\)\{0,1\}[^
\n,']\)\3\)\([- .,:!?;')}\]\|$\)"
but looking at that regex, heaven knows what happens to complex documents -- you have to try...
Edit: as it happens, if I evalute the following as region, quotes inside = are exported correctly, but nothing else is :-), I investigate further when I have more time.
(setq org-emph-re "\([ ('{]\|^\)\(\([*/_=~+]\)\([^
\n,']\|[^
\n,'].?\(?:\n.?\)\{0,1\}[^
\n,']\)\3\)\([- .,:!?;')}]\|$\)")
Edit 2:: Got it to work by changing org.el directly:
Change the line following (defvar org-emphasis-regexp-components from '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1) to '(" \t('{" "- \t.,:!?;')}\\" " \t\r\n,'" "." 1) and recompile org then restart emacs.
This was a defcustom prior to the 8.0 release, it isn't anymore, so you have to live with this manual modification.
regards,
Tom
Finally, I found a solution from http://comments.gmane.org/gmane.emacs.orgmode/82571
According to that thread, the regexp for verbatim is built from variable org-emphasis-regexp-components, which defines legal characters before, after, at the border of, or in the body of emphasis; and verbatim is one of the emphasis environment in org mode.
A workable setting given by that thread:
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n,")
(custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))
For small amounts of characters which have some unwanted effect in Emacs org-mode (because being metacharacters) it may be helpful to have a look at special symbols in org-mode (org-entities.el).
So for example " can be encoded by \quot{} (where the braces pair at the end is not mandatory, but needed if no whitespace follows).
So instead ="..."= you would write =\quot{}...\quot{}=.
That is some typing more and looks pretty ugly. But for the latter org-mode has a solution: by C-c C-x \ you can toggle a display magic for those symbols. If the magic is active, so directly after typing \quot{} resp. \quot{} a " will be displayed.
Besides, this symbols list can easily be extended, f.e.
(add-to-list 'org-entities
'("backslash" "\\textbackslash" nil "\\" "\\" "\\" "\\"))
Nevertheless I am heavily missing easier escaping in org-mode, besides the above solution and besides escaping a whole line by a : at its beginning.
I'd be happy if =verbatim= in all cases would leave the text between the ='s unchanged. Not =this*bold*text=, but =this *bold* text=. Like we know that from each well-designed markup/-down language.
But, of course, this is better placed at the org-mode development pages. Ideally with a fitting patch... :-)
I've met similar problem, and thanks #chaiko for a basic solution. However, #chaiko's solution only work for org-mode's fontification, it doesn't affect org-export. To get correct exported document, you need to do some more extra hack to org-mode's parser by (org-element--set-regexps).
So the full code snippets should be something like:
(setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n\r")
(custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))
(org-element--set-regexps)
I've integrated this to my oh-my-emacs project: https://github.com/xiaohanyu/oh-my-emacs/blob/e82fce10d47f7256df6d39e32ca288d0ec97a764/core/ome-org.org#code-block-fontification .

Comment token inside string

In pig etc. /* begins a block comment. If I put this in a regex string 'blah/blah/*', emacs thinks this is a block comment and syntax highlighting goes to hell. I am not familiar with elisp but I am certain that is a problem with script that is providing annotations for pig.
How can I fix it?
phils pointed out a better designed major mode in the question comments, but since you are still curious: The pig mode version you are using doesn't have the syntax table set up right. The most reliable way for emacs to recognize comments and strings is to use the syntax table to map characters to start/end of comments and strings. The version you are using is trying to do it with font-lock.
You have to escape the \'es and the *. All the characters that are used by the regexp engine, have to be escaped.
If you want to match "\", you might have to write "\\" when using replace-regexp interactively and "\\\\" if you use it as a lisp function.
(I even have to escape my escapes in this comment, so there are 8 escapes in the last escape sequence above)