When i use emacs' lisp indentation, the closing brackets are moved right leaving bigger space infront than necessary. this is how code looks like,
And also, in my code in 2nd line, it leaves 2 characters, in 3rd line, it leaves 4 characters... in some places, it is backward indented.....
note: please don't try to decrypt the code, just see how bad the indentation looks like....
(defun tern-req (port doc c)
(let*
(
(url-mime-charset-string nil) ; Suppress huge, useless header
(url-request-method "POST"
)
(deactivate-mark nil
) ; Prevents json-encode from interfering with shift-selection-mode
(url-request-data
(json-encode doc
)
)
(url-show-status nil)
(url (
url-parse-make-urlobj "http" nil nil tern-server port "/" nil nil nil
)
)
)
(url-http url #'tern-req-finished
(list c)
)
)
)
Lisp code formatting wars are virtually nonexistent because Emacs is the accepted standard. You should leave the closing parens on the same line, though. A space before a closing paren indicates it's for a bigger block which started on a previous line.
(defun tern-req (port doc c)
(let* ((url-mime-charset-string nil) ; Suppress huge, useless header
(url-request-method "POST")
;; Prevent json-encode from interfering with shift-selection-mode
(deactivate-mark nil)
(url-request-data
(json-encode doc))
(url-show-status nil)
(url
(url-parse-make-urlobj "http" nil nil tern-server port "/" nil nil nil)) )
(url-http url #'tern-req-finished
(list c)) ))
Related
Pretty straightforward, but I can't seem to find an answer. I have a string of 1s and 0s such as "01001010" - how would I parse that into a number?
Use string-to-number, which optionally accepts the base:
(string-to-number "01001010" 2)
;; 74
As explained by #sds in a comment, string-to-number returns 0 if the conversion fails. This is unfortunate, since a return value of 0 could also means that the parsing succeeded.
I'd rather use the Common Lisp version of this function, cl-parse-integer. The standard function is described in the Hyperspec, whereas the one in Emacs Lisp is slightly different (in particular, there is no secondary return value):
(cl-parse-integer STRING &key START END RADIX JUNK-ALLOWED)
Parse integer from the substring of STRING from START to END. STRING
may be surrounded by whitespace chars (chars with syntax ‘ ’). Other
non-digit chars are considered junk. RADIX is an integer between 2 and
36, the default is 10. Signal an error if the substring between START
and END cannot be parsed as an integer unless JUNK-ALLOWED is non-nil.
(cl-parse-integer "001010" :radix 2)
=> 10
(cl-parse-integer "0" :radix 2)
=> 0
;; exception on parse error
(cl-parse-integer "no" :radix 2)
=> Debugger: (error "Not an integer string: ‘no’")
;; no exception, but nil in case of errors
(cl-parse-integer "no" :radix 2 :junk-allowed t)
=> nil
;; no exception, parse as much as possible
(cl-parse-integer "010no" :radix 2 :junk-allowed t)
=> 2
This thread has an elisp tag. Because it also has a lisp tag, I would like to show standard Common Lisp versions of two solutions. I checked these on LispWorks only. If my solutions are not standard Common Lisp, maybe someone will correct and improve my solutions.
For solutions
(string-to-number "01001010" 2)
and
(cl-parse-integer "001010" :radix 2)
LispWorks does not have string-to-number and does not have cl-parse-integer.
In LispWorks, you can use:
(parse-integer "01001010" :radix 2)
For the solution
(read (concat "#2r" STRING))
LispWorks does not have concat. You can use concatenate instead. read won't work on strings in LispWorks. You have to give read a stream.
In LispWorks, you can do this:
(read (make-string-input-stream (concatenate 'string "#2r" "01001010")))
You can also use format like this:
(read (make-string-input-stream (format nil "#2r~a" "01001010")))
This seems hacky by comparison, but FWIW you could also do this:
(read (concat "#2r" STRING))
i.e. read a single expression from STRING as a binary number.
This method will signal an error if the expression isn't valid.
I'm trying to write a skeleton-function to output expressions in a loop. Out of a loop I can do,
(define-skeleton test
""
> "a")
When I evaluate this function it outputs "a" into the working buffer as desired. However, I'm having issues when inserting this into a loop. I now have,
(define-skeleton test
"A test skeleton"
(let ((i 1))
(while (< i 5)
>"a"
(setq i (1+ i)))))
I would expect this to output "aaaaa". However, instead nothing is outputted into the working buffer in this case. What is happening when I insert the loop?
The > somestring skeleton dsl does not work inside lisp forms.
You can however concatenate the string inside a loop:
(define-skeleton barbaz
""
""
(let ((s ""))
(dotimes (i 5)
(setq s (concat s "a")))
s)
)
My understanding is that code such as
> "a"
only works at the first nesting level inside a skeleton.
[EDIT] Regarding your question
What is happening when I insert the loop?
The return value of the let form (that is, the return value of the while form)is inserted. I do not know why it does not raise an error when evaluating > "a", but the return value of a while form is nil, so nothing is inserted.
I do agree that there's not much point using define-skeleton if you're going to need an (insert function within the skeleton.
This is also a rather trivial example to be using define-skeleton.
That said, they are often easier to read than a defun and useful when you want to create a function that inserts text (and optionally, takes input).
For example you may wish to have a different character repeated a set no. of times... Below, str refers to the argument supplied with the function (usually a string) and v1, v2 are the default names for local variables in a skeleton. Thus:
(define-skeleton s2 ""
nil ; don't prompt for value of 'str'
'(set 'v1 (make-string 5 (string-to-char str)))
\n v1 \n \n)
Below, calling the function leads to a newline, the string, then leaves the cursor at the position indicated by the square brackets [].
(s2 "a")
aaaaa
[]
The default printing in emacs is to print one page per paper with some margins.
I have this function that changes the margins and sets printing to two pages per paper "most numbers are to maximize printing space:
(defun ps-two-per-page ()
(interactive)
(require 'ps-print)
(setq ps-n-up-printing 2
ps-n-up-border-p nil
ps-paper-type 'letter
ps-font-size (quote (8 . 11))
ps-top-margin -20
ps-bottom-margin -35
ps-left-margin 18
ps-right-margin 18
ps-n-up-margin 1
ps-inter-column 1
)
'ps-two-per-page)
Once the function executes I will no longer able to go back to the default printing configuration.
How is it possible for me to go back to print using default configuration "as if I did not execute the above command"?
Also, is there way to bind the printing commands in emacs under the "file" drop down menu.
Meaning, I would like to bind "Postscript Print Buffer" to be print using the default configuration, and "Postscript Print Buffer (B+W)" to follow the configuration I have in the above command ps-two-per-page.
WRT to first question: while ignoring the details of ps-print, in these cases two strategies are to adopt
1) set variables behind a let
2) store old values with a prefix old-... and reset afterward.
Here a draft of the second way:
(defun ps-two-per-page ()
(interactive)
(require 'ps-print)
(setq old-ps-n-up-printing ps-n-up-printing
old-ps-n-up-border-p ps-n-up-border-p
old-ps-paper-type ps-paper-type
old-ps-font-size ps-font-size
old-ps-top-margin ps-top-margin
old-ps-bottom-margin ps-bottom-margin
old-ps-left-margin ps-left-margin
old-ps-right-margin ps-right-margin
old-ps-n-up-margin ps-n-up-margin
old-ps-inter-column ps-inter-column
ps-n-up-printing 2
ps-n-up-border-p nil
ps-paper-type 'letter
ps-font-size (quote (8 . 11))
ps-top-margin -20
ps-bottom-margin -35
ps-left-margin 18
ps-right-margin 18
ps-n-up-margin 1
ps-inter-column 1))
(defun ps-restore-default ()
"Get old values back"
(interactive)
(require 'ps-print)
(setq ps-n-up-printing old-ps-n-up-printing
ps-n-up-border-p old-ps-n-up-border-p
ps-paper-type old-ps-paper-type
ps-font-size old-ps-font-size
ps-top-margin old-ps-top-margin
ps-bottom-margin old-ps-bottom-margin
ps-left-margin old-ps-left-margin
ps-right-margin old-ps-right-margin
ps-n-up-margin old-ps-n-up-margin
ps-inter-column old-ps-inter-column))
I have a function that uses (case (read-event) . . .) -- I have been unable to get down-mouse-1 to equal an integer for the duration of the function. The following is an example where down-mouse-1 yields a result of Try again instead of Hello world. All of the following examples work, except for down-mouse-1: ('f12 516); (?\s-k 517); ('f3 518); ('C-tab 519); ('C-M-s-right 520); (?m 522).
(let* (
(test (case (read-event)
('down-mouse-1 9999))))
(cond
((eq test 9999)
(message "Hello world."))
(t (message "Try again."))))
read-event never returns down-mouse-1. For a mouse click, the first event it will return will look like (down-mouse-1 ...). So you could do:
(pcase (read-event)
(`(down-mouse-1 . ,_) 9999))
Note that in my experience, 99% of the uses of read-event would be better rewritten some other way.
Not clear to me what you are trying to do. But you should not quote the keys in a case clause. E.g, use down-mouse-1, not 'down-mouse-1.
I understand that the error message means there are no todo entries that mach the search criteria:
org-agenda-find-same-or-today-or-agenda: Args out of range: 0, 0
Is there any way to generate a prettier message instead:
(if (and (eq major-mode 'org-agenda-mode)
(org-agenda-find-same-or-today-or-agenda: Args out of range: 0, 0) )
(message "There are no events that match your search criteria."))
You can use condition-case to handle that standard error:
(if (and (eq major-mode 'org-agenda-mode)
(condition-case err
(org-agenda-find-same-or-today-or-agenda ...) ; Whatever args you need to pass.
(args-out-of-range (error "There are no events that match your search criteria"))
(error (error (error-message-string err)))))
...)
However, it sounds like you should correct the code so that that error is prevented, instead. Can't you test the argument(s) to org-agenda-find-same-or-today-or-agenda before you call it, and call it only if the argument(s) is/are valid?