Display-buffer-at-bottom to popup a message - elisp

I wrote such a function to pop-up a buffer at bottom
(defun popup-new-message (msg)
(interactive
(let ((buffer (generate-new-buffer "pop-message")))
(with-current-buffer buffer
(insert msg))
(display-buffer buffer '(display-buffer-at-bottom . nil)))
))
(popup-new-message "It's time to have a rest.")
When run it, nothing happened.
What's the problem?
I checked that functions of generate-new-buffer and display-message working
#+begin_src emacs-lisp :tangle yes
(let ((buffer (generate-new-buffer "pop-message")))
(print buffer))
#+end_src
#+RESULTS:
: #<buffer pop-message<2>>

Your (interactive S-EXP is not closed correctly.

Related

Scheme REPL in Scheme don't render prompt when user press enter

I've written Scheme REPL that works in all implementations I've tested, but have problem when when someone press enter and don't type anything.
(define void (if #f #f))
(cond-expand
(lips)
(kawa)
(gambit)
(guile (define (flush-output-port) void))
(gauche (define (flush-output-port) (flush (current-output-port)))))
(let ((repl (lambda ()
(let ((env (interaction-environment)))
(let iter ()
(display "scheme> ")
(flush-output-port)
(let ((input (read)))
(if (eof-object? input)
(newline)
(let ((result (eval input env)))
(if (not (eq? result void))
(begin
(display result)
(newline)))
(iter)))))))))
(repl))
How to fix my REPL so it works as expected when someone type enter, it should also work when user tried to exit the REPL by using CTRL+D. Right now it don't print prompt when you enter empty input.
NOTE: I was told that my void may don't work in every implementation, but even SRFI use this (I've learned about this in Sketchy Scheme book).
I was able to fix my REPL:
(let ((repl (lambda ()
(let ((env (interaction-environment)))
(let iter ()
(display "scheme> ")
(flush-output-port)
(if (char=? (peek-char) #\newline)
(begin
(read-char)
(iter)))
(let ((input (read)))
(if (eof-object? input)
(newline)
(let ((result (eval input env)))
(if (not (eq? result void))
(begin
(display result)
(newline)))
(if (and (char-ready?) (char=? (peek-char) #\newline))
(read-char))
(iter)))))))))
(repl))

orgmode - disable elisp code execute confirmation dialog

I have below code to call elisp function "myfun" when user click the link:
#+BEGIN_SRC elisp :results output raw
(defun myfun(filepath lineno)
(if (not (get-buffer bufname))
(get-buffer-create bufname)
)
(switch-to-buffer-other-window bufname)
(global-linum-mode)
(erase-buffer)
(insert-file-contents (format "%s" filepath))
(goto-char (point-min))
(forward-line lineno)
(setq start (point))
(forward-line -1)
(setq end (point))
(let ((x (make-overlay start end)))
(overlay-put x 'face '(:background "#fef0f1")))
)
(defun createSampleFile(file-name count)
(let ((c 0))
(with-temp-buffer
(while (< c (* 2 count))
(setq c (1+ c))
(insert (format "line %d\n" c))
(write-file filename)
))))
(let ((c 0)(filename nil))
(while (< c 4)
(setq c (1+ c))
(setq filename (format "./test%d.txt" c))
(createSampleFile filename c)
(princ (format "[[elisp:(myfun '%s' %d)][%s]]\n" filename c filename))
))
#+END_SRC
#+RESULTS:
[[elisp:(myfun './test1.txt' 1)][./test1.txt]]
[[elisp:(myfun './test2.txt' 2)][./test2.txt]]
[[elisp:(myfun './test3.txt' 3)][./test3.txt]]
[[elisp:(myfun './test4.txt' 4)][./test4.txt]]
But when click the link, I got below prompt warning dialog always:
Can we disable that dialog?
Add below line to init.el:
(setq org-confirm-elisp-link-function nil)

(wrong-type-argument arrayp nil) on starting emacs

Emacs 24 and Mac OSX Mavericks, and my emacs file is as follows:
(setq debug-on-error t)
;; -- common-lisp compatibility if not added earlier in your .emacs
(require 'cl)
;; -- Tuareg mode -----------------------------------------
;; Add Tuareg to your search path
(add-to-list
'load-path
;; Change the path below to be wherever you've put your tuareg installation.
(expand-file-name "~/.elisp/tuareg"))
(require 'tuareg)
(setq auto-mode-alist
(append '(("\\.ml[ily]?$" . tuareg-mode))
auto-mode-alist))
;; -- Tweaks for OS X -------------------------------------
;; Tweak for problem on OS X where Emacs.app doesn't run the right
;; init scripts when invoking a sub-shell
(cond
((eq window-system 'ns) ; macosx
;; Invoke login shells, so that .profile or .bash_profile is read
(setq shell-command-switch "-lc")))
;; -- opam and utop setup --------------------------------
;; Setup environment variables using opam
(defun opam-vars ()
(let* ((x (shell-command-to-string "opam config env"))
(x (split-string x "\n"))
(x (remove-if (lambda (x) (equal x "")) x))
(x (mapcar (lambda (x) (split-string x ";")) x))
(x (mapcar (lambda (x) (car x)) x))
(x (mapcar (lambda (x) (split-string x "=")) x))
)
x))
(dolist (var (opam-vars))
(setenv (car var) (substring (cadr var) 1 -1)))
;; The following simpler alternative works as of opam 1.1
;; (dolist
;; (var (car (read-from-string
;; (shell-command-to-string "opam config env --sexp"))))
;; (setenv (car var) (cadr var)))
;; Update the emacs path
(setq exec-path (split-string (getenv "PATH") path-separator))
;; Update the emacs load path
(push (concat (getenv "OCAML_TOPLEVEL_PATH")
"/../../share/emacs/site-lisp") load-path)
;; Automatically load utop.el
(autoload 'utop "utop" "Toplevel for OCaml" t)
(autoload 'utop-setup-ocaml-buffer "utop" "Toplevel for OCaml" t)
(add-hook 'tuareg-mode-hook 'utop-setup-ocaml-buffer)
Any help fixing this error would be great. Thanks!
Nots: I do have tuareg mode where I said it is (in~/.elisp/tuareg). I started getting this error after doing the following :
opam install \
async yojson core_extended core_bench \
cohttp async_graphics cryptokit menhir
Not sure whats wrong, but I had basically the same emacs file before installing those and had no issues. Now in emacs utop also doesn't run properly when I try to evaluate an ocaml file.
As noted above:
If I had to guess, I would think that opam config env is now outputting something that is resistant to the manipulations of the function opam-vars
Run Emacs --debug-init and you will get the backtrace.
And bisect that init file, recursively, to narrow things down more.

Need help to rewrite a elisp function to use minibuffer+ido instead of ibuffer

I’m using Distel for my erlang development but the function that is used for completion is printing its output in an ibuffer. I would like to get it in the minibuffer with ido support instead, does anyone know of a way to do it?
Here is the code:
(defun erl-complete (node)
"Complete the module or remote function name at point."
(interactive (list (erl-target-node)))
(let ((win (get-buffer-window "*Completions*" 0)))
(if win (with-selected-window win (bury-buffer))))
(let ((end (point))
(beg (ignore-errors
(save-excursion (backward-sexp 1)
;; FIXME: see erl-goto-end-of-call-name
(when (eql (char-before) ?:)
(backward-sexp 1))
(point)))))
(when beg
(let* ((str (buffer-substring-no-properties beg end))
(buf (current-buffer))
(continuing (equal last-command (cons 'erl-complete str))))
(setq this-command (cons 'erl-complete str))
(if (string-match "^\\(.*\\):\\(.*\\)$" str)
;; completing function in module:function
(let ((mod (intern (match-string 1 str)))
(pref (match-string 2 str))
(beg (+ beg (match-beginning 2))))
(erl-spawn
(erl-send-rpc node 'distel 'functions (list mod pref))
(&erl-receive-completions "function" beg end pref buf
continuing
#'erl-complete-sole-function)))
;; completing just a module
(erl-spawn
(erl-send-rpc node 'distel 'modules (list str))
(&erl-receive-completions "module" beg end str buf continuing
#'erl-complete-sole-module)))))))
(defun &erl-receive-completions (what beg end prefix buf continuing sole)
(let ((state (erl-async-state buf)))
(erl-receive (what state beg end prefix buf continuing sole)
((['rex ['ok completions]]
(when (equal state (erl-async-state buf))
(with-current-buffer buf
(erl-complete-thing what continuing beg end prefix
completions sole))))
(['rex ['error reason]]
(message "Error: %s" reason))
(other
(message "Unexpected reply: %S" other))))))
The company-mode suggested by Stefan did what I wanted so no rewrite needed.
Thank you for you help and time Stefan!

Emacs, ruby: convert do end block to curly braces and vice versa

I often find myself converting code like this:
before do
:something
end
to
before { :something }
Is there a way to automate this task in emacs? I use ruby-mode and rinary, but they're not too helpful here.
ruby-mode in Emacs 24.3 and newer has the command ruby-toggle-block.
The default binding is C-c {.
I am sure it can be made shorter and better, but for now I've got the following:
(defun ruby-get-containing-block ()
(let ((pos (point))
(block nil))
(save-match-data
(save-excursion
(catch 'break
;; If in the middle of or at end of do, go back until at start
(while (and (not (looking-at "do"))
(string-equal (word-at-point) "do"))
(backward-char 1))
;; Keep searching for the containing block (i.e. the block that begins
;; before our point, and ends after it)
(while (not block)
(if (looking-at "do\\|{")
(let ((start (point)))
(ruby-forward-sexp)
(if (> (point) pos)
(setq block (cons start (point)))
(goto-char start))))
(if (not (search-backward-regexp "do\\|{" (point-min) t))
(throw 'break nil))))))
block))
(defun ruby-goto-containing-block-start ()
(interactive)
(let ((block (ruby-get-containing-block)))
(if block
(goto-char (car block)))))
(defun ruby-flip-containing-block-type ()
(interactive)
(save-excursion
(let ((block (ruby-get-containing-block)))
(goto-char (car block))
(save-match-data
(let ((strings (if (looking-at "do")
(cons
(if (= 3 (count-lines (car block) (cdr block)))
"do\\( *|[^|]+|\\)? *\n *\\(.*?\\) *\n *end"
"do\\( *|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *end")
"{\\1 \\2 }")
(cons
"{\\( *|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *}"
(if (= 1 (count-lines (car block) (cdr block)))
"do\\1\n\\2\nend"
"do\\1\\2end")))))
(when (re-search-forward (car strings) (cdr block) t)
(replace-match (cdr strings) t)
(delete-trailing-whitespace (match-beginning 0) (match-end 0))
(indent-region (match-beginning 0) (match-end 0))))))))
There are two functions to be bound to keys: ruby-goto-containing-block-start and ruby-flip-containing-block-type.
Either command works anywhere inside a block, and hopefully they can skip blocks that should be skipped - although that shouldn't be an issue if you are converting to a short block format.
The ruby-flip-containing-block-type collapses three line do .. end blocks to single line {} and vice versa. If the blocks are not exactly 3 lines and 1 line long, it should leave them alone.
I am using this on my ruby setup now, so I would appreciate improvements.
You could use a regular expression that crosses newlines.
/do(C-q C-j\?)*(.*)(C-q C-j\?)*end/
and replace with
{\2 }
Something like that could work. You could then customize it until it does exactly what you need and bind it to a macro so that you can whip it out and impress your friends anytime!
I tested the above regexes in vi (my editor of choice) and they worked. So something similar should work for you.
For more information, make sure to checkout the emacs wiki!
Here is a function. I am an elisp beginner. It only goes one way; from do to {. let me know if it works for you.

Resources