Show arguments in title/status bar of Emacs - emacs

Is there a way to show Emacs command line arguments in status or title bar? Basically I start emacs like "emacs someproject/file.txt", and I want it to show "someproject/file.txt" somewhere in the window.

The command-line-args variable may be sufficient, but non-file arguments which are processed by Emacs are also deleted from that variable, so it won't include those.
Try this:
(setq frame-title-format '(:eval (mapconcat 'identity (cdr command-line-args) " ")))

The filename for the current buffer should be displayed in the modeline, which is what you should see if you're starting emacs with a file at the command line. If that's not what you're seeing then you should provide more context so we can help you. What configuration/customization have you done? Maybe include a screenshot of what you do see.

Related

Add enabling Relative Line Number to init.el

In Emacs 26.x, How do I get Emacs to start with Relative Line Numbers turned on by default ?
I tried to use C-x h, then clicked the menu item and the help showed the following
<menu-bar> <options> <showhide> <display-line-numbers> <relative>
runs the command #[nil "\300\301!\210\302\303!\207"
[menu-bar-display-line-numbers-mode relative message "Relative line
numbers enabled"] 2 nil nil] (found in global-map), which is an
interactive compiled Lisp function.
So tried adding the command into init.el as
(menu-bar-display-line-numbers-mode relative message "Relative line
numbers enabled")
How do I make this work ?
Unfortunately, Emacs's help message is pretty bad in this case. The menu button is bound to an anonymous function, and the help system is basically displaying the byte-compiled version of that function. I got the Emacs source, searched for the unique looking string "Relative line numbers enabled", and found the function in lisp/menu-bar.el:
(lambda ()
(interactive)
(menu-bar-display-line-numbers-mode 'relative)
(message "Relative line numbers enabled"))
So you can use menu-bar-display-line-numbers-mode, which takes only one argument, to set it:
(menu-bar-display-line-numbers-mode 'relative)
The canonical way to set this is adding display-line-numbers-mode to a mode hook,
(add-hook 'foo-mode-hook #'display-line-numbers-mode)
or enabling global-display-line-numbers-mode if you want them everywhere,
(global-display-line-numbers-mode 1)
and to set display-line-numbers-type to the desired style:
(setq display-line-numbers-type 'relative)

Tab indentation in CoffeeScript using Emacs

I hope this is within the reasonable scope of this site and not too trivial, this is my first post. I am new to Emacs and am trying to set up the environment so that when I start a new line in coffee-mode the automatic indentation is in the form of tabs. As I understood the documentation of coffee-mode all I need to do is set coffee-indent-tabs-mode to t. I've appended my init file with the code below:
(custom-set-variables
'(coffee-tab-width 2)
'(coffee-indent-tabs-mode t))
However when I start Emacs and open a .coffee file, though it gets the tab width right, when I press enter it indents with spaces. Quibbles about whether I need to indent with tabs aside, what am I doing wrong?
In the coffee-mode I find in GNU ELPA, there is no coffee-indent-tabs-mode. I recommend you simply do:
(add-hook 'coffee-mode-hook
(lambda ()
(set (make-local-variable 'tab-width) 2)
(set (make-local-variable 'indent-tabs-mode) t)))
This should work for pretty much any major mode.

How can I show the Org-mode agenda on Emacs start-up?

I would like the Org-mode agenda to automatically show what I have to do today when I open Emacs. The org-agenda command is interactive, so it doesn't seem to work well for this purpose.
Is there a way to show the Org-mode agenda on Emacs start-up?
Thanks,
Conor
You can use after-init-hook to run a piece of code after initialization has finished. To run (org-agenda-list) after init, use:
(add-hook 'after-init-hook 'org-agenda-list)
This works for me (in .emacs):
(setq inhibit-splash-screen t)
(org-agenda-list)
(delete-other-windows)
Without the first line, the splash screen "covered" the agenda; without the third one, the scratch buffer remained visible.
One alternative to the hook is to set the initial-buffer-choice variable. This is particularly useful if there are multiple buffers or a number of functions on the hook. The function on this variable needs to return a buffer. Naively this might be:
(setq initial-buffer-choice (lambda ()
(org-agenda-list 1)
(get-buffer "*Org Agenda*")))
Try (org-agenda-list). If you just want today, (org-agenda-list 1).
And of course, apropos is your friend. C-h C-a org-agenda (or whatever command) will show you useful info on that command.
I have a bash alias to start emacs with the Agenda open:
alias org='/usr/bin/emacs --funcall org-agenda-list &'
Enjoy.
It is not exactly at startup, but I keep Emacs running so I need a different approach
(require 'midnight)
(midnight-delay-set 'midnight-delay "7:30am")
(add-hook 'midnight-hook 'org-agenda-list)
Credits to https://stackoverflow.com/a/14947354/217408

Splitting window in Emacs Lisp function

I'd like to be able to run a shell command on the current file that I'm editing and have the output be shown in the Shell Command Output window. I've been able to define the
function which is shown below.
(defun cpp-check ()
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name))))
The only problem is that the output window isn't brought to the foreground in any way. What I'd like to happen is for the window to be split and the output window shown there. Also, am I on the right track here defining the function to be put in my .emacs file or is there a better way?
Any help would be appreciated. Thanks.
Take a look at the documentation for 'shell-command, this worked well for me:
(defun cpp-check ()
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name))
"cpp-check"))
It creates a new buffer named "cpp-check" and puts the results there. The current frame is split in to, and the "cpp-check" buffer is put in the other window.
See the function `pop-to-buffer'. I think.
You should be able to give it a buffer name to pop to -- just give in the Shell Command Output buffer.
This is what I came up with. Thanks for the responses. I defined a function that will go ahead and run cpp-check. I wanted it bound to a key in c-mode so I add it as a hook. I ran into the difference between normal functions and ones that can be bound to keymaps so I had to make the function interactive. This article helped explain that. So now when the shortcut is pressed the results come up in another window, but the cursor remains in the original buffer, which is what I want. The only problem is that the output is shown in the minibuffer as well which isn't quite what I want. Any thoughts on fixing that little detail?
(defun cpp-check ()
(interactive)
"Run cpp-check on current file the buffer is visiting."
(shell-command
(concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
(buffer-file-name)))
(display-buffer "*Shell Command Output*"))
(add-hook 'c-mode-common-hook
(lambda ()
(define-key c-mode-base-map
"\C-x\p" 'cpp-check)))
splitting the window is (split-window-vertically) It has an optional arg of the size of the (top if positive, bottom if negative) part of the window.
Then, and you need to do is bring the shell results buffer to the front with switch-to-buffer or switch-to-buffer-other-window.
Remember that when you spit the window (frame) in emacs, you end up with two "windows" because of a naming confusing back in the day that it's too late to deal with now...

goto-file in Emacs

Is there a substitute in emacs for the vi "gf" command?
meaning try to open the file which is under the cursor right now
if a real file name is in fact there.
Thanks
You want the find-file-at-point function (which is also aliased to ffap). It's not bound to a key by default, but you can use
M-x ffap
Or, you can put in your .emacs file:
(ffap-bindings)
This will replace many of the normal find-file key bindings (like C-x C-f) with ffap-based versions. See the commentary in ffap.el for details.
Thanks, it works quite well but somehow the vi (gf) version is
still somewhat smarter. I think it looks at some path variable for search paths.
I made something which is needlessly complicated but works for me (only in linux).
It uses the "locate" command to search for the path under the cursor.
I guess it could be made smarter by searching the relative path to the current file first.
sorry for my bad elisp skills...It can probably be achieved in a much nicer way.
put in your .emacs, then use with M-x goto-file
(defun shell-command-to-string (command)
"Execute shell command COMMAND and return its output as a string."
(with-output-to-string
(with-current-buffer standard-output
(call-process shell-file-name nil t nil shell-command-switch command))))
(defun goto-file ()
"open file under cursor"
(interactive)
(find-file (shell-command-to-string (concat "locate " (current-word) "|head -c -1" )) ))