calling org-content from lisp not working - elisp

When I call org-content from the active buffer I get the outline I want. However if I use it in a lisp function like this
(split-window-right (truncate (* W 0.75)))
(if (get-buffer "inbox.org")
(set-window-buffer nil "inbox.org")
(progn
(find-file "~/Documents/GTD/inbox.org")
(text-scale-set -1)))
(org-content)
The windows splits and the right buffer gets loaded but the org-content bit doesn't seem to do anything.
Any ideas of what I do wrong ?
Thanks,
Jouke

Here is a reproducible example, open a new buffer named test.org and define the following function in the *scratch* buffer:
(defun test ()
(let ((buffer (get-buffer "test.org")))
(when buffer
(set-window-buffer nil buffer)
(message "%s" (current-buffer)))))
The message being outputted is *scratch*: only the buffer associated with the window was changed, but what Emacs considers the current buffer did not.
If instead you use switch-to-buffer, as follows, the message displays the selected buffer:
(defun test ()
(let ((buffer (get-buffer "test.org")))
(when buffer
(switch-to-buffer buffer)
(message "%s" (current-buffer)))))
Applying the same change to your code makes (org-content) happy.

Related

Getting the output of a process

I am trying to create a function which takes as input a path and returns the output of the ls terminal command as a string. I'm using a process and sentinel since I'll eventually want to create other functions which will take some time to execute, and I want them to run asynchronously.
(defun ls-to-string (path)
(let (ls-proc
ls-output)
(progn (setq ls-proc (start-process "" "ls-buffer" "ls" path))
(set-process-sentinel ls-proc (lambda (p e)
(if (string= e "finished\n")
(progn (set-buffer "ls-buffer")
(setq ls-output (buffer-string))
(kill-buffer "ls-buffer")
(message ls-output))))) <---- (1)
ls-output))) <---- (2)
(ls-to-string "/home")
I have (temporarily) added (message ls-output) just to show that ls-output does contain the string (1). However the return value is nil (2).

Emacs: Symbol's value as variable is void: Removes (init.el)

Sorry if this is a silly question; I am a complete novice when it comes to emacs.
Recently, I began to do research on how to set up emacs and stumbled upon a great video series by Mike Zamansky. However, whilst following this video (creating an org init file), all of the packages I installed onto my emacsclient proceeded to not work. During initialization, there was an error - namely, "Symbol's value as variable is void: Removes." I copied his tutorial verbatim and I don't see any potential syntactical errors - perhaps I overlooked some errors. However, I've been searching throughout the internet, but could not find any answers to this problem.
Here is the contents of the init.el file:
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/"))
(package-initialize)
;; Bootstrap 'use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(org-babel-load-file (expand-file-name "~/.emacs.d/myinit.org"))
Here is the error:
Warning (initialization): An error occurred while loading
‘/Users/Kyojin/.emacs.d/init.el’:
Symbol's value as variable is void: Removes
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.
Debugger Output (--debug-init):
Debugger entered--Lisp error: (void-variable Removes)
eval-buffer(#<buffer *load*-527594> nil
"/Users/Kyojin/.emacs.d/myinit.el" nil t) ; Reading at buffer position
8
load-with-code-conversion("/Users/Kyojin/.emacs.d/myinit.el"
"/Users/Kyojin/.emacs.d/myinit.el" nil nil)
load("/Users/Kyojin/.emacs.d/myinit.el" nil nil t)
load-file("/Users/Kyojin/.emacs.d/myinit.el")
(progn (load-file exported-file) "Loaded")
(if compile (progn (byte-compile-file exported-file (quote load))
"Compiled and loaded") (progn (load-file exported-file) "Loaded"))
(message "%s %s" (if compile (progn (byte-compile-file exported-file
(quote load)) "Compiled and loaded") (progn (load-file exported-file)
"Loaded")) exported-file)
(let* ((age (function (lambda (file) (float-time (time-subtract
(current-time) (nth 5 ...)))))) (base-name (file-name-sans-extension
file)) (exported-file (concat base-name ".el"))) (if (and (file-exists-
p exported-file) (> (funcall age file) (funcall age exported-file)))
nil (setq exported-file (car (last (org-babel-tangle-file file
exported-file "emacs-lisp"))))) (message "%s %s" (if compile (progn
(byte-compile-file exported-file (quote load)) "Compiled and loaded")
(progn (load-file exported-file) "Loaded")) exported-file))
org-babel-load-file("/Users/Kyojin/.emacs.d/myinit.org")
eval-buffer(#<buffer *load*> nil "/Users/Kyojin/.emacs.d/init.el"
nil t) ; Reading at buffer position 358
load-with-code-conversion("/Users/Kyojin/.emacs.d/init.el"
"/Users/Kyojin/.emacs.d/init.el" t t)
load("/Users/Kyojin/.emacs.d/init" t t)
#[0"\205\266\306=\203\307\310Q\202?\311=\204\307\312Q\202?\313\307
\314\315#\203* \316\202?\313\307\314\317#\203>\320\321\322!D\nB\323
\202?\316\324\325\324\211#\210\324=\203e\326\327\330\307\331Q!\"\325
\324\211#\210\324=\203d\210\203\247\332!\333\232\203\247\334!
\211\335P\336!\203\201\211\202\214\336!\203\213\202\214\314\262\
\203\245\337\"\203\243\340\341#\210\342\343!\210\266\f\205\264\314\325
\344\324\211#)\262\207"[init-file-user system-type
delayed-warnings-list user-init-file inhibit-default-init inhibit-
startup-screen ms-dos "~" "/_emacs" windows-nt "/.emacs" directory-
files nil "^\\.emacs\\(\\.elc?\\)?$" "~/.emacs" "^_emacs\\(\\.elc?\\)?
$" initialization format-message "`_emacs' init file is deprecated,
please use `.emacs'" "~/_emacs" t load expand-file-name "init" file-
name-as-directory "/.emacs.d" file-name-extension "elc" file-name-sans-
extension ".el" file-exists-p file-newer-than-file-p message "Warning:
%s is newer than %s" sit-for 1 "default"] 7]()
command-line()
normal-top-level()
Search for the word Removes in your init file, "/Users/Kyojin/.emacs.d/myinit.el" (or possibly in some file that it loads).
If you don't find it immediately then recursively bisect your init file to find the code that is problematic. You can do that by commenting out first 1/2, then 3/4, then 7/8,... of the file until you locate the problem. You can comment a block of text/code by selecting it and then using M-x comment-region. You can uncomment a selection the same way, but with C-u first: C-u M-x comment-region.

Emacs : how to load file content in scratch screen

I would like to load "~/todo.org" file content in scratch buffer at startup.
I have tried:
(setq initial-buffer-choice "~/todo.org")
But it opens the file in a new buffer (not scratch).
I have also tried:
(setq initial-scratch-message "~/todo.org")
But it prints the file path in the scratch buffer and i would like it's content.
I also would like to change the mode of the scratch buffer to org-mode.
I have tried:
(setq initial-major-mode org-mode)
But i have an initialisation error
Symbol's value as variable is void: org-mode
You can achieve the desired effect with a little bit of Lisp code that you put in your init file:
(condition-case err
(with-current-buffer "*scratch*"
(let ((min (point-min))
(max (point-max))
(goto-char max)
(insert-file-contents "~/todo.org")
(delete-region min max)
(org-mode)))
(error (message "%s" error-message-string err)))
But as #phils pointed out in a comment to your question, the *scratch* buffer might not be the best buffer to use for this functionality. Thus, I suggest to consider the following alternative:
(condition-case err
(let ((buffer (get-buffer-create "*todo*")))
(with-current-buffer buffer
(insert-file-contents "~/todo.org")
(org-mode))
(setq initial-buffer-choice buffer))
(error (message "%s" error-message-string err)))
By using this version, you leave the *scratch* buffer alone. Your .org file will be inserted in a separate buffer by the name of *todo*. This buffer is not associated with your ~/todo.org file, so when you first try save it, you will have to specify a file name.
Finally, i'll go for this:
(condition-case err
(when (get-buffer "*scratch*")
(with-current-buffer "*scratch*"
(erase-buffer)
(insert-file-contents "~/todo.org")
(org-mode)
)
)
(error (message "%s" error-message-string err)))

Prevent Emacs from modifying the OS X clipboard?

How can I prevent Emacs from ever modifying the OS X clipboard unless I explicitly ask it to?
I've tried all of:
(setq x-select-enable-clipboard nil)
(setq interprogram-cut-function nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
Which does prevent kill/yank from modifying the clipboard, but selected text is still put on the clipboard.
This is GNU Emacs.app on OS X.
What else should I try?
After doing some digging into the same issue, I believe that the issue actually lies in the Emacs x-select-text function, which explicitly ignores the value of x-select-enable-clipboard on NextStep (and OS X is a NextStep).
I've "solved" this problem by replacing x-select-text with a no-op function, then explicitly using ns-{get,set}pasteboard for interprogram{cut,paste}-function:
; Override the default x-select-text function because it doesn't
; respect x-select-enable-clipboard on OS X.
(defun x-select-text (text))
(setq x-select-enable-clipboard nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
(setq interprogram-cut-function 'ns-set-pasteboard)
(setq interprogram-paste-function 'ns-get-pasteboard)
Here is the original x-select-text code:
(defun x-select-text (text)
"Select TEXT, a string, according to the window system.
On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard. If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.
On MS-Windows, make TEXT the current selection. If
`x-select-enable-clipboard' is non-nil, copy the text to the
clipboard as well.
On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
is not used)."
(cond ((eq (framep (selected-frame)) 'w32)
(if x-select-enable-clipboard
(w32-set-clipboard-data text))
(setq x-last-selected-text text))
((featurep 'ns) ; This is OS X
;; Don't send the pasteboard too much text.
;; It becomes slow, and if really big it causes errors.
(ns-set-pasteboard text)
(setq ns-last-selected-text text))
(t
;; With multi-tty, this function may be called from a tty frame.
(when (eq (framep (selected-frame)) 'x)
(when x-select-enable-primary
(x-set-selection 'PRIMARY text)
(setq x-last-selected-text-primary text))
(when x-select-enable-clipboard
(x-set-selection 'CLIPBOARD text)
(setq x-last-selected-text-clipboard text))))))

How to wait for / capture aysnchronous shell command output in emacs lisp?

If I execute a shell command asynchronously in emacs lisp like so:
(shell-command "mycommand &")
Is there a way to wait for the command to generate output before continuing? For my current application, it is probably sufficient to wait until the command generates any output at all, but ideally I'd like to capture the output for additional processing. Is this possible?
You should use comint-output-filter-functions variable that contains function to call after output is inserted into the buffer.
For example, you can do :
(add-hook 'comint-output-filter-functions '(lambda (txt) (message "hello")))
N.B. : From Emacs 23.2, you have the new command async-shell-command, bound globally to M-&.
This executes your command asynchronously without requiring an ampersand. The output of your command is sent to the buffer
*Async Shell Command*.
Perhaps you need to register a Process Filter to give you the callback timing you need? See 37.9 Receiving Output from Processes in the Elisp manual (I see this in my copy for Emacs 22.3).
Here is an example of running a callback when you get the first process output and also storing it into an "associated buffer". Copy it to your *scratch* buffer and eval-region it, but make sure to split-window and show the *Messages* buffer visible so that you can see what's going on.
;; this is emacs lisp (and a comment line)
(defvar my-callback-got-some-already nil)
(defun my-callback ()
(message "callback ran at %s" (current-time-string)))
(defun my-filter-waits-for-first-time-input (proc string)
(unless my-callback-got-some-already
(setq my-callback-got-some-already t)
;; do your one-time thing
(my-callback))
;; insert into the associated buffer as if no process filter was
;; registered
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc))))))
(defun async-callback-test-harness ()
(interactive)
(let ((process-handle "async-callback-test")
(associated-process-buffer "*async-callback-test*")
(command "ls")
(busy-loop-var ""))
(setq my-callback-got-some-already nil)
(message "start test %s" (current-time-string))
(start-process process-handle associated-process-buffer command)
;; Supposedly async but Emacs doesn't get the input until
;; "Emacs is waiting" so the following set-process-filter
;; can be registered in time.
;; To prove the point, make emacs busy loop to show that the
;; emacs doesn't drop its input and
;; the callback will get the unskipped input.
(switch-to-buffer associated-process-buffer)
(dotimes (k 2000) ; about two seconds on my machine
(setq busy-loop-var (concat busy-loop-var "busy looping...")))
(message "done busy waiting %s" (current-time-string))
(set-process-filter (get-process process-handle)
'my-filter-waits-for-first-time-input)
nil))
;; run it!
(async-callback-test-harness)

Resources