Change "ls" from emacs dired to other app - linux

I wrote some script in elisp, it merges ls -l and du (showing real folder size instead of what is written in ls). I named it lsd. Here is screenshot:
http://i.imgur.com/PfSq6.png
Now i'll list implementation. I am not a good coder, so I will appreciate any information about bugs and things that can be made better.
lsd.el
#!/usr/bin/emacs --script
(progn
(setq argz command-line-args-left)
(setq folder "./")
(while argz
;; (message (car argz))
(if (/= ?- (aref (car argz) 0))
(setq folder (car argz)))
(setq argz (cdr argz)))
(if (/= ?/ (aref folder (1- (length folder)))) (setq folder (concat folder "/")))
(switch-to-buffer " *lsd*")
(erase-buffer)
(shell-command (concat "ls -l -h --color=always " " " (apply 'concat (mapcar '(lambda(arg) (concat arg " ")) command-line-args-left))) (current-buffer))
(switch-to-buffer " *du*")
(erase-buffer)
(shell-command (concat "du -h -d 1 " folder) (current-buffer))
(goto-char 1)
(while (search-forward "Permission denied" (point-max) t nil)
(goto-char (point-at-bol))
(let ((beg (point)))
(forward-line)
(delete-region beg (point)))) ; Remove all permission denied lines, thus show only permitted size.
(goto-char 1)
(while (and (search-forward folder (point-max) t nil) (/= (point-max) (1+ (point-at-eol)))) ; we do not need last line(the folder itself), so this line is something complex.
(setq DIR (buffer-substring (point) (point-at-eol)))
(goto-char (point-at-bol))
(setq SIZE (buffer-substring (point) (1- (search-forward " " (point-at-eol) nil nil))))
(goto-char (point-at-eol))
(switch-to-buffer " *lsd*")
(goto-char 1)
(if (search-forward DIR (point-max) t nil)
(progn
(goto-char (point-at-bol))
(search-forward-regexp "[0-9]+" (point-at-eol) nil nil)
(search-forward-regexp " *[0-9]+[^ \n]*[ \n]*" (point-at-eol) nil nil)
;; If ls have options, that makes some numbers before size column - we are doomed. (-s, for example)
(setq SIZE (concat SIZE " "))
(while (< (length SIZE) (length (match-string 0))) (setq SIZE (concat " " SIZE)))
(replace-match SIZE)))
(switch-to-buffer " *du*"))
(switch-to-buffer " *lsd*")
(message "%s" (buffer-substring (point-min) (point-max)))
(defun error(&rest args) args)
(defun message(&rest args) args)) ; Do not show any messages.
lsd
(I made this script to start emacs without loading anything but script. If it can be done easier, please point this)
#/bin/bash
emacs -Q --script /usr/local/bin/lsd.el $#
And here is the problem: how to use this lsd in dired?
Can I change something in dired to use lsd instead of ls?
Can I rename ls in oldls, and make some ls bash script that passes all arguments to ls if there no --lsd flag, and passing all arguments to lsd if --lsd is here?
Is it good idea at all?

In Emacs24 there is also `insert-directory-program' to set the ls executable. Put
(setq insert-directory-program "/usr/local/bin/lsd")
(adjust the path accordingly) in your .emacs or init.el and dired takes your lsd script.

I don't know if this the most efficient way to do things, I'm still a bit of an Emacs beginner. But here's how I would do it.
Since you're on Linux you should start by telling emacs to use its built-in ls emulation. A simple (require 'ls-lisp) in your init file should suffice.
Set the variable ls-lisp-use-insert-directory-program to true. This tells emacs to use an external program for ls.
The actual program it uses can be customized by setting the variable insert-directory-program to point to your lsd script.
Here's an example of how to do this:
;; Put this in your init file
(require 'ls-lisp)
(setq ls-lisp-use-insert-directory-program T)
(setq insert-directory-program "~/path/to/lsd")
Let me know if this works for you. I use emacs on Windows so I'm not sure how well this ports over to linux (the ls emulation part that is).

Related

set init.el file to start-up the python interpreter

On ubuntu 16.04 emacs24 python3.5, I have a simple init.el file as follows:
;; Set what python
(setq elpy-rpc-python-command "python3.5")
(setq python-shell-interpreter "python3.5")
;; Set org mode
(eval-after-load "org"
'(org-babel-do-load-languages
'org-babel-load-languages
'((sh . t)
(python . t)
(emacs-lisp . t)
(ditaa . t)
))
)
;; split windows
;; layout definition
(defun my-startup-layout ()
(interactive)
(delete-other-windows)
(split-window-horizontally) ;; -> |
(next-multiframe-window)
(dired "~")
)
;; execute the layout
(my-startup-layout )
However, i would like to be able to run the right window with the python interpreter by default.
How can i do this please?
You can call run-python in your hook, eg
(defun my-startup-layout ()
(delete-other-windows)
(split-window-horizontally)
(dired "~")
(run-python nil nil 'show))
(my-startup-layout)

Elisp: How to search a wordlist and copy the results to another buffer?

I have a wordlist
dempron {hic, haec, hoc, huius, huic, hunc, hanc, hac, hi, hae, horum, harum, his, hos, has}
I have a xml-kind-of text
<p>Hoc templum magnum est.</p>
<p>Templa Romanorum magna sunt.</p>
<p>Claudia haec templa in foro videt.</p>
I would like to search the wordlist "dempron" and copy the sentences that have words from the wordlist to a buffer called results.
I agree with Simon Fromme, but hopefully this will get you started. Let me know if you have any questions!
(defconst dempron
'("hic" "haec" "hoc" "huius" "huic" "hunc" "hanc" "hac" "hi" "hae" "horum"
"harum" "his" "hos" "has"))
(defun dempron-search ()
"A function to naively search for sentences in XML <p> tags
containing words from `dempron'. Run this in the buffer you want
to search, and it will search from POINT onwards, writing results
to a buffer called 'results'."
(interactive)
(beginning-of-line)
(while (not (eobp)) ;; while we're not at the end of the buffer
(let ((cur-line ;; get the current line as a string
(buffer-substring-no-properties
(progn (beginning-of-line) (point))
(progn (end-of-line) (point)))))
;; See if our current line is in a <p> tag (and run `string-match' so we
;; can extract the sentence with `match-string')
(if (string-match "^<p>\\(.*\\)</p>$" cur-line)
(progn
;; then extract the actual sentence with `match-string'
(setq cur-line (match-string 1 cur-line))
;; For each word in our sentence... (split on whitespace and
;; anything the sentence is likely to end with)
(dolist (word (split-string cur-line "[[:space:].?!\"]+"))
;; `downcase' to make our search case-insensitive
(if (member (downcase word) dempron)
;; We have a match! Temporarily switch to the
;; results buffer and write the sentence
(with-current-buffer (get-buffer-create "results")
(insert cur-line "\n")))))))
(forward-line 1))) ;; Move to the next line

How to reset emacs to save files in utf-8-unix character encoding?

I have a problem. I found out that emacs recently stopped to save all my new files with the default character set "utf-8-unix".
I do not understand what I did, but when I open a file, above the mini-buffer I see "--:---" instead of "-U:---", where the "U" says that the file is saved with utf-8-unix charset.
How can I reset emacs to save files in the proper coding system???
Here is my setup:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ENCODING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-h C RET
;; M-x describe-current-coding-system
(add-to-list 'file-coding-system-alist '("\\.tex" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.txt" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.el" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.scratch" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("user_prefs" . utf-8-unix) )
(add-to-list 'process-coding-system-alist '("\\.txt" . utf-8-unix) )
(add-to-list 'network-coding-system-alist '("\\.txt" . utf-8-unix) )
(prefer-coding-system 'utf-8-unix)
(set-default-coding-systems 'utf-8-unix)
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8-unix)
(set-selection-coding-system 'utf-8-unix)
(setq-default buffer-file-coding-system 'utf-8-unix)
;; Treat clipboard input as UTF-8 string first; compound text next, etc.
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
;; mnemonic for utf-8 is "U", which is defined in the mule.el
(setq eol-mnemonic-dos ":CRLF")
(setq eol-mnemonic-mac ":CR")
(setq eol-mnemonic-undecided ":?")
(setq eol-mnemonic-unix ":LF")
(defalias 'read-buffer-file-coding-system 'lawlist-read-buffer-file-coding-system)
(defun lawlist-read-buffer-file-coding-system ()
(let* ((bcss (find-coding-systems-region (point-min) (point-max)))
(css-table
(unless (equal bcss '(undecided))
(append '("dos" "unix" "mac")
(delq nil (mapcar (lambda (cs)
(if (memq (coding-system-base cs) bcss)
(symbol-name cs)))
coding-system-list)))))
(combined-table
(if css-table
(completion-table-in-turn css-table coding-system-alist)
coding-system-alist))
(auto-cs
(unless find-file-literally
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(funcall set-auto-coding-function
(or buffer-file-name "") (buffer-size))))))
(preferred 'utf-8-unix)
(default 'utf-8-unix)
(completion-ignore-case t)
(completion-pcm--delim-wild-regex ; Let "u8" complete to "utf-8".
(concat completion-pcm--delim-wild-regex
"\\|\\([[:alpha:]]\\)[[:digit:]]"))
(cs (completing-read
(format "Coding system for saving file (default %s): " default)
combined-table
nil t nil 'coding-system-history
(if default (symbol-name default)))))
(unless (zerop (length cs)) (intern cs))))
For some reason, Windows started interpreting my init.el file as being encoded in something other than UTF-8, and choked on characters such as "ö" and "§". The solution was to add a line ; -*- coding: utf-8 -*- at the start of the file.
To make very sure that UTF-8 is used in every case, I have the following lines in init.el:
;; Use UTF-8 for all character encoding.
(set-language-environment 'utf-8)
(set-default-coding-systems 'utf-8)
(set-selection-coding-system 'utf-8)
(set-locale-environment "en.UTF-8")
(prefer-coding-system 'utf-8)
(setq utf-translate-cjk-mode nil) ; disable CJK coding/encoding
To get back the described old behavior, try adding
(set-language-environment "UTF-8")
to your .emacs startup file.

In emacs: exclude folders from searching IDs "gid"

I have this snippet of code in my dotemacs file that helps me view IDs.
How is it possible to exclude some folders from the ID search?
; gid.el -- run gid using compilation mode.
;(require 'compile)
;(require 'elisp-utils)
;(provide 'gid)
(defvar gid-command "gid" "The command run by the gid function.")
(defun gid (args)
"Run gid, with user-specified ARGS, and collect output in a buffer.
While gid runs asynchronously, you can use the \\[next-error] command to
find the text that gid hits refer to. The command actually run is
defined by the gid-command variable."
(interactive (list
;(read-input (concat "Run " gid-command " (with args): ") ;confirmation
(word-around-point)))
;)
;; Preserve the present compile-command
(let (compile-command
(gid-buffer ;; if gid for each symbol use: compilation-buffer-name-function
(lambda (mode) (concat "*gid " args "*"))))
;; For portability between v18 & v19, use compile rather than compile-internal
(compile (concat gid-command " " args))))
(defun word-around-point ()
"Return the word around the point as a string."
(save-excursion
(if (not (eobp))
(forward-char 1))
(forward-word -1)
(forward-word 1)
(forward-sexp -1)
(let ((beg (point)))
(forward-sexp 1)
(buffer-substring beg (point)))))
Found the solution.
Simply when making the IDs prune any uninteresting folders like this:
mkid --prune X

Racket eof-object read from input port

I tried to read string from an input-port in Racket, but no matter what API functions I used to read (read, read-string, read-bytes etc), the return value of those functions was never equal eof-object.
(define (some_process inp)
(begin
(let ([c (read-string 1 inp)])
(if (eof-object? c)
(begin
(display "EOF \n")
#f)
(if (equal? c "\n")
(begin
(display "NEWLINE \n"))
(some_process inp))))))
The c can never be an eof-object?
If you display what c is, it is always a newline.
Read reference:
read-char: "Reads a single character from in – which may involve reading several bytes to UTF-8-decode them into a character. If no bytes are available before an end-of-file, then eof is returned."
read-string: "Returns a string containing the next amt characters from in. If no characters are available before an end-of-file, then eof is returned."
Examples:
> (read-char (open-input-string "char"))
#\c
> (read-string 50 (open-input-string "the string"))
"the string"
>
But if there are no character(s) in the buffer, you'll get eof:
> (read-char (open-input-string ""))
#<eof>
> (read-string 50 (open-input-string ""))
#<eof>
I think you just want to read some amount of characters in a loop and do something with them. If so, the solution would look something along the lines of:
(define (another-process inp)
(let ([c (read-char inp)])
(if (eof-object? c)
(begin (display "==== EOF ====") (newline))
(begin (display c) (newline)
(another-process inp)))))
Example:
> (another-process (open-input-string "OK"))
O
K
==== EOF ====
> (another-process (open-input-string ""))
==== EOF ====
>
Notice a second call to another-process with an empty line, it detects eof immediately and exits the loop.
EDIT:
In case you need to check if the read character is newline:
(define (process-moo inp)
(let ([c (read-char inp)])
(cond
((eof-object? c)
(display "==== EOF ====") (newline))
((eq? c #\newline)
(newline) (display "---- NEWLINE ----") (newline)
(process-moo inp))
(else
(display c)
(process-moo inp)))))
Example:
> (call-with-input-string "Hello\nhumans\n!" process-moo)
Hello
---- NEWLINE ----
humans
---- NEWLINE ----
!==== EOF ====
>
Hope that helps.
If you are entering your input from the console, try pressing Ctrl+D (in Unix and MacOSX), or Ctrl+Z, then Enter (in Windows). This will signal the end of the input.

Resources