How to insert date only in org-mode - emacs

I can insert an inactive timestamp interactively with org-time-stamp-inactive (C-c C-!), then pressing Enter when presented with the calendar. This inserts a value like [2021-12-10 Fri].
I would like to know if this can be achieved programmatically with org-time-stamp-inactive. I tried org-time-stamp-inactive '(16) but this also inserts a time which I do not want. I can do insert (format-time-string "[%Y-%m-%d %a]") but this uses a different approach, and I would like to know if what I want can be achieved with org-time-stamp-inactive.

EDIT (in response to comment): No, it is not possible, but you got to look at the code to make sure. org-time-stamp-inactive is a thin wrapper around org-time-stamp. That in turn calls org-insert-time-stamp, which takes a time and a with-hm argument (among others - see the doc string of the function with C-h f org-insert-time-stamp for the details). If with-hm is nil, then no time is printed. But org-time-stamp (and therefore org-time-stamp-inactive) always calls org-insert-time-stamp with a non-nil with-hm, so the time is always inserted in that case. The best you can do is call the underlying function org-insert-time-stamp like so: (org-insert-time-stamp (current-time) nil 'inactive).
I still think your simple suggested solution (elaborated a bit in the original answer below) is the best way to go about the problem.
[ORIGINAL ANSWER]
Org mode is plain text: things may appear differently, but all that is smoke and mirrors: you can cat an Org mode file in the terminal and see precisely what it contains. In particular, inactive time data are just strings of the form [2021-12-11]. So there is no penalty if you do it manually, instead of going through some Org mode interfaces: the end result is the same, so your manually inserted date is just as good as the one inserted by org-time-stamp-inactive.
So assuming that the simple solution you suggested is acceptable, here's filling in the details:
(defun my/org-insert-current-time-as-inactive-time-stamp ()
(interactive)
(insert (format-time-string "[%Y-%m-%d]")))
(define-key org-mode-map (kbd "C-c _") #'my/org-insert-current-time-as-inactive-time-stamp)
C-c _ was undefined in the Org mode keymap in my case, but YMMV: choose something that is not used.

Related

emacs > org mode > agenda - always use current buffer

I am using many different org mode files for various projects, and I rev them by adding the date to the filename eg filename-2020-09-17.org. I realize I could use version control but in this case that is not possible, due to needing to share the file with others who are not using VC.
I would like the Agenda to always show just the items for the current file/buffer.
When I save eg the file with filename-2020-09-16.org to filename-2020-09-17.org, then the agenda still shows the old file name unless I remove it from the agenda file list and add the new file.
I realize that I can use C-c a < a but I am lazy and would rather not have to type S-, each time to get the <.
I looked at
Agenda view of the current buffer
And the OP says the solution was simple but he/she/they did not provide the solution - at least I don't see it and I tried the posted code but it no works.
I also found https://www.reddit.com/r/orgmode/comments/bxwovd/agenda_for_current_buffer/ but that did not seem to meet my need.
Specifically I would like to put something in .emacs so that this would apply to all files all the time.
I also looked into a keystroke macro programs but this does not seem ideal.
Any help is appreciated.
Thanks ahead of time.
Here's a simple function to do what you want, but there is no error checking to make sure e.g. that you are invoking it from a buffer that is visiting an Org mode file. The idea is that you set the org-agenda-files list to contain just the file which the buffer is visiting and then you call the regular org-agenda function. Binding the modified function to the C-c a key may or may not be what you want to do, but you can try and decide for yourself:
(defun org-agenda-current-buffer ()
(interactive)
(let ((org-agenda-files (list (buffer-file-name (current-buffer)))))
(org-agenda)))
(define-key global-map (kbd "C-c a") #'org-agenda-current-buffer)

how to prevent helm-swoop from returning symbol at point?

I would like to invoke M-x helm-swoop such that it does NOT return the symbol at point. I find that it always picks up undesired symbols, e.g. when I invoke M-x helm-swoop in org-mode, I get Swoop: \*, and I then have to delete the \* before I can enter my desired search term. How can I do this?
This has been bugging me as well, for exactly the same reason (swoop in an Org-mode buffer picking up heading characters), so this question motivated me to go and look.
Looking at the source, Helm-swoop calls helm-swoop-pre-input-function to populate the prompt, and by default this is set to a function that returns (thing-at-point 'symbol), which is what causes the problem in headings.
A quick test with the following in my init file seems to work:
(setq helm-swoop-pre-input-function
(lambda () nil))
This could certainly be improved by, for example, keeping the default behaviour in non-Org buffers but as I'm only really using swoop in Org buffers this is good enough for me.

How to prompt the user for a block of text in elisp?

read-from-minibuffer is a great way to prompt a user for a single line of text. How do I prompt a user for a large block of multi-line text in elisp?
This is what I'm thinking, but I don't know if it's the smoothest approach:
create a temporary buffer (via with-temporary-buffer?)
seed the buffer with some default text
display the buffer
tell the user, "edit the text as you see fit, then hit <some key sequence> to indicate that you are done" (perhaps via header-line-format)
wait for the user to hit the key sequence
collect the buffer text and put it in a variable (via buffer-string)
destroy the temporary buffer and restore the window layout as it was before
do stuff with the text
(defun my-read-mb-lines (prompt some-keyseq)
(let ((keymap (copy-keymap minibuffer-local-map)))
(define-key keymap (kbd "RET") 'newline)
(define-key keymap some-keyseq 'exit-minibuffer)
(read-from-minibuffer prompt nil keymap)))
Calling example:
(my-read-mb-lines "Insert text (C-s to submit): " (kbd "C-s"))
The 'let' block creates a local copy of the minibuffer's default keymap. The next two calls to "define-key" modify the keymap copy. Afterward, "read-from-minibuffer" passes the modified keymap for the minibuffer to use while prompting the user (instead of its default keymap, "minibuffer-local-map").
FWIW, C-j is mapped to "exit-minibuffer" by default and a simplified version can be written:
(defun my-simplified-read-mb-lines (prompt)
(let ((keymap (copy-keymap minibuffer-local-map)))
(define-key keymap (kbd "RET") 'newline)
(read-from-minibuffer prompt nil keymap)))
Calling example:
(my-simplified-read-mb-lines "Insert text (C-j to submit): ")
I suppose it depends on the exact use-case?
There are no shortage of examples of your proposed approach working very nicely (e.g. writing a VCS commit message), so there's certainly nothing wrong with that -- it's tried and true. In addition, if it really is a large (or simply not small) block of text, then I suspect that providing a normal buffer to edit in may provide the nicest experience for the user.
If you're talking about collecting multiple input fields including a multi-line field, then the widget-based approach (as suggested by wvxvw) would enable you to put everything in a single buffer, which might also be desirable.
Or you could use the mail-like approach of using a single buffer for multiple fields, and then parse the results afterwards.
First of all Emacs 23.4 is very old. You should upgrade.
The work-flow you describe is what org-mode uses to edit source blocks.
Org-mode is included in Emacs 24.
See the source of org-edit-special for how it works.
It does a bit more than you need.
Basically, you want to set up a minor-mode for the created buffer that has a
binding to gather the text and restore window configuration.
I've written ges to edit arbitrary blocks in a new buffer using org-mode
machinery, but it's more complex than what you need.

Emacs / Slime Key Binding / Sending command to Swank Server

I'm familiar with scheme, but new to emacs (switching over from VIM) and elisp.
I know how to do the following:
make a simple key binding
C-c iwb = indent whole buffer
F2 = turns folding on/off
use slime from emacs
some basic keys, like C-x 2, paredit keys, some basic movement keys
I need help doing something a bit more advanced:
I want F3 to equal:
put emacs into C-x 2 mode
in bottom window, switch to "slime-repl" buffer
in the "slime-repl" buffer, send the command "(test/run)" <-- note, this is meant to be sent to the swank server, NOT to elisp
I realize it's terrible form to ask people to write a script for me; however, if anyone could do that, I would learn rather quickly from it. [And it would allow me to do more complicated types of scripting through studying your example.]
Thanks!
This is not exactly what you want, but should be a good starting point for further tweaking:
(defun slime-run-test ()
(interactive)
(slime-interactive-eval "(test/run)")
(slime-pop-to-buffer (slime-output-buffer) t))
(global-set-key (kbd "<f3>") 'slime-run-test)
I don't use slime, but assuming it uses comint-mode then I would think the following might do the trick:
(defun my-slime-test-run ()
(interactive)
(delete-other-windows)
(split-window-below)
(with-selected-window (next-window)
(switch-to-buffer "slime-repl")
(goto-char (point-max))
(insert "(test-run)")
(comint-send-input)))
(global-set-key (kbd "<f3>") 'my-slime-test-run)
There is probably a better way to do this, but hopefully that gives you a little insight into how you can write elisp functions to carry out tasks in the editor (and note how the function reads very much like a set of editor instructions -- you can do a lot simply by converting the keystrokes you would use into equivalent code -- or even not writing code at all, and simply recording & saving keyboard macros).
Use C-hf name-of-the-function RET to get documentation on any of the function/macro calls in that function.
For the keybinding, I used C-hkF3 to check how Emacs referred to that key, and then used that string as the argument to kbd (and note how you can use that sequence to find the name of the function bound to any given key sequence, which you can then utilise in code if desired).
Many things are far less obvious if you don't already know them, but that's only to be expected with a code base as large as this (and dating back as long as this).
The great thing is that if you don't know what you're looking for, you can always search for function names matching patterns with C-uC-ha (and similarly for variables, values, libraries, and documentation; see M-: (info "(emacs) Apropos") RET for more about this facility). Plus the info manuals (complete with indexes -- press I or i within any particular manual, or use the info-apropos command to search all info manuals at once).
Truly one of the very best things you can do is to learn how to use the self-documenting nature of Emacs to find answers to the things you don't already know.

How to make flyspell bypass some words by context?

I use Emacs for writing most of my writings. I write using reStructuredText, and then transform them to LaTeX after some preprocessing since I write my citations รก-la LaTeX. This is an excerpt of one of my texts (in Spanish):
En \cite[pp.~XXVIII--XXIX]{Crnkovic2002} se brindan algunos riesgos
que se pueden asumir con el desarrollo basado en componentes, los
This text is processed by some custom scripts that deals with the \cite part so rst2latex can do its job.
When I activate flyspell-mode it signals most of the citation keys as spelling errors.
How can I tell flyspell not to spellcheck things within \cite commands.
Furthermore, how can I combine rst-mode and flyspell, so that rst-mode would keep flyspell from spellchecking the following?
reST comments
reST code literal
reST directive parameters and arguments
reST raw directive contents
Any ideas?
You can set the variable ispell-parser to the value 'tex so that flyspell will ignore (la)tex sequences. To do so, you can either set it manually in each buffer like so:
M-: (setq 'ispell-parser 'tex)
or you write a little function that will do that for you. Put the following in your .emacs file:
(defun flyspell-ignore-tex ()
(interactive)
(set (make-variable-buffer-local 'ispell-parser) 'tex))
Then you can still invoke it manually, using
M-x flyspell-ignore-tex
or you could add a hook that calls that function automatically whenever you edit a file of a certain type. You would do the latter by adding the newly defined function to your auto-mode-alist. Say your filenames typically end with ".rst", then add this line to your .emacs file:
(add-to-list 'auto-mode-alist '("\\.rst$" . flyspell-ignore-tex))
As for the second part of your question: making flyspell-mode ignore larger regions, such as, e.g., reST comments, is not easily achievable. It becomes clear when you think about the way flyspell works: it checks text on a word-by-word basis. For that, flyspell-word only looks at one word at a time which it sends to an ispell process running in the background. The ispell process does the dictionary lookup and returns whether or not the current word is correct. If flyspell-word had to check every single time whether or not the current word is part of a comment or other region that should not be checked, it would be rather slow, because that would include quite a bit of searching through the buffer.
Now of course, one could approach this a little bit smarter and first find the non-comment regions etc. and then do the word-by-word checking only in those parts that are outside of those regions - but unfortunately, that's not the way flyspell is implemented.
If you can do without the "fly" part, however, ispell-mode has a mechanism to customize which regions of a buffer can be skipped. This is done via the variable ispell-skip-region-alist. But although flyspell-mode works off ispell-mode, for the reasons outlined above that variable is not used by flyspell-mode.
You can also use flyspell-generic-check-word-predicate as I explained in this question at Super User.
(aspell's tex filter may do exactly what you want - but if you want a more general solution)
Although I am using the code below to persuade flyspell to not flag certain words with numbers in them,
you can use this sort of hook to match certain context.
looking-at starts at the position you want - so you may want to search backwards for start/end of whatever context you care about.
(when "another attempt to accept certain words flyspell/ispell/aspell flags as incorrect"
(defun flyspell-ignore-WordNumber99-stuff/ag (beg end info)
(save-excursion
(goto-char beg)
(cond
((or
(looking-at "\\bWord1\\b")
(looking-at "\\bWord99Foo\\b")
)
t)
(t nil)
)
)
)
)
(add-hook 'flyspell-incorrect-hook 'flyspell-ignore-WordNumber99-stuff/ag)