Ansi-coloured file editing - emacs

Context
Have some transcript files from terminal interaction, obtained using traditional Unix command "script".
Those transcripts contain lots of control character (like backspace when editing shell commands), and lots of color code sequences as result of running various commands. Occasionally, even colorful full-terminal (ncurses-based) applications like "emacs -nw" or "aptitude" were run.
At program runtime, TERM environment variable was set as "xterm".
Need 1: read (more or less solved)
I need to read those files again and sometimes copy-paste some small parts.
The trouble is : while one color change here and there is not so much of a problem, their actual density makes the output barely readable. Worse, edited command lines (with cursor jumps and edited words) are completely unreadable.
"Okay" solution
Browse through files using e.g. "less -r". Paging forward in the same terminal setup reproduces the various color and character style.
But many other features come out more or less broken, e.g. search backwards produces jumbled terminal output, often have to pres "Ctrl-L" to clean thing up.
Need 2: editing
My preferred editor is emacs. Some people have had a similar situation when running the shell inside emacs, e.g. Something wrong with Emacs shell.
Here is not the same situation. Examples of differences: here we don't have to run an actual shell, but we need to move cursor freely like in usual editing.
Editing here means easily open such a transcript file in editor and then:
at all times through editing, see character changes (color, attributes) as conveyed by the terminal codes
(optional) some character that are neighbour on the terminal grid but are separated in file by some control characters would have a visual hint about this
ability to insert some text,
delete sections,
use all editor features like search/replace etc.
copy & paste to & from file (including to external programs, which would receive just plain text)
in my wildest dreams, some kind of "flatten" action, like select a sequence with a heavily edited command line and replace it with a simple series of characters as if it was typed in one run. "Visual hints" mentioned above would disappear.

Type Alt-: to evaluate something in the minibuffer. Evaluate (ansi-color-apply-on-region (point-min) (point-max)) and it will convert ansi color codes to be font colors.

Related

Wrapping already typed text in Emacs

I have a text that I already typed in a text editor, but when I open it in Emacs, so I can keep working o Emacs (Org-Mode), it appears it is not wrapping the lines propperly. The paragraph (or what it is supposed to be a paragraph) appears in a single line, instead of having a line break when reaching the screen limit.
Do you guys know how to reformat these line breaks?
Emacs has a built-in variable called word-wrap, which has a default value of nil. When word-wrap is nil, long lines are wrapped indiscriminately at the window-edge without consideration as to whether a whole word is visually broken up -- i.e., part of a word may be at the end of the visually wrapped line and part of the word may be at the beginning of the subsequent visually wrapped line.
The most common method of enabling word-wrap is to use the built-in visual-line-mode: http://www.gnu.org/software/emacs/manual/html_node/emacs/Visual-Line-Mode.html Visual line mode includes a few other goodies, like remapping certain keyboard shortcuts and pretty bitmap images in the fringes if they are visible in the window. Enabling visual-line-mode can be accomplished a number of ways -- one example is manually: M-x visual-line-mode

Eshell for RTL File Names

By default, Emacs 24 can handle Arabic, Hebrew, and other right-to-left (RTL) languages. I have multilingual term emulators that handle this in X, but I would like to use eshell inside emacs to see the non-English character filenames (and boy do I have many). When I use eshell, the characters are not in the correct order and it spells out gibberish. (setq-default bidi-display-reordering t) (which is default) makes no difference obviously.
Is this possible?
I believe you're getting double reordering by emacs + mlterm, and need to disable one.
Your screenshot shows in upper half mlterm successfully detects line-by-line base direction and reorders characters.
And Emacs 24 by default reorders bidi in the terminal, assuming a non-bidi LTR termianl.
Combining both results in double reordering ABC -> CBA -> ABC.
You have 2 options:
Disable bidi in mlterm (at least while running emacs, not sure how to automate that).
Disable bidi in emacs (at least when running under mlterm).
I.e. set bidi-display-reordering to nil, not t.
I expect the first will work better. mlterm can only give you a dumb "bidi display" layer, which may be confusing on top of an editor; emacs gives you "bidi editing" where someone thought of how the whole interaction should work.
Specifically, side-by-side "windows" within emacs should work well when emacs is doing the bidi and break horribly when mlterm is.
OTOH, mouse copy-paste probably works better when mlterm is doing the bidi.
If emacs sends reordered chars, that's what mlterm mouse selection will copy.
Emacs can mostly take over mouse with xterm-mouse-mode but (untested speculation) it works on terminal coordinates and would need extra work to match them to logical-order text.
P.S. I'm not sure why the end result is right-aligned, as it seems there is a blank line before so Emacs should consider it a new paragraph, decide it's RTL and send right-aligned text to mlterm, and I'd expect mlterm to then reverse the line resulting in it left-aligned. I'm probably missing something.

Emacs - How to keep text formatted to other editors?

I'm a beginner with emacs. Altough I'm finding it amusing and challenging, I still don't know some basic things, like, when I open a text or a piece of script wrote in another editors, emacs don't show the text formatted properly (missing all tabs, all text left-aligned) and vice-versa.
Also, when I copy a link with emacs with M-w, my clipboard is still empty and I can't paste it in a browser. I already did my "homework". I've read the tutorial and I'm almost finishing the manual and didn't see anything to address that.
tnx in advance.
Some editors, like Intellij IDEA for example, will indent code based on how they understand it and not based on how it was actually indented, there's no Emacs mode that operates in the same way, not to my best knowledge. If you were using something like Eclipse or MS Visual Studio before - then you probably just have a different size of tab character (this is why some programmers insist on indenting code with spaces rather than tabs). But the width of the tab character is adjustable. In order to customize it you would:
add in your initialization file (usually .emacs file in your $HOME directory, you can create one, if it is not there yet):
;; makes tab character as wide as four space characters
(setq default-tab-width 4)
though some other major editing modes override this variable, you would need to tell what language you are dealing with to get better instructions.
Clipboard, see this answer: How to copy text from Emacs to another application on Linux if you are on Linux, then likely you need to set x-select-enable-clipboard to t.
Aligning text to the right (or left for LTR languages) is not possible in Emacs, as far as I understand. You could align block of text, if you split it into lines and align on the line ends, but that would mean aligning by adding spaces at the beginning - something you don't really want to do.
Tabs should work (you might need to fix the width). Use mouse to select to the clipboard, or use CtrlInsert to copy and ShiftDelete to cut.
Assuming emacs has picked the right mode for the file - it usually does - you can press C-x h to select all, then TAB to indent all selected lines. What other editors are you using, and what platform(s)?
As for the clipboard issue, some builds of emacs work correctly with the native clipboard, some don't. You might want to investigate CUA mode.

Stop emacs from wrapping 80 column lines in an 80 column terminal?

In an 80 column wide terminal emacs wraps 80 column lines, putting a backslash in the 80th column. Is there a way to tell emacs to use all 80 columns of my terminal and not wrap lines until they reach 81 characters?
If I understand this question correctly, this is not about logical line
wrapping (how lines are segmented in your file), but about visual wrapping
(how lines are displayed with respect to window width).
If you just want the display to visually wrap more than zero characters before
window boundary, and thus avoid the backslash everywhere, however long your logical lines really are, you can use longlines-mode
:
Unlike Visual Line mode, Long Lines mode breaks long lines at the fill column (see Fill Commands), rather than the right window edge. To enable Long Lines mode, type M-x longlines-mode. If the text is full of long lines, this also immediately “wraps” them all.
Then, it's only a matter of setting the fill-column appropriately, using
either global settings (.emacs, though you probably want to use a
specific mode-hook for that particular case), local settings (file
variable, dir-locals) or C-u 79 C-uC-x
f to set variable fill-column to 79. This way, lines 79
characters or higher will wrap, but before touching the right edge of the
80-char window (and thus never leaving an ugly backslash character). Your
file will be untouched.
If you simply want no visual wrapping to occur on 80-character lines, and thus do not want the 80th logical character visually displayed below the first, there are two
possible answers:
either you work in an environment where you don't necessarily wrap
logically at or before 80 characters, and you want to see the end of
those 81+ lines somewhere in your screen (i.e. you do want visual wrap, but at a number of chars above the window width), then I don't know how to do
it.
or you want to stop your lines at 80 chars logically (e.g. you
have auto-fill on and fill-column at 80), and if you do happen to have
lines 81 characters or more, you don't care about seeing their ending.
In that case, activate truncate-mode (toggle-truncate-lines).
If the issue is about the last character of your window, and what you really want is the 80th logical character of your line to be displayed on the 80th visual character of your window, though, I'm afraid I don't know how. Either you are truncating lines (as above), and the last character of your window will be a $, or you let emacs do its thing, and the last character will be a backslash.
Note when testing that auto-fill's wrapping (but also longlines-mode's, since it is its visual equivalent) will occur only at word boundaries.
One option is to set the wrapping to happen at 81, instead of 80.
M-x set-fill-column RET 81
Another option, maybe the best choice, is to define the variable overflow-newline-into-fringe as t. Try this once, manually:
M-x set-variable RET overflow-newline-into-fringe RET t
Either of these could be set by default. You can do that through M-x customize or by editing your .emacs file. Post again if you need help.
BTW, do you use emacs in a graphical or terminal environment? In a graphical environment, I often just make the window larger if I have long lines. Or I may turn on line truncation with a horizontal scrollbar.
added later
With the added information that you are running emacs in terminal mode, as you discovered, none of those options work. I tried an example running emacs in putty, where I can change the size of the window and emacs picks it right up. So, I could size to 81 columns and my 80-column lines remain intact without continuation. I am not sure which TERM value you have assigned with tmux, but you could consider creating a custom terminal type (termcap or terminfo) which supports 81 columns. I only took a brief glance at tmux but I noticed that you can resize panes within a terminal.
Now, out of curiosity, what is the primary motivator for you using tmux? I would think that the resume capability would be valuable. I would find however, that the other features are not that useful because in an X-Window environment it is cheap & easy to open more terminals or if I am using putty, I can create more of those. As far as using emacs, whether I am running under X-Window or MS-Windows, I just create as many frames as I would like and can work quite easily with that. So, is there something else that makes you interested in using tmux?
Six years after the previous answers and the workaround I still use is to use 81 character-wide windows.
Unfortunately 80-wide windows that wrap 'correctly' are not possible because of evil fringe characters. Emacs requires the last (fringe) character to have exclusive use of the final column. The argument for this is an optional fringe character is ambiguous. I dream of the day someone will submit a patch to make the fringe character optional, and perhaps solving the ambiguity with a background color.
I don't think that is prossible in general, because emacs needs at least one character to indicate that the line continues in the next line (wraps), although I'm not sure, because emacs has so many options... You could maybe instead select "Word Wrap (Visual Line Mode)" in the "Options" menu (or keyboard):
M-x visual-line-modeRET
This makes the flow more natural, without showing (at least in text modes) the indication of wrapping.

How can I script vim to run perltidy on a buffer?

At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
After trying #hobbs answer I noticed that when filtering the entire buffer through perltidy the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy completed.
So building on #hobbs' and #Ignacio's answers, I added the following to my .vimrc:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(closing " added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy() will return the cursor to its original position plus or minus at most X bytes, where X is the number of bytes added/removed by perltidy relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim version: 7.2]
EDIT: Updated DoTidy() to incorporate #mikew's comment for readability and for compatibility with Vim 7.0
My tidy command:
command -range=% -nargs=* Tidy <line1>,<line2>!
\perltidy (your default options go here) <args>
If you use a visual selection or provide a range then it will tidy the selected range, otherwise it will use the whole file. You can put a set of default options (if you have any) at the point where I wrote (your default options go here), but any arguments that you provide to :Tidy will be appended to the perltidy commandline, overriding your defaults. (If you use a .perltidyrc you might not have default args -- that's fine -- but then again you might want to have a default like --profile=vim that sets up defaults only for when you're working in vim. Whatever works.)
The command to filter the entire buffer through an external program is:
:%!command
Put the following in ~/.vimrc to bind it to Ctrl-F6 in normal mode:
:nmap <C-F6> :%!command<CR>
For added fun:
:au Filetype perl nmap <C-F6> :%!command<CR>
This will only map the filter if editing a Perl file.
Taking hobbs' answer a step further, you can map that command to a shortcut key:
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
noremap <C-F6> :Tidy<CR>
And another step further: Only map the command when you're in a Perl buffer (since you probably wouldn't want to run perltidy on any other language):
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm command! -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm noremap <C-F6> :Tidy<CR>
Now you can press Ctrl-F6 without an active selection to format the whole file, or with an active selection to format just that section.
Instead of creating a new keyboard shortcut, how about replacing the meaning of the = command which is already in people's finger memory for indenting stuff? Yes, perlcritic does more than just indent but when you use perlcritic anyways, then you probably don't want to go back to the inferior "just indent" = command. So lets overwrite it!
filetype plugin indent on
autocmd FileType perl setlocal equalprg=perltidy
And now we can use = just like before but with the added functionality of perlcritic that goes beyond just indenting lines:
== run perlcritic on the current line
5== run perlcritic on five lines
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
gg=G run perlcritic on the entire buffer
And the best part is, that you don't have to learn any new shortcuts but can continue using the ones you already used with more power. :)
I'm used to select text using line oriented visual Shift+V and then I press : an I have !perltidy -pbp -et4 somewhere in history so I hit once or more up arrow ⇧.