org-mode source inclusion line numbers - emacs

Org-mode has a great feature to include source code like this:
#+begin_src java -n
/**
* #param foo
*/
public static void doBar(Baz ba)
{
Collection<String> strings = ba.getStrings(true);
return strings;
}
#+end_src
The -n option shows line numbers.
There's a +n option to have the numbering continue from the last block.
Is there any option to set the starting number? This would be useful for source code snippets where you want the line numbers to correspond to the full file.

This has been added in Org Mode 9. It's now possible to pass numbers to both -n and +n.
So, for example, you could make the example start on line 12 by using the following header:
#+begin_src java -n 12
...
#+end_src

I agree with jco. A long answer to the question could read like the following. But except from the screenshot it's just copy and paste from the manual.
As documented in the Section Literal Examples you can add line numbers to the source blocks and example blocks:
Both in example and in src snippets, you can add a -n switch to the
end of the BEGIN line, to get the lines of the example numbered. The
-n takes an optional numeric argument specifying the starting line number of the block. If you use a +n switch, the numbering from the
previous numbered snippet will be continued in the current one. The +n
can also take a numeric argument. The value of the argument will be
added to the last line of the previous block to determine the starting
line number.
You can also refer to line numbers:
In literal examples, Org will interpret strings like (ref:name) as
labels, and use them as targets for special hyperlinks like [[(name)]]
(i.e., the reference name enclosed in single parenthesis). In HTML,
hovering the mouse over such a link will remote-highlight the
corresponding code line, which is kind of cool.
An example for demonstrating both features is
#+BEGIN_SRC emacs-lisp -n -r
(save-excursion (ref:sc)
(goto-char (point-min))) (ref:jump)
#+END_SRC
In line [[(sc)]] we remember the current position.
[[(jump)][Line (jump)]] jumps to point-min.
It would produce

This answer is out of date, see the other ones
No implemented options that I know of.
A very hacky solution I just tested would be to define yourself the counter org uses for +n to work.
It goes :
#+begin_src emacs-lisp :exports results
;; we need it not be a buffer-local value
(setq-default org-export-last-code-line-counter-value 42)
#+end_src
As the result of being a dirty hack, it only works once, though, for the very first block, but I'm not sure how ±n behaves .
It is evaluated (because of :exports results, without actually exporting anything when I tried) before numbering any other blocks, so it can be anywhere in your buffer.
I guess it wouldn't be very hard to implement, or that it could be done better by someone who actually knows elisp, but I hope it'll help.

Related

Emacs Org-Mode: Reset Line Indention Within org-indent-mode

I love Emacs and Org-Mode. But I can only stand to use Org Mode in the clean view (or whatever it's called - with org-indent-mode on).
My problem is that I often want to use headers that don't have a bullet in front of them. I want one asterisk to be the start of the list, not the header.
Example:
List 1
List 2
Header 1
List 3
List 4
But when I try to do this, Header 1 gets indented to the level of List 2.
I know just turning off org-indent-mode and getting used to that is one solution. But is there a way to reset the indentation for Header 1?
The things you are talking about changing are pretty fundamental to org-mode; basically you are trying to change the org-mode syntax. The reason why Header 1 in your example is not being dedented, is that org-mode does not see it as a headline, because headlines by definition start with leading stars. Also, while it is technically supported to use * to identify a plain list item, this is not recommended, and can cause some unexpected behavior (see footnote 1 here).
That being said, you can have some control over the appearance of headlines. For example, you can use the org-bullets package. You can then define the bullets to use in place of * like this:
(setq org-bullets-bullet-list
'("◉" "◎" "⚫" "○" "►" "◇"))
which will define the bullets used for the first six levels of headlines. You can replace the bullets in that list with other utf-8 symbols, and you can even use " " as one of the symbols, so that your Headlines will be preceded by a single space. However, note that this only affects the way headlines are displayed; they will still be preceded by * in the actual file.
I know it is not very helpful, but my overall suggestion would be to stick with the org-mode syntax if you want to use org-mode, i.e., use a structure like this:
- List one
- List two
* Header 1
- List three
- List four
with * starting a headline, and - starting a plain list. Since org-mode files are just plain text, the magic of that mode depends heavily on those files having a set structure. In my own experience, if you try to change that structure (another example is changing timestamp formats), it will cause more headaches than it relieves, and cause a lot of the functionality that makes org-mode so great to break.
Just as a side note: I prefer a cleaner view as well, and one option I like to enable in addition to org-indent-mode is (setq org-hide-leading-stars t), which will display only a single star/bullet per headline (although the leading stars will still be present in the actual text file).

Combine org mode capture and drill modules to learn vocabulary

I want to use the capture module of org-mode to create a data base of new words that I want to learn, and then use the drill module to learn them (flash cards style).
In my org-capture-templates I added the following:
("v" "Vocabulary" entry
(file+headline (concat org-directory "/vocab.org")
"Vocabulary")
"* Word :drill:\n%^ \n** Answer \n%^")
This is a rather naive template which I borrowed from here. It works fine but it is too limited. Unfortunately I'm rather new to elisp and I don't know how to improve it.
I think the above template has to be improved in the following there aspects:
Headline Currently the first input string is the (new) word and the headline is fixed. How can the headline be the same (input) word? I think that the following result is desirable:
* Vocabulary
** Foo :drill:
Foo
*** Answer
What is foo
Actually an even better way would be to have 3 input strings.
The new word (for example foo) which will be the headline.
If the second is empty, then it gets the same string as (1). Otherwise, concatenates the string to the one from (1). E.g. having as second input bar would yield foo bar. This will be the content of the entry.
The word's definition which should come in the answer sub-headline.
Duplications (see again this) If at some later point I try to capture foo again, I would like to know it, and be directed to edit the already existing entry - skipping all the inputs.
Sorting After capturing I think it would be nice to sort the list of words. This should not be too hard given that the headline of each entry is the word itself. In this case one can probably use the org-sort-entries function.
I know this is a rather big questions but I also think that if it can be solved here it will be of great use to many users.
Edits:
Using #juan_g suggestions, I improved my template and now it is:
("v" "Vocabulary" entry
(file+headline (concat org-directory "/vocab.org")
"Vocabulary")
"* %^{The word} :drill:\n %t\n %^{Extended word (may be empty)} \n** Answer \n%^{The definition}")
I didn't manage to set the default value of the second input to be the 1st one. I tried something like %^{Extended word (may be empty)|%\1} but it returns ^A which is not helpful.
In any case, this improved version seems to be already usable.
About the input question, in Org Mode Manual: 9.1.3.2 Template expansion, there is the %\1 special escape code:
%\n Insert the text entered at the nth %^{prompt}, where n a number, starting from 1.
The duplications question probably would need some Emacs Lisp coding.
For sorting, see C-c ^ (org-sort).
BTW, org-drill seems indeed a really interesting package, based on SuperMemo's spaced repetition algorithms.
You need an extra "\", therefore %\\1 works as expected.

Searching meaning of a word under cursor in Emacs using `dict` utility

Hi I am currently reading some novels in .txt format downloaded from Gutenberg.
Often I come across a word I am unfamiliar with, and I need to look it up on the web.
I recently stumbled upon the Unix dict command-line utility to search for meanings of words. It fetches
the meanings of words from multiples dictionaries and thesauruses on the web.
I would like to have this integrated with Emacs in the following way.
I would like to pass the word under the cursor to the dict and split the window into two buffers
: one buffer containing the current text-file and another-one the meaning of the words.
How would I write an Emacs-Lisp function for doing this?
NOTE: Using dict at the command-line is as simple as dict *word-being-searched*
and I am using Emacs-24 under Ubuntu 11.04
The word-at-point function does most of the work you need:
(require 'thingatpt)
(defun lookup-word-at-point ()
(interactive)
(shell-command (format "dict %s" (shell-quote-argument (word-at-point)))))
Like any other invocation of shell-command, this will show the output of dict in the minibuffer if it's short enough; otherwise, it will pop up a window named *Shell Command Output*.
ispell is your friend. ispell-word, bound by default to M-$ does exactly that.
Also see wordnet.el
EDIT: As Thomas pointed out, ispell is not appropriate. I am a big fan of wordnet that I mentioned earlier, you will need to download the program.
Another option is an interface to the dict command, e.g. enter link description here or one of the alternatives listed on that page.
There is enough stuff out there that you don't need to write something on your own.

Possible bug in Org Mode export of item with `(*)`

I'm using org mode to make a list that I would like exported to PDF. some items in the list have the combination (*) and this seems to throw org mode off.
This MWE
#+TITLE: Bug?
#+OPTIONS: *:nil toc:nil author:nil
- hello (*) I would like to have two items
- may (*) I please?
results in
even though the #+OPTIONS: *:nil part was obviously read and understood (the part between the stars is not bold as it would be had that line been missing.
Is this is bug? Am I doing something wrong? Is there a workaround?
The *:nil option is not the problem here.
The problem comes from the fact that the * in (*) actually do emphasize the text (it is in bold font in the buffer). Given that, the exporter does its best, which is not very good.
The bug here is to allow multiline fontification over distinct list items -- we'll try to fix it, but that's a rather complex issue.
On top of the workaround above, have a look at `org-emphasis-regexp-components', thru which you can prevent parentheses as post/pre-characters in a fontified string.
I found a work-around for this behavior (bug?): add an empty line between the two offending lines:
#+TITLE: Workaround!
#+OPTIONS: toc:nil author:nil
- hello (*) I would like to have two items
- may (*) I please?
gives:
Thus allowing the string (*) to exist in an item (or rather in two consecutive items)
I'm not certain if this is a bug or not. I was able to reproduce it, however if I escaped the * the export occurred as expected.
#+TITLE: Bug?
#+OPTIONS: toc:nil author:nil
- hello (\*) I would like to have two items
- may (\*) I please?
Latex created:
\begin{itemize}
\item hello (\*) I would like to have two items
\item may (\*) I please?
\end{itemize}
I will however post the question to the mailing to confirm whether this is the expected behaviour or not in this situation. The thread for this question is HERE.
EDIT I edited to correct the LaTeX that was exported (needed to add this as 1 char is not enough, feel free to remove this line.)

How can I make Org-mode export to LaTeX with a specific preamble?

When I do C-c C-e l to export an Org file to LaTeX it produces a document with a particular preamble. Instead of this particular preamble I would like it to use a preamble of my choice. Say that I want it to use the following preamble:
% Don't forget to qpdf --linearize the final copy
\RequirePackage[l2tabu,orthodox]{nag}% Old habits die hard. All the same, there are commands, classes and packages which are outdated and superseded. nag provides routines to warn the user about the use of those.
\immediate\write18{sh ./vc}
\input{vc}% Version control macros (for \VCDateISO in \date) http://www.ctan.org/pkg/vc
\documentclass[a4paper,12pt]{article}% pt? doublepage?
%\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}% Latin Modern (derivate of Knuth's CM)
\usepackage{fixltx2e}% \textsubscript and bugfixes for LaTeX
\usepackage{microtype}
\usepackage[strict=true]{csquotes}% Context-sensistive quotes. \enquote "" \enquote* ''. Use the integrated commands \textcquote and \blockcquote rather than biblatex internal commands to get contex sensistive quotes for them too. s/babel/autostyle in new version.
\usepackage[bookmarks,pdfborder={0 0 0}]{hyperref}% links and pdfinfo. MUST BE LOADED LAST!
\hypersetup{% Setup for hyperref
pdftitle = {[Title from #+TITLE]},
pdfauthor = {[Author from #+AUTHOR]}
}
I know that you can manipulate which packages are used on a per file basis as described in the manual but I want this preamble to be used for all files unless ) specify otherwise. The preamble I want to use includes the following:
deactivated packages (such as geometry above)
packages loaded by RequirePackage
input macros
\immediate\write18 macros
comments after usepackage macros
a hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files
Deactivated packages (such as geometry above)
Org-mode recognizes LaTeX syntax inside LaTeX code-blocks, as well as when including LaTeX files in the content. (See Quoting LaTeX code.)
Packages loaded by RequirePackage
As above.
Input macros
As above.
\immediate\write18 macros
I believe this should also be as above, however there is an alternate method of dealing with this. If you create a source code block of type sh with the command within it, Org will evaluate it on export and produce the desired behaviour. You have to enable sh as a babel language type for it to work however.
(require 'ob-shell)
You can also include sh as one of the languages loaded by babel by adding it to org-babel-load-languages
(acons 'sh 't org-babel-load-languages)
Then use a code block similar to the following to run your ./vc
#+name: Test
#+begin_src sh :results output silent :exports results
./vc
#+end_src
As long as this comes before your \input{vc} line it should run the code and then include it. Simply follow the code-block with
#+LATEX: \input{vc}
And your content should be included.
Comments after usepackage macros
If the code is within a LaTeX block it should recognize it as LaTeX.
A hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files.
This will have to be included within each document rather than separate. The following will provide what you desire for your macros. It will not be within the preamble, however it will end up at the top of the document and the export does behave as expected (however it will not behave as expected if added through #+INCLUDE: from org.
#+begin_latex
\hypersetup{% Setup for hyperref
pdftitle = {{{{TITLE}}}}, %Org macro to take from #+TITLE
pdfauthor = {{{{AUTHOR}}}} %Org macro to take from #+AUTHOR
}
#+end_latex
Creating your own Latex export class
If you follow the instructions in the worg tutorials (See Org Latex Export) you can create your own export-class. If you want to have full control over the packages in the preamble you would simply need to:
(add-to-list 'org-export-latex-classes
'("<CLASS NAME>"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[NO-PACKAGES]"
<insert desired sectioning configuration>))
You can also add in your desired packages between the \\documentclass and [NO-DEFAULT-PACKAGES] lines. The alternative would be to add them to the file itself using:
#+LATEX_CLASS: <CLASS NAME>
#+LATEX_HEADER: \usepackage{package}
...
As a third option, you can simply create a custom .sty file with the desired packages etc and include it as a single #+LATEX_HEADER:.
This doesn't answer your question, but it does allow you to do what you want.
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-export-as-latex 3 nil nil nil t nil)
)
This function exports the content of your ORG-mode file without any preamble. You can then \input it into a file with your desired preamble. Further reading.
I use a different method to get things done :
Define a class (I call it per-file-class for some strange reason. You can call it something else). Put this code in your .emacs :
;; per-file-class with minimal packages
(unless (find "per-file-class" org-export-latex-classes :key 'car
:test 'equal)
(add-to-list 'org-export-latex-classes
'("per-file-class"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))
Use this class in your org file :
#+LaTeX_CLASS: per-file-class
#+LaTeX_CLASS_OPTIONS: [10pt, a4paper]
I'm not allowed to comment (bad reputation or something ;-)) so I will post an answer. A previous answer had a very nice snippet of code for a headless export function. That code needs updating for later versions of org:
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-latex-export-as-latex nil nil nil t nil)
)