Numerate the org headings - elisp

I want to numerate the org-headings from:
* heading how
* heading are
* heading you
to
* 1.heading how
* 2.heading are
* 3.heading you
So I compose a numerate-org-heading as:
(defun numerate-org-heading (args)
"Numerate org headings"
(interactive "s")
(let ((count 0))
(while (re-search-forward args nil t)
(setq count (1+ count))
(replace-match (concat "\1" (string count)
(forward-char 1))))))
Unfortunately, It report error of Wrong type argument: stringp, ^* when i call numerate-org-heading interactively and feed it with string "^*".
Could you please give any hints to solve the problem?
Modified the function and it works now for particular cases:
(defun numerate-org-heading (args)
"Numerate org headings"
(interactive "s")
(let ((count 0))
(while (re-search-forward args nil t)
(setq count (1+ count))
(replace-match (concat "* " (number-to-string count) ".")
(forward-char 1))))))

If you set args as a matching group and capture any trailing space, you can replace that back as group 1 ("\\1") followed by your numeration e.g. 1.
(defun numerate-org-heading (args)
"Numerate org headings"
(interactive "s")
(let ((count 0))
(while (re-search-forward (concat "\\(" args "[ \\t]*\\)") nil t)
(setq count (1+ count))
(replace-match (concat "\\1" (number-to-string count) ".")))))

Related

how to handle this error generated in doing sicp exercise 4.9?

I am doing the exercise.4.9 of the sicp and I am trying to implement a syntax of the "for statement" which looks like the others would see in the c++:
(for (((i 0) (j 1))
(< (+ i j) 10)
((i (+ i 1))))
(display "i:")
(display i)
(display "\n")
(display "j:")
(display j)
(display "\n"))
the syntax looks like:
(for ((initial-statement) (predicate-statement) (updating-statement)) (for-body))
and what I have generated is like:
((lambda ()
(define j 1)
(define i 0)
(define inner-loop
(if (< (+ i j) 10)
(begin (display "i:")
(display i)
(display "\n")
(display "j:")
(display j)
(display "\n")
(set! i (+ i 1))
(inner-loop))))
(inner-loop)))
And I got an error saying that the first inner-loop invocation was trying to approach an unbound variable,
I am wondering what does the correct code I should generate look like?
As #Rainer mentioned in the comments, your definition of inner-loop is incorrect.
A function in scheme is defined as: (define (name ...args) body)
Or if there are no arguments: (define (name) body)
The following works:
((lambda ()
(define j 1)
(define i 0)
(define (inner-loop)
(if (< (+ i j) 10)
(begin (display "i:")
(display i)
(display "\n")
(display "j:")
(display j)
(display "\n")
(set! i (+ i 1))
(inner-loop))))
(inner-loop)))

Serialize scheme object to string

When doing (display obj), a nice representation is shown to the output. But is it possible to capture this representation to a string?
I could use this to better handle debug information.
The closest I could get is to display the object to a .txt, and then read it back as a string:
(define (to-string obj)
(call-with-output-file "to-string.txt"
(lambda (output-port)
(display obj output-port)))
(call-with-input-file "to-string.txt"
(lambda (input-port)
(define str "")
(let loop ((x (read-char input-port)))
(if (not (eof-object? x))
(begin
(set! str (string-append str (string x)))
(loop (read-char input-port))))
str)))
)
(define obj (cons "test" (make-vector 3)))
(define str (to-string obj))
; str will contain "{test . #(0 0 0)}"
Found the answer thanks to #soegaard!
(define (to-string obj)
(define q (open-output-string))
(write obj q)
(get-output-string q)
)
(define obj (cons "test" (make-vector 3)))
(define str (to-string obj))
; str will contain ("test" . #(0 0 0))

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)

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!

Processing command-line-args-left?

I have a function in which I pass a filename followed by several integer parameters. The problem is that I now want to run my code as a Unix script, using command-line-args-left to pass parameters from the command line. When #1 calls process-args, a list is created with all of the values. In #2, a list of a list {eg. ((1 2 3)) } is created upon entry to process-args. What is the best way to keep my code generic so I can handle both cases #1 and #2 in the same function?
(defun process-args (filename &rest cols) ...)
(process-args filename 1 2 3); #1
(process-args (car command-line-args-left) (cdr command-line-args-left)); #2
Here is some working sample code with which I'm testing:
#!/usr/bin/emacs --script
(defun process-args (filename &rest cols)
(princ (concat "Script Name: " file "\n"))
(princ (concat "File parameter: " filename "\n"))
(princ "Other arg values: ")
(princ cols)
(princ "\nIs list: ")
(princ (listp cols))
(princ "\n----------\n")
(while cols
(princ (car cols))
(princ "...")
(setq cols (cdr cols)))
(princ "\n"))
(print "===== Version #1: Base case - becomes (1 2 3) =====")
(process-args (car command-line-args-left) 1 2 3)
(print "===== Version #2: Passing cdr of list as one string =====")
(process-args (car command-line-args-left) (mapconcat 'identity (cdr command-line-args-left) " "));
(print "===== Version #3: Test of list of list - becomes ((1 2 3)) =====")
(process-args (car command-line-args-left) '(1 2 3))
You could try using 'apply to flatten out the last argument (the last argument to 'apply' is a list of arguments, use'funcall` if you don't have the last bit in a list).
So, version #3 above would be handled so:
(apply 'process-args (car command-line-args-left) '(1 2 3))
(The other invocations wouldn't change.)

Resources