objdump from inside emacs - emacs

Assuming I have my working folder as ~/X/ and the binaries after compiling are in ~/X/bin/
I usually first get the objdump like so:
objdump -D ~/X/bin/bin1 > bin1.list
then emacs bin1.list&
How is it possible to assign a function in .emacs to open a temporary buffer in Emacs and execute objdump -D in on the binary in it directly?

Sure, by default the command M-! (shell-command) outputs shell output to a default output buffer.
M-! objdump -D /bin/ls
If you want to change the name of the output buffer, you can invoke shell-command through some elisp:
(shell-command "objdump -D /bin/ls" "ls.dump")
or
C-x C-w (write-file) - to write the shell output buffer to a file
Edit:
Example which grabs file at point if available, or prompts the user
(defun my-objdump-file (&optional file)
(interactive)
(unless file
(setq file (expand-file-name (or (thing-at-point 'filename)
(read-file-name "File: ")))))
(when file
(shell-command (concat "objdump -D "
file)
"*Objdump Output*")))

Related

How to customize dired-compress-files-alist?

The original value of dired-compress-files-alist is:
(("\\.tar\\.gz\\'" . "tar -cf - %i | gzip -c9 > %o") ("\\.tar\\.bz2\\'" . "tar -cf - %i | bzip2 -c9 > %o") ("\\.tar\\.xz\\'" . "tar -cf - %i | xz -c9 > %o") ("\\.tar\\.zst\\'" . "tar -cf - %i | zstd -19 -o %o") ("\\.zip\\'" . "zip %o -r --filesync %i"))
I'd like to add command for .tgz files, so I add a line in .emacs:
(add-hook 'dired-mode
'(lambda ()
(setq dired-compress-files-alist
(cons '("\\.tgz\\'" . "tar -cf - %i | gzip -c9 > %o") dired-compress-files-alist))))
When Emacs starts, it reports error variable dired-compress-files-alist is not found. I found only when I run command `dired-do-compress-to', this variable will appear. So how to set this variable?
Just require standard library dired-aux in your init file at the outset (at least before you use Dired). That library defines variable dired-compress-files-alist.
(require 'dired-aux)
(Oh, and don't quote lambdas: just use (lambda...), not '(lambda...).)
(You can also use add-to-list or push instead of setq with cons etc., but that's just a question of style.)

How to copy Zsh aliases to Eshell

I'm trying to copy using the command provided in here. That is,
alias | sed -E "s/^alias ([^=]+)='(.*)'$/alias \1 \2 \$*/g; s/'\\\''/'/g;" >~/.emacs.d/eshell/alias
This worked with Bash, I was using Emacs-Starter-Kit; but not working with Zsh -- not working means it copied things but to no effect.
[As a side note]
It seems like, I don't have few Eshell default variables i.e. eshell-read-aliases-list, and eshell-aliases-file. So, I even don't know where should my Eshell alias file reside.
Got it working after setting
(setq eshell-directory-name (expand-file-name "./" (expand-file-name "eshell" prelude-personal-dir)))
in post.el (my personal .el file for post-processing) under prelude/personal
... and modified the given bash command to
alias | awk '{print "alias "$0}' | sed -E "s/^alias ([^=]+)='(.*)'$/alias \1 \2 \$*/g; s/'\\\''/'/g;" > ~/.emacs.d/personal/eshell/alias
... and appended that to .zshrc.
Found that alias command, in zsh, prints aliases without prefix alias<space>, unlike bash. Therefore this part
| awk '{print "alias "$0}'

I want to replace `ls` with `ls -l` in eshell

I use eshell-parse-command 'ls -l' and I got this:
(progn (eshell-trap-errors
(eshell-named-command "ls" (list "-l"))))
Then I write a alias in my .emacs file:
(defalias 'ls
(progn ...))
But I doesn't work. I don't know why.
Add following code to your configuration file.
(require 'em-alias)
(add-to-list 'eshell-command-aliases-list (list "ls" "ls -l"))
The easiest way to add alias to eshell is:
Open eshell,
alias alias-name definition
Eshell will automatically write it into ~/emacs.d/eshell/alias (don't edit it yourself).
For example:
alias sau sudo aptitude update
Then you can type sau to launch sudo aptitude update.
Type alias (in eshll, of course) will list all the alias you've defined.
Some useful alias:
Map find-file to ff, then you can open a file in emacs with ff file:
alias ff 'find-file $1'
Map dired to d:
alias d 'dired $1'
Resources:
Mastering Eshell
http://www.masteringemacs.org/article/complete-guide-mastering-eshell

How do I provide a command-line option to emacsclient?

I start emacsclient using:
emacsclient -a "" -c
This opens a frame connected to the emacs daemon, and starts the daemon if it's not already started. Great, this works fine.
However, I like opening my emacs frames maximized. With emacs, I would use -mm. However, that doesn't work with emacsclient. How do I make this work?
(It seems I could make something work by adding a shell file like so: emacsclient -a "myshell.sh" -c, where the shell file is: emacs -mm, but I haven't been able to make that work - the server doesn't stay up.)
You can add the following line to .emacs, so that Emacs can be started with the window maximized. See http://www.gnu.org/software/emacs/manual/html_node/elisp/Size-Parameters.html#Size-Parameters for details.
(add-to-list 'default-frame-alist '(fullscreen . maximized))
Emacs client accepts -F option, where you can specify frame parameters, so the above example would be:
emacsclient -c -a "" -F "((fullscreen . maximized))"
Let's say you want to run emacsclient fullscreen, which was my case.
man emacsclient shows emacsclient has -F option:
-F, --frame-parameters=ALIST
set the parameters of a newly-created frame.
In Emacs Manual, which is an info file, section (emacs) emacsclient Options has more information. Specifically for this question (elisp) Size Parameters mentions fullscreen parameter. To run emacsclient fullscreen, you need to supply an alist, with one element being (fullscreen . fullboth) like that:
emacsclient -c -F "((fullscreen . fullboth))"
emacsclient provides the --eval (-e for short) command line option for executing arbitrary Emacs Lisp code, so you can visit a file and call suspend-frame from the command line like so:
emacsclient -a "" -c --eval "(progn (find-file \"/tmp/my-file\") (suspend-frame))"
You could put this in a script, e.g:
#!/bin/bash
emacsclient -a "" -c --eval "(progn (find-file \"$1\") (suspend-frame))"

Emacs - connect to daemon (if it exists) without using emacsclient

If I have emacs running as a daemon on my system, I can connect to it easily using emacsclient. This I know. However, what I would like to know is, is there a way to tell emacs (not emacsclient) to behave like emacsclient if a daemon is already running?
e.g.
# emacs daemon is not running
emacs # should start a new frame
# ...
# emacs daemon IS running
emacs # should actually behave like emacsclient, i.e. connect to my daemon
Is there anything I can do to my init.el to replicate this kind of behaviour?
I don't think so, but can you achieve a similar effect by using emacsclient with an empty string as the the --alternate-editor option? From http://www.gnu.org/s/libtool/manual/emacs/emacsclient-Options.html#emacsclient-Options:
-a command
--alternate-editor=command
.
.
.
As a special exception, if command is the empty string, then emacsclient starts Emacs in daemon mode and then tries connecting again.
You can do the -a '' thing with emacsclient but what I do and a lot of people do is to have some kind of script that basically does what emacsclient '' does in multiple steps.
My version is something like this BASH script: The part you are interested in is the ensure-server-is-running function. This is the "main function" of the script, what follows is the ensure-server-is-running function and the rest is there after that for your curiosity but does not contribute to answering the question.
#!/bin/bash
# ec.sh
#
# [function definitions]
#
ensure-server-is-running
ensure-frame-exists
focus-current-frame
Ensuring that the server is running
# ec.sh function definition
# From https://emacs.stackexchange.com/a/12896/19972
function server-is-running() {
emacsclient -e '(+ 1 0)' > /dev/null 2>&1
}
function ensure-server-is-running(){
if ! server-is-running ; then
echo "Need to start daemon, press enter to continue, C-c to abort"
read
emacs --daemon
fi
}
And the other two function:
# ec.sh function definition
# From https://superuser.com/a/862809
function frame-exists() {
emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" 2>/dev/null | grep -v nil >/dev/null 2>&1
}
function ensure-frame-exists() {
if ! frame-exists ; then
emacsclient -c --no-wait
fi
}
# From https://emacs.stackexchange.com/a/54139/19972
function focus-current-frame() {
# Doesn't work a frame exists and is in a terminal
emacsclient --eval "(progn (select-frame-set-input-focus (selected-frame)))"
}
focus-current-frame is something that will make the OS put you in the current Emacs Frame. This is the most important feature. For me I insert an adapted version of this into a MacOS Automator app. When there is an emacs GUI frame, doing Spotlight Search "EmacsC" (usually just typing the "e" is enough), puts me in my emacs window. It's a super fast way of switching to an emacs window.
Here's what I do. It's like the solution by #philippe-carpin, in the sense that it's a script that does multiple steps:
If emacs daemon is not running: start it, create a frame
If daemon but no frame: create a frame
If daemon with frame: focus that frame
In all cases you can pass filename(s) that will be opened.
In addition, my script only tries to run a piece of elisp just once and if there's no daemon it will not start an emacs process to run that elisp. So it should be slightly faster.
get_emacs_daemon_state () {
emacs_get_state_script='(if (> (length (frame-list)) 1) "daemon-with-frame" "daemon-no-frame")'
emacsclient -e "$emacs_get_state_script" -a "echo no-daemon" 2>/dev/null |
tr -d \" | cut -d' ' -f1
}
state=$(get_emacs_daemon_state)
create_frame_arg=""
if [[ $state = no-daemon ]]; then
emacs --daemon
fi
if [[ $state != daemon-with-frame ]]; then
create_frame_arg="--create-frame"
fi
client="emacsclient --no-wait $create_frame_arg"
if [[ $# -gt 0 ]]; then
# open files passed as arguments
$client "$#"
else
# if no file passed, we just focus the frame
$client --eval "(select-frame-set-input-focus (selected-frame))" >/dev/null
fi