emacsclient, restore all windows in a frame in terminal - terminal

The object: In terminal, on start, in one frame, restore emacs workspace. i.e. restore all "windows", to the state before C-x C-c.
Descriptions: The window restoration could be simply done in emacs GUI, by adding (desktop-save-mode 1) to the init.el file.
However, when running emacs as a Daemon, emacsclient does not perform the same way. With the same setup, it still remember the major mode of the file, as well as other buffers before exiting, but the window layout will not be restored. Only 1 major window is displayed depend on the default settings.
I did some research, poked into some functions/variables using the elisp build-in documentation. I checked (desktop-save-buffer 1), framset-save, framset-restore and none of them seems to work. Also I checked similar questions posted before, and I found someone's answer using the following script:
(setq desktop-restore-forces-onscreen nil)
(add-hook 'desktop-after-read-hook
(lambda ()
(frameset-restore
desktop-saved-frameset
:reuse-frames (eq desktop-restore-reuses-frames t)
:cleanup-frames (not (eq desktop-restore-reuses-frames 'keep))
:force-display desktop-restore-in-current-display
:force-onscreen desktop-restore-forces-onscreen)))
However this script is buggy, it opens a "GUI" frame, and could not be closed properly(reopen automatically whenever closed, and if possible, I prefer to open the frame in terminal, so that a terminal 'texteditor' setting is possibe, with some other minor merits).
Sorry for the tedious question, any help would be appreciated.

I don't really know the right way to do this, but I was able to restore a frameset created in a GUI frame into a terminal frame. The new frame must be big enough or it will throw an error. I did (setq foo (frameset-save (frame-list))) and (pp foo (get-buffer "*scratch*")) to dump a readable representation of the frameset to the *scratch* buffer. Then I edited it by hand to get rid of (display . ":0.0"). That seemed to be enough to get it to not force a GUI frame. Then I did (frameset-restore [frameset ...]) on the edited frameset.
If you look at frameset.el, there are filtering functions to edit a frameset programmatically. Also, if you look at the comments, the display parameter is treated specially. You should look at the details there.

Related

How do I stop emacs from highlighting the line under the mouse in shell

In emacs shell, invoked using M-x shell, input lines under the cursor are highlighted, so you can then "mouse 2: insert after prompt as new input" to repeat the command. I'd prefer not to have this activated and just select text under the mouse as I do in a regular terminal and middle click to insert (I know I can do this in emacs but I don't get any visual feedback until I stop the mouse selection).
Is there any way to do this? I've done quite a bit of searching and can't find anything.
That message comes from this function in comint.el.
Setting comint-use-prompt-regexp to a non-nil value should do what you want.
A good solution would probably be something like this (put the following somewhere in your initialization; regexp taken from comint-prompt-regexp docstring):
(add-hook 'shell-mode-hook
(lambda ()
(set (make-local-variable 'comint-use-prompt-regexp) t)
(set (make-local-variable 'comint-prompt-regexp) "^[^#$%>\n]*[#$%>] *")))
You do not specify the version of Emacs and I do not see that behaviour in my Emacs why I type M-x shell. However, a little googling turned up this:
(setq comint-highlight-input nil)
which is something that you can set in ~/.emacs.
This variable does not exist in GNU Emacs 25, even though there is a reference to a specific face (highlighting) to use in that case. Either that is a mere reminisence from when the feature was available or it is something that is actually enabled with another package. You can test by starting emacs with the "-Q" option and start shell as the first thing, if the behaviour does not appear, it is something that is related to your startup configuration or something you do before starting the shell.
The post in which the above variable was mentioned can be seen here:
https://stat.ethz.ch/pipermail/ess-help/2002-August/001134.html

Why does shell mode display some rubbish code?

When I use bower in M-x shell, as you can see in the picture, some rubbish code is displayed.
However M-x ansi-term works well
What could be the problem ? Is it possible to make shell mode display properly ?
Those symbols are ANSI escape sequences that the terminal emulator uses for visual effects like changing the color of text. shell-mode apparently doesn't know how to display these codes by default. What you want may be Term Mode:
Some programs (such as Emacs itself) need to control the appearance of the terminal screen in detail. They do this by emitting special control codes. Term mode recognizes and handles ANSI-standard VT100-style escape sequences, which are accepted by most modern terminals, including xterm. (Hence, you can actually run Emacs inside an Emacs Term window.)
Try the solution given in Cucumber's ANSI colors messing up emacs compilation buffer:
(require 'ansi-color)
(defun colorize-compilation-buffer ()
(toggle-read-only)
(ansi-color-apply-on-region (point-min) (point-max))
(toggle-read-only))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
that works beautifuly for me on emacs24.
ps: to colorize even more the shell output I like to play with
M-x highlight-regexp RET a regexp, i.e. \[OK\] RET a color (make use of TAB to see choices)
or
(add-hook 'shell-mode-hook (lambda () (highlight-regexp "\\[OK\\]" "hi-green-b")))
and (add-hook 'shell-mode-hook (lambda () (goto-address-mode ))) to make URLs clikable. Looking for the same stuff for file paths.
edit: making file paths clickable is as easy as using compilation-shell-minor-mode :)
edit2: my sources: http://wikemacs.org/index.php/Shell
WRT to bash, sometimes setting $PAGER helps here, i.e.
PAGER=cat

Shift-selection and emacs 24

I use emacs 24 (OSX) and have a problem with shift-selection.
If I select a region with my mouse, the text is highlighted and automaticaly saved on to the kill ring (I can yank it immediately).
With shift-selection, the text is highlighted but I have to manualy save the text (M-w) to be able to yank it.
Is there any way that shift-selection hightlights the text AND saves it onto the kill ring ?
Selecting text with the mouse does not place anything onto the kill ring by default (it copies it to the X clipboard).
There are various ways in which you can customise how Emacs interacts with the clipboard. See:
C-hig (emacs) Cut and Paste RET
Is there any way that shift-selection hightlights the text AND saves it onto the kill ring ?
I'm not sure that there's any way to detect that you've stopped selecting things, but you could perhaps advise handle-shift-selection to set a temporary post-command-hook to run a custom function to place the region in the clipboard (and remove the post-command-hook).
Edit: Hang on, what you describe is exactly what happens for me in Emacs 24.1 on Ubuntu.
If I shift-select some text in Emacs and then middle-click the mouse in any application, I paste the selected text.
Try testing when running emacs -Q
Edit 2: Ah, but you didn't mean the mouse, did you?
How about this?
(defvar my-shift-selection-in-progress nil)
(make-variable-buffer-local 'my-shift-selection-in-progress)
(defadvice handle-shift-selection
(before my-shift-selection-to-kill-ring-advice)
"Automatically copy shift-selected text to the kill ring.
If `interprogram-cut-function' is non-nil, also save the text for a window
system cut and paste.
See `my-shift-selection-to-kill-ring'."
(when (and shift-select-mode
this-command-keys-shift-translated
(not my-shift-selection-in-progress))
(add-hook 'post-command-hook 'my-shift-selection-to-kill-ring nil t)))
(ad-activate 'handle-shift-selection)
(defun my-shift-selection-to-kill-ring ()
"Post-command callback for my-shift-selection-to-kill-ring-advice."
(if this-command-keys-shift-translated
(kill-new (filter-buffer-substring (region-beginning) (region-end))
my-shift-selection-in-progress)
(remove-hook 'post-command-hook 'my-shift-selection-to-kill-ring t))
(setq my-shift-selection-in-progress this-command-keys-shift-translated))
The problem with "auto-kill from shift-selection" is to determine when the shift-selection ends (at which point you can finally do the kill).

Can I make emacs grep windows just use the other window to open files in?

I've got emacs in front of me.
I've run a find-grep, and it's got many hits, which are displayed in a window. The file names are displayed in green as hyperlinks.
I make that the only window, with C-x 1.
If I click on a file name, the window splits vertically, and the file with the found text is displayed in the other window.
If I click on further filenames, then the new file replaces the old file, which is what I want to happen.
So far, so good...
However if I resize the windows, then emacs will periodically (when I click) split one of the two windows again, rendering the display difficult to read. It will then cycle opening new files between the two new windows. Occasionally it will open more windows and make the situation worse. If I close any of these new windows they just get reopened again.
In fact sometimes this perverse behaviour happens even if I don't resize anything. It just seems to happen more often if I do.
I would like emacs to stop buggering around and just have one find-grep window and one 'display' window, and always replace the display window with the new file. I would also like to be able to set these windows to the sizes that seem most convenient.
Is there any way to achieve this?
Or can anyone point me to an essay on how the whole (replace the contents of this window/replace the contents of a different window/create another window by splitting) thing works, so I can go and hack it sane.
Short fix:
Try doing this
(setq split-height-threshold nil
split-width-threshold nil)
This will prevent Emacs from splitting windows automatically (horizontally or vertically). This might be undesirable in other situations, but this should do the job. Try it for a week or so and see if it disrupts your flow.
Also, I found that if the point was in one of the windows, and I clicked on a link, the file opened up in the next window (if any).
So, if you want to make the file open in the right window (when you have more than one window), you can ensure that the point is in the window before the window you want.
Longer answer:
OK. I was able to reproduce the problem. The thing is the window showing the files is pretty big (wide or tall) because you resized it and Emacs sees that the width or height is greater than the respective threshold and splits it likewise. So, we have to either make the threshold higher or disallow the behaviour completely.
And, just to answer the last few questions:
To get current window - (selected-window)
To get next window - (next-window)
To select a window - (select-window foo-window)
To get the buffer of the current window - (current-buffer)
To get the buffer of some window - (window-buffer foo-window)
To set a buffer for a window - (set-window-buffer foo-window bar-buffer)
I'm sure you can hack together decent window/buffer management functions using these functions.
You can use C-h f to get more details on each of these functions.
Also check out the Elisp manual - http://www.chemie.fu-berlin.de/chemnet/use/info/elisp/elisp_26.html

Emacs M-x commands for invoking "GUI-style" menus

Question: How could I find out the M-x equivalent commands for doing GUI-based operations in Emacs, in those cases where my Emacs-variant uses OS-specific desktop functionality?
Background: Conventional understanding states that everything in Emacs is a command, and that commands can be invoked via M-x, as long as you know the name of the command. Assuming this statement is correct, what is the way to find the name of the commands used to trigger the "GUI-style" menus in a "desktop" based Emacs variant?
For example, if I were to mouse-select the File menu to open a file, the OS-specific "GUI" style file-open dialog pops up, waiting for my input.
How could I find out the M-x equivalent command for doing the exact same thing?
I thought that describe-key would tell me what I needed to know, but it's indication to use:
M-x menu-find-file-existing
doesn't invoke the "GUI" style file-open dialog. Instead, it uses the Emacs internal non-GUI-OS-neutral variant.
You need to trick Emacs into thinking that the keyboard was not being used, which is not as intuitive as tricking it into thinking that the mouse was used. :)
(defadvice find-file-read-args (around find-file-read-args-always-use-dialog-box act)
"Simulate invoking menu item as if by the mouse; see `use-dialog-box'."
(let ((last-nonmenu-event nil))
ad-do-it))
Tested on Emacs 22.2.1 on WinXP. I believe the paradigm has been around for a while, though, so it should work on older Emacs. No clue if XEmacs works similarly or not.
Wow, I'm glad you asked that. I've been meaning for a while to look it up myself.
C-h k followed by the menu choice will tell you this. Here, for example, is what you get from choosing menu/edit/paste:
<menu-bar> <edit> <paste> runs the command clipboard-yank
which is an interactive compiled Lisp function in `menu-bar.el'.
It is bound to <paste>, <f18>, <menu-bar> <edit> <paste>.
(clipboard-yank)
Insert the clipboard contents, or the last stretch of killed text.
If you want the details, follow the link for menu-bar-el to the LISP source:
(defun menu-find-file-existing ()
"Edit the existing file FILENAME."
(interactive)
(let* ((mustmatch (not (and (fboundp 'x-uses-old-gtk-dialog)
(x-uses-old-gtk-dialog))))
(filename (car (find-file-read-args "Find file: " mustmatch))))
(if mustmatch
(find-file-existing filename)
(find-file filename))))

Resources