I am converting a region to a table by using C-c |.
Is there a way to reverse the process, say after converting do some editing and go back to original format (tab separated values will do)?
I know that I can do it via org-table-export but that is too cumbersome.
Try orgtbl-to-tsv for tab-separated values.
There is also orgtbl-to-csv for comma-separated values.
Combining the table with a short code block to do the conversion is convenient. For example:
* Some heading
#+name: foo
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example
(orgtbl-to-csv x nil)
#+END_SRC
#+RESULTS: foo-csv
#+begin_example
1,2,3
4,5,6
#+end_example
C-c C-c on the code block will produce the results shown. Adding :colnames no as a header argument to the code block will also preserve the header line:
#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example :results raw :colnames no
(orgtbl-to-csv x nil)
#+END_SRC
#+RESULTS: foo-csv
#+begin_example
a,b,c
1,2,3
4,5,6
#+end_example
I needed this too and just wrote the following based on org-table-export:
(defun org-table-transform-in-place ()
"Just like `ORG-TABLE-EXPORT', but instead of exporting to a
file, replace table with data formatted according to user's
choice, where the format choices are the same as
org-table-export."
(interactive)
(unless (org-at-table-p) (user-error "No table at point"))
(org-table-align)
(let* ((format
(completing-read "Transform table function: "
'("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
"orgtbl-to-html" "orgtbl-to-generic"
"orgtbl-to-texinfo" "orgtbl-to-orgtbl"
"orgtbl-to-unicode")))
(curr-point (point)))
(if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
(let ((transform (intern (match-string 1 format)))
(params (and (match-end 2)
(read (concat "(" (match-string 2 format) ")"))))
(table (org-table-to-lisp
(buffer-substring-no-properties
(org-table-begin) (org-table-end)))))
(unless (fboundp transform)
(user-error "No such transformation function %s" transform))
(save-restriction
(with-output-to-string
(delete-region (org-table-begin) (org-table-end))
(insert (funcall transform table params) "\n")))
(goto-char curr-point)
(beginning-of-line)
(message "Tranformation done."))
(user-error "Table export format invalid"))))
(define-key org-mode-map (kbd "\C-x |") 'org-table-transform-in-place)
It'd be great if this got added to org-mode proper as I think many would use it.
Mark the region.
M-x replace-string
|
C-q TAB RET
If you want to tweak it, use replace-regex.
Here are the steps to use export the table as tab or comma separated values:
Use the command org-table-export. M-x org-table-export
Enter the filename to save to (or hit enter for the same file).
Select the format (this is where you can set the orgtbl-to-tsv or any other formats).
These are some of the formats that can be used:
orgtbl-to-csv
orgtbl-to-generic
orgtbl-to-html
orgtbl-to-latex
orgtbl-to-orgtbl
orgtbl-to-texinfo
orgtbl-to-tsv
Related
I use Emacs' org-mode to take notes in class, which I then export in latex. I found org-mode's default markers for bold and italic (* and / respectively) quite useful, but I had to stop using them to avoid confusing the org exporter. I would like to have a function to italicize a word selection. In other words, the function should append \textit{ and } respectively to the beginning and to the end of a selection.
Org has a function, defined in org.el, that does this, called org-emphasize (the code is posted below), but it is constructed to work only with the org markers. Unfortunately, I am not very proficient in elisp to modify it to do what I want.
Any suggestions on how to go about building my desired function, would be greatly appreciated. Thank you, all!
(defun org-emphasize (&optional char)
"Insert or change an emphasis, i.e. a font like bold or italic.
If there is an active region, change that region to a new emphasis.
If there is no region, just insert the marker characters and position
the cursor between them.
CHAR should be the marker character. If it is a space, it means to
remove the emphasis of the selected region.
If CHAR is not given (for example in an interactive call) it will be
prompted for."
(interactive)
(let ((erc org-emphasis-regexp-components)
(string "") beg end move s)
(if (org-region-active-p)
(setq beg (region-beginning)
end (region-end)
string (buffer-substring beg end))
(setq move t))
(unless char
(message "Emphasis marker or tag: [%s]"
(mapconcat #'car org-emphasis-alist ""))
(setq char (read-char-exclusive)))
(if (equal char ?\s)
(setq s ""
move nil)
(unless (assoc (char-to-string char) org-emphasis-alist)
(user-error "No such emphasis marker: \"%c\"" char))
(setq s (char-to-string char)))
(while (and (> (length string) 1)
(equal (substring string 0 1) (substring string -1))
(assoc (substring string 0 1) org-emphasis-alist))
(setq string (substring string 1 -1)))
(setq string (concat s string s))
(when beg (delete-region beg end))
(unless (or (bolp)
(string-match (concat "[" (nth 0 erc) "\n]")
(char-to-string (char-before (point)))))
(insert " "))
(unless (or (eobp)
(string-match (concat "[" (nth 1 erc) "\n]")
(char-to-string (char-after (point)))))
(insert " ") (backward-char 1))
(insert string)
(and move (backward-char 1))))
I do not know if it worth defining a new lisp function. This is mainly a LaTeX problem as described here: https://tex.stackexchange.com/questions/258394/make-block-of-text-italicized
You can transpose this solution to Org as follow. First, observe how Org special blocks:
#+BEGIN_mySpecialBlock
...
#+END_mySpecialBlock
are exported to LaTeX
\begin{mySpecialBlock}
...
\end{mySpecialBlock}
(see Org doc Special-blocks-in-LaTeX-export for further details)
Then define new LaTeX environments as:
#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars} {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars} {\par\bfseries} {\par}
The lipsum LaTeX package is used to generate some dummy text.
#+LATEX_HEADER: \usepackage{lipsum}
You can test the approach with this Org document
#+LATEX: \lipsum[66]
#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote
#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars
#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote
#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars
After LaTeX export, C-c C-e l o, you will get something like:
For a better clarity here is the complete Org document. For a real document remove all references to lipsum which is only the text generator.
#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars} {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars} {\par\bfseries} {\par}
#+LATEX_HEADER: \usepackage{lipsum}
#+TITLE: My Title
* My section
#+LATEX: \lipsum[66]
#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote
#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars
#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote
#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars
I have this:
(defun promptread (prompt)
(format *query-io* "~10t~a:" prompt)
(force-output *query-io*)
(read-line *query-io*))
(defun prompt-cd ()
(make-cd
(promptread "Artist")
(promptread "Album")
(promptread "Rating")
(promptread "Like [y/n]")))
It works, however the format ~10t only affects the first call to promptread inside make-cd; the others are left-aligned without this padding.
Why would this be?
REPL:
CL-USER> (addcds)
Artist:Dixie
Album:Funny
Rating:22
The first promptread is indented because of the format with ~10t but not the others, which use the same exact format call.
The problem is that after force-output and readline, the cursor is not known to formatto be at position 0. Thus absolute tabulation will fail. If you start the format string with ~&, you will see this as an additional newline will be outputted anyway.
To solve the problem use the # modifier to get relative tabulation:
(format *query-io* "~10#t~a:" prompt)
The original string is like this:
# chrom,name,strand,txStart
And the result should looks like this:
# $1: chrom
# $2: name
# $3: strand
# $4: txStart
Does anyone have idea of a quick way to do that?
Lots of ways.
You could use a search and replace making use of the \# counter in the replacement. That's zero-based, so you'd either need to add a dummy replacement at the front to use up the zero, or else use the elisp replacement expression \,(1+ \#) instead.
You could use a keyboard macro, and insert a counter with C-xC-kTAB or <f3>. You can seed that counter by supplying a prefix argument when you start recording.
On Emacs 24 you can number the lines of a marked region using a custom format string with C-uC-xrN, so your format string could be # $%1d:
Evaluate following code and execute foo function on input line.
(require 'cl)
(defun foo ()
(interactive)
(let* ((str (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(words-str (and (string-match "# \\(.+\\)$" str)
(match-string 1 str)))
(buf (get-buffer-create "*tmp*")))
(unless words-str
(error "Line should be '# word1,word2,...'"))
(with-current-buffer buf
(erase-buffer)
(loop with index = 1
for word in (split-string words-str ",")
do
(progn
(insert (format "# $%d: %s\n" index word))
(incf index)))
(pop-to-buffer buf))))
I would like to be able to specify the window layout of the Emacs when starting it from commandline.
More specifically, I invoke "emacs file1 file2 file3 file4" and would, for example, like see
+---------+ +--------+
| file1 | | buff |
| | | list |
+---------+ instead of the default +--------+ that I see currently
| | | |
| file3 | | file4 |
+---------+ +--------+
My emacs is GNU Emacs 24.0.91.1, and I do not use emacsclient.
Note, I do not want to make the change permanent. That is why I ask for command-line solution.
Put the following in layout.el
(setq inhibit-startup-screen t)
(defun ordered-window-list-aux (tree)
(if (windowp tree)
(list tree)
(append (ordered-window-list-aux (nth 2 tree))
(ordered-window-list-aux (nth 3 tree)))))
(defun ordered-window-list ()
"Lists windows from top to bottom, left to right."
(ordered-window-list-aux
(car (window-tree))))
(require 'cl)
(defun fill-windows ()
"Make window list display recent buffer."
(mapcar*
(lambda (win buf)
(set-window-buffer win buf))
(nreverse (ordered-window-list))
(buffer-list)))
(delete-other-windows)
;; your window configuration
(split-window-horizontally)
(split-window-vertically)
;; Make window list display recent buffer
(fill-windows)
Then
emacs blah foo bar --load layout.el
The only thing you have to do is customizing the layout the way you want using a combination of the following functions:
(split-window-horizontally)
(split-window-vertically)
(other-windows 1)
This is a follow-up to How to get C-x C-e to display the result in octal and hexadecimal by default?
Is there a way to get C-x C-e to to display the result in decimal and hexadecimal only?
e.g.
(+ 40 2)
should produce
42 (#x2a)
instead of
42 (#o52, #x2a, ?*)
You just need to rewrite/tweak the routine that creates the printed format for this. Add the following to your .emacs and you'll be good to go.
(require 'simple)
(defun eval-expression-print-format (value)
"Format VALUE as a result of evaluated expression.
Return a formatted string which is displayed in the echo area
in addition to the value printed by prin1 in functions which
display the result of expression evaluation."
(if (and (integerp value)
(or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
(eq this-command last-command)
(if (boundp 'edebug-active) edebug-active)))
(let ((char-string
(if (or (if (boundp 'edebug-active) edebug-active)
(memq this-command '(eval-last-sexp eval-print-last-sexp)))
(prin1-char value))))
(format " (#x%x)" value value))))