Emacs lisp: Concise way to get `directory-files` without "." and ".."? - file

The function directory-files returns the . and .. entries as well. While in a sense it is true, that only this way the function returns all existing entries, I have yet to see a use for including these. On the other hand, every time a use directory-files I also write something like
(unless (string-match-p "^\\.\\.?$" ...
or for better efficiency
(unless (or (string= "." entry)
(string= ".." entry))
..)
Particularly in interactive use (M-:) the extra code is undesirable.
Is there some predefined function that returns only actual subentries of a directory efficiently?

You can do this as part of the original function call.
(directory-files DIRECTORY &optional FULL MATCH NOSORT)
If MATCH is non-nil, mention only file names that match the regexp MATCH.
so:
(directory-files (expand-file-name "~/") nil "^\\([^.]\\|\\.[^.]\\|\\.\\..\\)")
or:
(defun my-directory-files (directory &optional full nosort)
"Like `directory-files' with MATCH hard-coded to exclude \".\" and \"..\"."
(directory-files directory full "^\\([^.]\\|\\.[^.]\\|\\.\\..\\)" nosort))
although something more akin to your own approach might make for a more efficient wrapper, really.
(defun my-directory-files (directory &optional full match nosort)
"Like `directory-files', but excluding \".\" and \"..\"."
(delete "." (delete ".." (directory-files directory full match nosort))))
although that's processing the list twice, and we know there's only one instance of each of the names we wish to exclude (and there's a fair chance they'll appear first), so something more like this might be a good solution if you're expecting to deal with large directories on a frequent basis:
(defun my-directory-files (directory &optional full match nosort)
"Like `directory-files', but excluding \".\" and \"..\"."
(let* ((files (cons nil (directory-files directory full match nosort)))
(parent files)
(current (cdr files))
(exclude (list "." ".."))
(file nil))
(while (and current exclude)
(setq file (car current))
(if (not (member file exclude))
(setq parent current)
(setcdr parent (cdr current))
(setq exclude (delete file exclude)))
(setq current (cdr current)))
(cdr files)))

If you use f.el, a convenient file and directory manipulation library, you only need function f-entries.
However, if you don't want to use this library for some reason and you are ok for a non-portable *nix solution, you can use ls command.
(defun my-directory-files (d)
(let* ((path (file-name-as-directory (expand-file-name d)))
(command (concat "ls -A1d " path "*")))
(split-string (shell-command-to-string command) "\n" t)))
The code above suffice, but for explanation read further.
Get rid of dots
According to man ls:
-A, --almost-all
do not list implied . and ..
With split-string that splits a string by whitespace, we can parse ls output:
(split-string (shell-command-to-string "ls -A"))
Spaces in filenames
The problem is that some filenames may contain spaces. split-string by default splits by regex in variable split-string-default-separators, which is "[ \f\t\n\r\v]+".
-1 list one file per line
-1 allows to delimit files by newline, to pass "\n" as a sole separator. You can wrap this in a function and use it with arbitrary directory.
(split-string (shell-command-to-string "ls -A1") "\n")
Recursion
But what if you want to recursively dive into subdirectories, returning files for future use? If you just change directory and issue ls, you'll get filenames without paths, so Emacs wouldn't know where this files are located. One solution is to make ls always return absolute paths. According to man ls:
-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
If you pass absolute path to directory with a wildcard and -d option, then you'll get a list of absolute paths of immediate files and subdirectories, according to How can I list files with their absolute path in linux?. For explanation on path construction see In Elisp, how to get path string with slash properly inserted?.
(let ((path (file-name-as-directory (expand-file-name d))))
(split-srting (shell-command-to-string (concat "ls -A1d " path "*")) "\n"))
Omit null string
Unix commands have to add a trailing whitespace to output, so that prompt is on the new line. Otherwise instead of:
user#host$ ls
somefile.txt
user#host$
there would be:
user#host$ ls
somefile.txtuser#host$
When you pass custom separators to split-string, it treats this newline as a line on its own. In general, this allows to correctly parse CSV files, where an empty line may be valid data. But with ls we end up with a null-string, that should be omitted by passing t as a third parameter to split-string.

How about just using remove-if?
(remove-if (lambda (x) (member x '("." "..")))
(directory-files path))

Related

Asynchronous copy-directory with the option to ignore subdirectory in Emacs-lisp?

I am using the code from the answer on this problem for asynchronous copy-directory for a few months now, but sometimes I need one or more subdirectories to be ignored. Is there an easy way by slightly modifying the code to do that?
I have tried to use Selective Directory Copying: SDC package from here, but it brakes when file or folder already exists.
This is the code I am using right now:
(async-start
`(lambda()
(copy-directory ,"~/Documents/data/" ,"~/Dropbox/data_backup/" t t t)
,"~/Documents/data/")
(lambda(return-path)
(message "Upload '%s' finished" return-path)))
There is a subdirectory in ~/Documents/data that sometimes I want it to be ignored because it is larger than a threshold.
copy-directory calls itself recursively. You can use cl-flet to redefine it locally, while keeping the original definition. You can also do this with advice (and actually this cl-flet technique seems to break advice), but then it's effectively globally redefining the function and you need to control it with e.g. variables.
(defun jpk/copy-directory (directory newname &optional keep-time parents copy-contents)
(cl-letf (((symbol-function 'orig/copy-directory) (symbol-function 'copy-directory))
((symbol-function 'copy-directory)
(lambda (directory newname &optional keep-time parents copy-contents)
(if (string= directory "/path/to/foo")
(message "skipping: %s" directory)
(orig/copy-directory directory newname keep-time parents copy-contents)))))
(copy-directory directory newname keep-time parents copy-contents)))
In more detail: store the original function to orig/copy-directory, replace the function copy-directory with a lambda that calls orig/copy-directory only if the directory name doesn't match some string, then call the new definition of copy-directory. The recursive call to copy-directory also uses the new definition. All of this is wrapped up in jpk/copy-directory. To make it more flexible, you could add a predicate argument to jpk/copy-directory so the test isn't hard coded.

Globbing in C, how to exclude files

I've read http://linux.die.net/man/3/glob and it seems that glob will do disk access, even though I don't want it to.
Is there a C glob function to compare a string with a glob pattern and tell me if it matches? i.e. no disk access.
If not, how can I use glob to exclude files when recursively (depth first) traversing a filesystem?
while((entry = readdir(dp))) {
// need to continue to next iteration of loop, here, if entry->d_name matches glob pattern
// do stuff and recurse
}
Use the fnmatch function. It compares a filename/path against a given pattern.

string-append to files in a directory with Scheme

Please note first of all that this is a homework question so I'm not looking for straight code or anything like that, just for someone to maybe help me with my logic.
The assignment is in DrRacket. The question asks:
Given a FileSystem, which we've defined as a structure with two fields, name and contents, where contents is a list of either directories or files; write a function that will create a ".bak" filename for every file in the directory and place it immediately after the file.
I am totally lost. My logic is as follows: If the first thing in the content list is a file, simply remake the directory with that file and a new file with ".bak" appended. This is as far as I can get - I can't see how to work things out if there's a subdirectory, OR how to go about moving further down the list.
Here's my atrocious code:
(define (backup my-fs)
(cond
[(empty? (dir-contents my-fs)) empty]
[(file? (first (dir-contents my-fs))) (make-dir (dir-name my-fs) (append (backup-list (first (dir-contents my-fs)))(rest (dir-contents my-fs))))]
[(dir? (first (dir-contents my-fs))) (backup (first (dir-contents my-fs)))]))
Can anyone help me reason this out?
The contents part of your FileSystem is a list containing files or directories (which are lists containing ....).
This is a basic tree-traversal problem where you have three cases, as you noted:
list is empty
first element in list is a file
first element in list is a directory
Then you need an action for each case:
done
keep that filename, create a new filename, and continue processing the rest of the list
keep that directory, recursing over it, and continue processing the rest of the list
For example:
(define (traverse contents)
(cond
[(empty? contents) ... nothing to do ...]
[(file? (first contents)) ;; if the first element's a file:
(cons (first contents) ;; keep the file
(cons (... make backup filename ... (first contents)) ;; make the backup
(traverse (rest contents))))] ;; and recurse on the rest
[(dir? (first contents) ;; if the first element's a directory:
(cons (traverse (first contents)) ;; recurse on the first
(traverse (rest contents)))])) ;; and also recurse on the rest
You need to clarify your data definition. You write:
"Given a FileSystem, which we've defined as a structure with two fields, name and contents, where contents is a list of either directories or files; write a function that will create a ".bak" filename for every file in the directory and place it immediately after the file. "
This makes it clear what a FileSystem is... if you know what "directories" and "files" are. You need to clarify this by writing data definitions for "directory" and "file". Each of these should be a separate sentence. They might be really simple, e.g. "A file is represented as a string".
After doing this, write some examples of FileSystems.

Emacs: Write buffer to new file, but keep this file open

I want to do the following in Emacs: Save the current buffer to a new file, but also keep the current file open.
When I do C-x C-w then current buffer gets replaced, but I want to keep open both buffer. Is this possible without reopening the original file?
I don't think there's anything built in, but it's easy enough to write:
(defun my-clone-and-open-file (filename)
"Clone the current buffer writing it into FILENAME and open it"
(interactive "FClone to file: ")
(save-restriction
(widen)
(write-region (point-min) (point-max) filename nil nil nil 'confirm))
(find-file-noselect filename))
Here's a snippet I've had for a while to do just this
;;;======================================================================
;;; provide save-as functionality without renaming the current buffer
(defun save-as (new-filename)
(interactive "FFilename:")
(write-region (point-min) (point-max) new-filename)
(find-file-noselect new-filename))
I found it helpful to combine Scott's and Chris's answers above. The user can call save-as and then answer "y" or "n" when prompted whether to switch to the new file. (Alternatively, the user can select the desired functionality via the function name save-as-and-switch or save-as-but-do-not-switch, but this requires more keystrokes. Those names are still available for being called by other functions in the future, however.)
;; based on scottfrazer's code
(defun save-as-and-switch (filename)
"Clone the current buffer and switch to the clone"
(interactive "FCopy and switch to file: ")
(save-restriction
(widen)
(write-region (point-min) (point-max) filename nil nil nil 'confirm))
(find-file filename))
;; based on Chris McMahan's code
(defun save-as-but-do-not-switch (filename)
"Clone the current buffer but don't switch to the clone"
(interactive "FCopy (without switching) to file:")
(write-region (point-min) (point-max) filename)
(find-file-noselect filename))
;; My own function for combining the two above.
(defun save-as (filename)
"Prompt user whether to switch to the clone."
(interactive "FCopy to file: ")
(if (y-or-n-p "Switch to new file?")
(save-as-and-switch filename)
(save-as-but-do-not-switch filename)))
C-x h
selects all the buffer, then
M-x write-region
writes the region (whole buffer in this example) to another file.
Edit: this function does what you need
(defun write-and-open ( filename )
(interactive "GClone to file:")
(progn
(write-region (point-min) (point-max) filename )
(find-file filename ))
)
It's a bit crude, but modify to your will.
The interactive code 'G' prompts for a filename which goes into the 'filename' argument.
Drop this into your .emacs and call with M-x write-and-open (or define a key sequence).

How do I create an empty file in emacs?

How can I create an empty file from emacs, ideally from within a dired buffer?
For example, I've just opened a Python module in dired mode, created a new directory, opened that in dired, and now need to add an empty __init__.py file in the directory.
If I use C-x C-f __init__.py RET C-x C-s then emacs doesn't create the file because no changes have been made to it. I would have to type in the file, save it, delete my typing and then save it again for that to work.
Thanks
You can use the touch command:
M-! touch __init__.py RET
The following works: C-x b __init__.py RET C-x C-w RET
If you're in a dired buffer the file will be saved in the directory show here.
The trick is to first create an empty buffer by switching to a name that doesn't exist. Then write out the file.
If you want Emacs to treat all new files as modified, you can automate the solution like this:
(add-hook 'find-file-hooks 'assume-new-is-modified)
(defun assume-new-is-modified ()
(when (not (file-exists-p (buffer-file-name)))
(set-buffer-modified-p t)))
Programatically and without any dependency on touch, it's quite easy:
(with-temp-buffer (write-file "path/to/empty/file/"))
After this thread, Emacs has added two new commands:
make-empty-file
dired-create-empty-file
These commands will be available in the emacs 27.1 release.
Here's an adaptation of dired-create-directory. It works the same way, so as well as a plain filename, you can also specify new parent directories (to be created under the current directory) for the file (e.g. foo/bar/filename).
(eval-after-load 'dired
'(progn
(define-key dired-mode-map (kbd "C-c n") 'my-dired-create-file)
(defun my-dired-create-file (file)
"Create a file called FILE.
If FILE already exists, signal an error."
(interactive
(list (read-file-name "Create file: " (dired-current-directory))))
(let* ((expanded (expand-file-name file))
(try expanded)
(dir (directory-file-name (file-name-directory expanded)))
new)
(if (file-exists-p expanded)
(error "Cannot create file %s: file exists" expanded))
;; Find the topmost nonexistent parent dir (variable `new')
(while (and try (not (file-exists-p try)) (not (equal new try)))
(setq new try
try (directory-file-name (file-name-directory try))))
(when (not (file-exists-p dir))
(make-directory dir t))
(write-region "" nil expanded t)
(when new
(dired-add-file new)
(dired-move-to-filename))))))
Emacs won't allow you to save a buffer unless it thinks the contents have changed. The quickest, though possibly not cleanest is to open the file using C-x C-f, then press (say) space and backspace, then you should be able to save a file with no contents.
There are other ways of changing the "buffer has been modified" flag, but I don't think there's any easier.
Use touch command.
M-! touch __init__.py RET
The shortest way
Creates an empty file via a shell operation (but does not open it):
M-! > __init__.py RET
Open the new file:
C-x C-f RET
(Note: we don't have to type in the name again, because the new file is automatically the first choice)
(shell-command (concat "touch " (buffer-file-name))) will do what you want, if you've already opened the empty file.
In addition to other answers on the page, you can use f.el's function f-touch:
M-:(f-touch "__init__.py")RET
You can mark an empty buffer as modified by running set-buffer-modified-p. Then when you save it, Emacs will write the file.
M-; ; Eval
(set-buffer-modified-p t) ; Mark modified
C-x C-s ; Save buffer
I use the following bound to t in dired.
(defun my-dired-touch (filename)
(interactive (list (read-string "Filename: " ".gitkeep")))
(with-temp-buffer
(write-file filename)))
;; optionally bind it in dired
(with-eval-after-load 'dired
(define-key dired-mode-map "t" 'my-dired-touch))
I've modified answer from MrBones and created custom function with keybinding:
; create empty __init__.py at the place
(defun create-empty-init-py()
(interactive)
(shell-command "touch __init__.py")
)
(global-set-key (kbd "C-c p i") 'create-empty-init-py)
This is very useful to not spend time on recurring action of creating init.py everywhere in new Python project folder.
The best option would be:
(with-temp-file "filename"
(insert ""))

Resources