Emacs open directory - emacs

Emacs allows opening a directory and choosing files. After going one step up or down in the directory, the control moves to another split window. My problem how to make the control in same window?
Given this also how to open files in specific split windows?

If you are using dired, you can use ^ to include an upward directory and i to include a subdirectory. Jump between directory entries with < and >.

In dired-mode, you can press i (dired-maybe-insert-subdir) while point is over a directory to insert that directory's contents into the same buffer (rather than opening a new buffer).
In a buffer with multiple directories open, press C-M-p (dired-prev-subdir) while point is in an opened subdirectory to jump up to previous directories in the buffer.
You mentioned you wanted to insert subdirectories into the dired buffer with a right click. Here's some code to do that:
(add-hook 'dired-mode-hook
(lambda ()
(local-set-key [mouse-3]
(lambda (click)
(interactive "e")
(goto-char (posn-point (event-start click)))
(call-interactively #'dired-maybe-insert-subdir)))))

Related

How to exclude specific extension file or directory be opened another application by org-open-at-point(C-c C-o) in Emacs?

My org file is like following.
In org-mode when I use org-open-at-point(C-c C-o), another directory application(Finder in mac) will be opened. But I want to open directory ./ in Emacs dired by org-open-at-point(C-c C-o). C-u C-c C-o will do this. but I don't want to type C-u.
my.org
sample dir is [[./][here]]
sample memo is [[file:sample.memo][here]] <-- this won't be opened by C-c C-o,
because there is no application
to open *.memo file.
Thanks.
It's not 100% clear what the problem is. If you're on [[./][here]], C-c C-o should open the dired buffer automatically, and if on [[file:sample.memo][here]] it should open (or create) the file automatically.
If your goal is to FORCE org to visit a file in Emacs (overriding whatever you have set in org-file-apps), you pass the prefix argument to C-c C-o (ie, as C-u C-c C-o). If, as you mentioned, you don't want to prepend the C-u, you can write a little function as below and bind it to the keys of your choice. All it's doing is setting the the optional argument programmatically:
(defun ooap-force-emacs ()
"Visit a file in emacs from an org-mode buffer."
(interactive)
(org-open-at-point t))
You can use:
(add-hook 'org-mode-hook
'(lambda ()
(add-to-list 'org-file-apps '(directory . emacs) t)))
to open directories using Emacs instead of system default.
See:
M-x customize-variable [RET] org-file-apps

OSX -- How to define save-file-as that opens a directory tree window

I need some assistance, please, defining a save-as function that opens a directory tree instead of just giving me a path in the status bar . . . Specifically, ns-popup-save-panel is not recognized by the current version of emacs. I am looking for something similar to write-file filename &optional confirm. I have tried different combinations, but I still cannot get the pop-up menu save-file directory tree. I already have the keyboard shortcut figured out. Emacs complains of void variables when I try to insert write-file filename &optional confirm. Just plain old write-file or save-buffer doesn't open the directory listing.
(defun mac-key-save-file-as (&optional filename)
"Save buffer to a file, selecting file by dialog.
Displays sheet. File is saved once user has dismissed sheet."
(interactive)
(ns-popup-save-panel "Select File to Save Buffer" default-directory (if buffer-file-name (file-name-nondirectory buffer-file-name) "Untitled"))
)
M-x ns RET
This brings up a list of all commands begging with ns-
The code cited in the question is from Aquamacs 2.4.
The newest version of Emacs (24.3) built from source uses different names. In this particular case, the new name is ns-write-file-using-panel.

In Emacs, is there a command to visit the most recently opened file?

In Emacs, is there a command to open the most recently opened file? Like a visit-most-recent-file?
Note that I don't want to view a LIST of recent files and then select one. I want to automatically visit the most recently opened one.
It would be great to visit a recently-opened file (that's no longer in a current buffer) with a single keystroke. And then be able to visit the next most recently opened file by invoking the keystroke again, and so on.
So that by pressing that keystroke four times (e.g. A-UP), I could automatically open the top four files on my recent files list.
I can kind of sort of do this already by browsing through recent files, by pressing C-x C-f UP RET, but it would be cool to do it in a single keystroke (e.g. A-UP).
Building off of pokita's answer,
(defun visit-most-recent-file ()
"Visits the most recently open file in `recentf-list' that is not already being visited."
(interactive)
(let ((buffer-file-name-list (mapcar 'buffer-file-name (buffer-list)))
most-recent-filename)
(dolist (filename recentf-list)
(unless (memq filename buffer-file-name-list)
(setq most-recent-filename filename)
(return)))
(find-file most-recent-filename)))
I haven't tested this very much.
You can do this by using the recentf package:
(require 'recentf)
(defvar my-recentf-counter 0)
(global-set-key (kbd "<M-up>")
(lambda ()
(interactive)
(setq my-recentf-counter (1+ my-recentf-counter))
(recentf-open-most-recent-file my-recentf-counter)))
You will probably face the problem that there are files in the "recent" list which you are not interested in (temporary stuff that was saved upon exiting Emacs). Use the recentf-exclude variable to exclude those files from the list of recently opened files.

How to make emacs stay in the current directory

When I start working on a project in emacs, I use M-x cd to get into the project root directory. But every time I use C-x C-f to open a file in one of the subdirectories (like app/model/Store.rb) emacs changes current directory to that of the file. Is there a way to make emacs stay at the root?
How about this? It replaces the regular find-file command with your own which always starts in some "root" directory (customize the find-file-root-dir variable):
(defvar find-file-root-dir "~/"
"Directory from which to start all find-file's")
(defun find-file-in-root ()
"Make find-file always start at some root directory."
(interactive)
(let ((default-directory find-file-root-dir))
(call-interactively 'find-file)))
(global-set-key (kbd "C-x C-f") 'find-file-in-root)
Assuming that you want the working directory of a file to be set to whatever the working directory was before you executed find-file, you could try the following:
(defmacro disallow-cd-in-function (fun)
"Prevent FUN (or any function that FUN calls) from changing directory."
`(defadvice ,fun (around dissallow-cd activate)
(let ((old-dir default-directory) ; Save old directory
(new-buf ad-do-it)) ; Capture new buffer
;; If FUN returns a buffer, operate in that buffer in addition
;; to current one.
(when (bufferp new-buf)
(set-buffer new-buf)
(setq default-directory old-dir))
;; Set default-directory in the current buffer
(setq default-directory old-dir))))
Armed with this macro, go search for operations that set the variable default-directory: M-x find-library files; M-x occur (setq default-directory. After some investigation, you discover that the desired function is called find-file-noselect-1. Also, it looks like set-visited-file-name is also a candidate. So:
(disallow-cd-in-function find-file-noselect-1)
(disallow-cd-in-function set-visited-file-name)
Note
Note that (disallow-cd-in-function find-file) would work just fine, but then if you switched to ido-mode, you'd be opening files with ido-find-file instead of find-file. Both of these functions ultimately use find-file-noselect-1, so hitting that with the macro is a more univeral solution.
Is there a way to make emacs stay at the root?
No, there isn't. C-x C-f always visits starting from the default directory of the buffer you are already vising. The default directory, by default, is the same directory as the file. You can change these (separately for every buffer) using M-x cd.
But that is not what you want. What you should do is C-x b to *scratch* (whose default directory is the same as where you launched Emacs from -- in your words "root"), and then visit a new file. And if you need to do this frequently, just open up a dired in there and work your way thru.
I appreciate I'm not answering your question directly, but I noticed you were more specific in your requirements in one of your comments: "I don't use compile or recompile, I just tend to close files I am not working on, since it takes fewer keystrokes to open a file again".
Have you got ido turned on for buffer switching? If you exclude the directory thing for a moment, switching files or buffers with ido is an identical number of keystrokes (C-x C-f vs C-x b, followed by a few characters in the file name). If you include the directory thing, switching files is more tricky for the precisely the reasons you mention. Sticking with buffers is much easier.
Going a step further, with the help of 'anything.el' it's quite easy to abstract away whether a given file is in a buffer or in a file using the file cache. For example, if you do the following:
(file-cache-add-directory-recursively "/my/ruby/project") ".*\\.rb$")
and run 'anything-for-files' (I have it bound to C-x f) all your open buffers are listed, along with all of the files you've just added to the file cache; isolating a given file usually only takes one or two more characters.
Any file in your project is thus 4 or 5 key presses away, and the directory they are in or whether or not they are in a buffer becomes irrelevant.
Hope that's helpful...
Sorry I haven't worked out the details, but you might be able to add a function to find-file-hook that resets the default directory to whatever you want.

How do I get list of recent files in GNU Emacs?

When I use Emacs I want to be able to easily display and navigate through a list of files I worked on from not just the current session but from previous sessions. (BTW, running Emacs 22.2 on Windows)
From Joe Grossberg's blog (no longer available):
But if you're using GNU Emacs 21.2
(the latest version, which includes
this as part of the standard distro),
you can just put the following lines
into your .emacs file
;; recentf stuff
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 25)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
Then, when you launch emacs, hit
CTRL-X CTRL-R. It will show a list of
the recently-opened files in a buffer.
Move the cursor to a line and press
ENTER. That will open the file in
question, and move it to the top of
your recent-file list.
(Note: Emacs records file names.
Therefore, if you move or rename a
file outside of Emacs, it won't
automatically update the list. You'll
have to open the renamed file with the
normal CTRL-X CTRL-F method.)
Jayakrishnan Varnam has a page
including screenshots of how this
package works.
Note: You don't need the (require 'recentf) line.
Even if you don't have recentf turned on, Emacs is saving a list of files entered via the minibuffer in the variable file-name-history. Also, executing (savehist-mode 1) in your .emacs file makes that variable persist across invocations of Emacs.
So here's a little function that displays the files that actually exist from that list (anyone is welcome to use/build on this):
(defun dir-of-recent-files ()
"See the list of recently entered files in a Dired buffer."
(interactive)
(dired (cons
"*Recent Files*"
(seq-filter
'file-exists-p
(delete-dups
(mapcar (lambda (s) (string-trim-right s "/*"))
file-name-history)
))))
)
I find this quite useful and have it bound to one of those little special function keys on my desktop keyboard. (And so I have not seen the point of turning on recentf...)