I use elfeed to read RSS.
In each RSS there is an http link to a torrent file.
And I want to call an external program (aria2c) to download that torrent link, assuming that there is no such tool within Emacs.
How to write a small function to do this?
Here a simple solution:
(defun open-with-ariac2()
(interactive)
(shell-command
(format (concat "aria2c " (browse-url-url-at-point)))))
Related
I have a bunch of org-mode files with snippets containing HTML code and I would like to convert those to plain text.
I don't need any fancy fully automated solution, I can just past my HTML snippet into a scratch buffer if that's easier.
Here's a simple example of desired behavior:
<div><div>First Line<br>Second Line</div></div>
First Line
Second Line
What are the options available to Emacs users for such a task?
Emacs added EWW in Emacs 24.4 (2014), the Emacs Web Wowser, a built-in web browser . The shr.el library is used for rendering HTML, e.g.,
(with-temp-buffer
(insert
"<div><div>First Line<br>Second Line</div></div> ")
(shr-render-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))
;; =>
"First Line
Second Line
"
shr-render-region uses libxml-parse-html-region which requires your Emacs has libxml2 support.
html2org package seems to get the job done
html2org function converts and replaces the HTML code as text.
To get the absolute file path without extension in a buffer, e.g. /home/alice/hello.cpp -> /home/alice/hello, the following code works
(concat (file-name-directory (buffer-file-name)) (file-name-base (buffer-file-name)))
But it looks too verbose. Is there a much elegant way or a direct function for this?
(file-name-sans-extension (buffer-file-name))
Are you using auto-complete? It completes elisp names so I found the function in a second.
If you manipulate files very often in Elisp, i recommend installing f.el file and directory API, which adds a big amount of utility functions. For example, you can use f-no-ext to drop extension from the path.
I'm looking to automate my development and I would like emacs to execute a few commands automatically when I load any file called "project.clj"
Specifically I'd like it to check the open buffers, and if there isn't a buffer called "swank" execute the clojure-jack-in command as if it came from the "project.clj" buffer,
and then I'd like it to run shell and speedbar as well, but I imagine once I figure out how to do the above, those will be easy.
I am a complete elisp noob, but I'm familiar with lisp in general.
You could do something like this:
(defun my-project-hook (filename)
(when (string= (file-name-nondirectory filename) "project.clj")
(do-stuff)))
(add-hook 'after-load-functions 'my-project-hook)
I need to process a file of certain types with external command line program accepting single argument (filename) and then use file modified by this program either open modified file or accept output of command line program as data source for file.
Any way to do this?
Where I used to work there were some binary files that I wanted to view in emacs. The way I did this was to add to jka-compr-compression-info-list like the following for editing applescripts:
(add-to-list 'jka-compr-compression-info-list
["\\.scpt\\'"
"Compiling" "osacompile-helper.sh" nil
"Decompiling" "osacompile-helper.sh" ("-d")
nil nil "Fasd"])
(jka-compr-update)
Here osacompile-helper.sh is just a little shell wrapper around osacompile and osadecompile that reads from stdin and writes to stdout (which is required). You also need to turn on auto-compression-mode, although I think that's the default. If you use the customize interface to change jka-compr-compression-info-list, instead of setting it directly, then you don't have to call jka-compr-update.
If you just want this to work when you open the file with C-x C-f, then you can probably just attach your behaviour to find-file, but deeper down I believe insert-file-contents is what eventually reads files in.
A cursory look doesn't seem to show any appropriate hook, so you could look at doing this with before advice.
(defadvice insert-file-contents
(before my-before-insert-file-contents-advice)
"Process files externally before reading them."
(let ((filename (expand-file-name (ad-get-arg 0))))
(message "About to read file %s" filename)
;; your code here.
;; ;; stupid unsafe example:
;; (let ((file (shell-quote-argument filename))
;; (tempfile (shell-quote-argument (make-temp-file "some-prefix-"))))
;; (shell-command (format "sort %s >%s" file tempfile))
;; (shell-command (format "mv %s %s" tempfile file)))
))
(ad-activate 'insert-file-contents)
You might like to elaborate on your requirements, in case you don't actually need to clobber the original file? (which I think is a horrendous idea, frankly; I certainly wouldn't use code like this!)
For example, you could read in the original file, process it within the buffer (maybe using shell-command-on-region with the replace flag), and set the buffer as unmodified. That way you are only likely to save the changes made by the shell command if you make other edits to the file, and the mere act of loading the file into an editor hasn't actually modified it.
In any case, I trust you'll implement sensible backup processes into your code, and will be plenty paranoid when testing!
You can call the external program with shell-command, with the output directed to a new buffer. A minimal working example is:
(defun my-find-and-process-file ()
(interactive)
(let* ((file (read-file-name "File name: "))
(buf (pop-to-buffer file)))
(shell-command (format "cat %s" file) buf)))
Replace cat with the name of your program. This will create a buffer and fill it with the output of your program. If a buffer with the name of your file already exists, it will over-write it. If that's a possibility, you will want to change the buffer name to something safe by adding a suffix or something. This code also doesn't trigger any of the find-file hooks, so you'll have to manually select the mode, or modify the code to do that for you.
I recently found org-annotate-file. I would like to use it to annotate pdf documents or music files or any other files on my computer, and write my annotations in a file annotations.org. I am not looking to include annotations IN the pdf. But what I cannot figure out is what it means to "visit a file"? Does it have to be a file that emacs can open?
But more generally, is there a package that can do something like this: I visit a directory in dired mode, mark a bunch of files on some topic of my interest, and with one command I send links to the files to my annotations.org file (maybe as subheadings under a heading, which may be the directory name), and then I can write the annotations in the annotations file. Then with one command, I should be able to reach any of the files (which org-mode will allow) or open it in an external program. Is this possible in some package?
Thanks.
Of course, it can be done. However, it seems the actual code of org-annotate-file.el, that I found
here, doesn't seem to accept annotating a file that has not been opened (visited means here opened), because the function to annotate uses the current open file as a source for the name. The current implementation of org-annotate-file is this:
(defun org-annotate-file ()
"Put a section for the current file into your annotation file"
(interactive)
(error-if-no-file)
(org-annotate-file-show-section))
At least you could modify it to accept an arbitrary file (if you provide it):
(defun org-annotate-file (&optional filename)
"Put a section for the current file into your annotation file"
(interactive "FFile to tag: ")
; if a file is specified, bypass the check for error when no file
(if filename
(org-annotate-file-show-section filename)
(progn
(error-if-no-file)
(org-annotate-file-show-section))))
This ask you for a file name whenever you do M-xorg-annotate-file.
You also have to change the org-annotate-file-show-section to accept either a file name or a buffer. The first let should be like this:
(defun org-annotate-file-show-section (&optional buffer-or-file)
"Visit the buffer named `org-annotate-file-storage-file' and
show the relevant section"
(let* ((line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(filename (if (stringp buffer-or-file)
buffer-or-file
(get-filename buffer-or-file (buffer-file-name))))
(link (get-link filename))
(search-link (org-make-link-string
(concat "file:" filename "::" line)
(org-annotate-file-prettyfy-desc line))))
(show-annotations filename link)
.... rest of the code....
dired integration can be started from here, but I'm still not familiar with the dired API...
EDIT: I'm creating a branch in bitbucket for that modifications. I find the utility very useful and might use it myself. I'll post the link here. And here it is: https://bitbucket.org/dsevilla/org-annotate-file/src