How to get a Calendar prompt and insert date - emacs

I'm writing a function that takes two dates as arguments. (something like 2016-8-1)
I would like to get the dates from the beautiful emacs calendar view.
(defun my-fun (date1 date2)
(setq date1 (get-date-from-calendar))
)
I know in Org mode there is
C-c . (org-time-stamp)
But when used in elisp code, this function expect arguments.
Any ideas?

The argument is used to pass a prefix option if you want to avoid prompting the user. You can just pass nil and it should work (org-time-stamp nil)

(defun org-read-date-example ()
"Doc-string."
(require 'org)
(let ((date-in-string-format (org-read-date nil nil nil "Date #1: "))
(date-in-time-format (org-read-date nil 'to-time nil "Date #2: ")))
(message "Date #1: %s | Date #2: %s" date-in-string-format date-in-time-format)))

Related

Lisp string formatting with named parameters

Is there a way in Lisp to format a string using named parameters?
Perhaps something with association lists like
(format t "All for ~(who)a and ~(who)a for all!~%" ((who . "one")))
in order to print "All for one and one for all".
Similar to this python question, or this scala one, or even c++, but in Lisp.
If this functionality isn't in the language, does anyone have any cool functions or macros that could accomplish the same thing?
Use CL-INTERPOL.
(cl-interpol:enable-interpol-syntax)
String interpolation
For simple cases, you don't need FORMAT:
(lambda (who) #?"All for $(who) and $(who) for all!")
Then:
(funcall * "one")
=> "All for one and one for all!"
Interpret format directives
If you need to format, you can do:
(setf cl-interpol:*interpolate-format-directives* t)
For example, this expression:
(let ((who "one"))
(princ #?"All for ~A(who) and ~S(who) for all!~%"))
... prints:
All for one and "one" for all!
If you are curious, the above reads as:
(LET ((WHO "one"))
(PRINC
(WITH-OUTPUT-TO-STRING (#:G1177)
(WRITE-STRING "All for " #:G1177)
(FORMAT #:G1177 "~A" (PROGN WHO))
(WRITE-STRING " and " #:G1177)
(FORMAT #:G1177 "~S" (PROGN WHO))
(WRITE-STRING " for all!" #:G1177))))
Alternate reader function
Previously, I globally set *interpolate-format-directives*, which interprets format directive in all interpolated strings.
If you want to control precisely when format directives are interpolated, you can't just bind the variable temporarily in your code, because the magic happens at read-time. Instead, you have to use a custom reader function.
(set-dispatch-macro-character
#\#
#\F
(lambda (&rest args)
(let ((cl-interpol:*interpolate-format-directives* t))
(apply #'cl-interpol:interpol-reader args))))
If I reset the special variable to its default value NIL, then strings where directives are formatted are prefixed with #F, whereas normal interpolated ones use the #? syntax. If you want to change readtables, have a look at named readtables.

How do I hard-code arguments in emacs lisp?

I have the following function in my .emacs that notifies me after I've worked for a suitable amount of time.
Problem is, I'm unable to hardcode the values time and msg, so I have to reenter them each time.
(defun timed-notification(time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time
nil
(lambda (msg) (terminal-notifier-notify "Pomodoro" msg))
msg))
(setq column-number-mode t)
How do I set the time to always be "25 min" and the message to be "Take a break, time's up!"?
Here is my attempt:
(defun timed-notification()
;(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time 25
nil
(lambda ("Time's up")
(terminal-notifier-notify "Take a break, time's up!" msg))
msg))
(setq column-number-mode t)
Define your function like you did originally, then invoke it once with the parameters you want. The interactive form, like its name suggests, is only used when you actually invoke the function interactively. When you invoke it from code, you pass the parameters; so the interactive form is simply ignored.
(defun timed-notification (time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)
(timed-notification 25 "Take a break, time's up!") ;; New addition
You got rid of the msg parameter, but you were still trying to use it. Use let to bind a local variable to that value.
(defun timed-notification()
(interactive)
(let ((msg "Take a break, time's up!"))
(run-at-time 25 nil (lambda (mess) (terminal-notifier-notify "pomodoro" mess)) msg)))
(defun timed-notification (time msg)
(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
(run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)
(defun tf()
(interactive)
(timed-notification "1 min" "Take a break, time's up!"))
Now tf can be called for a regular pomo, while the original function is still available for when I want an x-minute break.

print only text discarding text properties

I have the following function to print the line where point is to the *scratch* buffer,
(defun print-line ()
(print (thing-at-point 'line) (get-buffer "*scratch*")))
but it prints even the fontified info like this
#(" OFFICE
" 0 2 (fontified t org ...
How to discard the printing of the fontified info.
To expand on Daimrod's mention of buffer-substring-no-properties...
M-x apropos RET no-properties RET
buffer-substring-no-properties
Function: Return the characters of part of the buffer, without the
text properties.
field-string-no-properties
Function: Return the contents of the field around POS, without text
properties.
insert-buffer-substring-no-properties
Function: Insert before point a substring of BUFFER, without text
properties.
match-string-no-properties
Function: Return string of text matched by last search, without text
properties.
minibuffer-contents-no-properties
Function: Return the user input in a minibuffer as a string, without
text-properties.
substring-no-properties
Function: Return a substring of STRING, without text properties.
You can read about text properties in the manual:
M-: (info "(elisp) Text Properties") RET
I needed something similar for eredis when manipulating strings from an org-table. You can use `set-text-properties' to get rid of them when displaying the string.
(defun strip-text-properties(txt)
(set-text-properties 0 (length txt) nil txt)
txt)
(defun print-line ()
(print (strip-text-properties
(thing-at-point 'line))
(get-buffer "*scratch*")))
I've tried some things but it's weird, I don't really understand how text properties work.
For example:
(type-of (thing-at-point 'line)) => string
As you've said if one tries to print it, the properties are printed as well, but if one tries to insert it:
(insert (format "%s" (thing-at-point 'line)))
Only the string is printed, not the properties.
So it seems to me that those properties are just bound to the string but you can manipulate the string as usual:
(lenght (thing-at-point 'line))
(substring (thing-at-point 'line) 0 2)
However, if all you want is the line, and the line only you can use buffer-substring-no-properties:
(defun print-line ()
(print (buffer-substring-no-properties (point-at-bol) (point-at-eol))))

How do you have a process in emacs return value as a function?

I'm trying to write emacs tools that require sending data to an external process, my example is a REPL as part of an inferior-lisp.
How do you get the output of the process to be returned as if it were an emacs function?
I would like something like:
(defun get-data ()
(process-send-string (get-process) "foo command")
(get-data-output-from-process (get-process)))
I've tried using process-send-string and accept-process-output-proc but the output always gets sent to the inferior-lisp. I would like something to return data to the function that was called so I can manipulate the data. Also if possible I would like to not copy the output to the inferior-lisp buffer.
Generally, you want to read up on running EMACS subprocesses. There are several useful functions for getting information from a subprocess.
Here's an example with call-process:
;; results go into current buffer
(call-process "pwd" nil t)
/nishome/crmartin ; there's the results
0 ; return value is value from exit
(call-process "false" nil t)
1 ; false(1) always returns non-0
;; results go into a buffer named "bletch", creating it if needed.
(call-process "pwd" nil "bletch" t)
0
To try this, type the elisp code into scratch and run with C-j.
The only way I could get this to work was to store the results in a global, then pass it back in the function.
(defvar ac-dj-toolkit-current-doc nil "Holds dj-toolkit docstring for current symbol")
(defun dj-toolkit-parse (proc text)
(setq ac-dj-toolkit-current-doc text)
text)
(defun dj-toolkit-doc (s)
(let ((proc (inferior-lisp-proc)))
(process-send-string (inferior-lisp-proc)
(format "(clojure.repl/doc %s)\n"
s))
(set-process-filter proc 'dj-toolkit-parse)
(accept-process-output proc 1)
(set-process-filter proc nil)
ac-dj-toolkit-current-doc))

Why might the Emacs "downcase" function refuse to do downcasing?

I'm trying to write simple Emacs function to convert ids between C style ones and camelCase ones (i.e. c_style <-> cStyle). But for some reason, Emacs built in downcase function leaves the word intact. M-x downcase-word works fine so I completely lost. Any ideas are welcome.
(defun toggle-id-style ()
"Toggle between C-style ids and camel Case ones (i.e. c_style_id -> cStyleId and back)."
(interactive)
(save-excursion
(progn
(re-search-forward "[^A-Za-z0-9_]" nil t)
(let ((end (point))
(case-fold-search nil))
(progn
(re-search-backward "[^A-Za-z0-9_]" nil t)
(let* ((cstyle (if (string-match "_" (buffer-substring-no-properties (point) end)) t nil))
(regexp (if cstyle "_\\(\\w+\\)" "\\([A-Z][a-z0-9]+\\)") )
(func (if cstyle 'capitalize (lambda (s) (concat "_" (downcase s) ) ))))
(progn
(while (re-search-forward regexp end t)
(replace-match (funcall func (match-string 1)) nil nil)))))))))
;;M-x replace-regexp _\(\w+\) -> \,(capitalize \1) ;; c_style -> cStyle
;;M-x replace-regexp \([A-Z][a-z0-9]+\) -> _\,(downcase \1) ;;cStyle -> c_style
It works fine if I convert c_style but when I'm trying to convert cStyle I got c_Style as result. Yes, I've checked that this is due to downcase behaviour.
Your problem is the second argument to replace-match. From the documentation:
If second arg fixedcase is non-nil, do not alter case of replacement text.
Otherwise maybe capitalize the whole text, or maybe just word initials,
based on the replaced text.
If the replaced text has only capital letters
and has at least one multiletter word, convert newtext to all caps.
Otherwise if all words are capitalized in the replaced text,
capitalize each word in newtext.
You're passing nil for the fixedcase argument, which causes replace-match to capitalize the replacement when the text being replaced is capitalized. Pass t instead and this bit of the code will work.
I have two general comments about your code.
All of your uses of progn are unnecessary. The body of save-excursion is an implicit progn and so are the bodies of let and let*.
You search forwards and then backwards to try to find the bounds of the symbol underneath point. Emacs already has a thingatpt library to find things at or near the point. In your case you can just call (bounds-of-thing-at-point 'symbol) which returns a cons cell (START . END) giving the start and end positions of the symbol that was found.
I think you need the second arg of replace-match to be t instead of nil.