How to use the function option of org-capture correctly? - emacs

I want to dynamically open the correct file in an orgmode capture template using the function option:
("a" "foo" plain
(function my-visit-timestamped-file)
"<some content>")
Function my-visit-timestamped-file is defined as
(defun my-visit-timestamped-file ()
(interactive)
(let
((theDate (format-time-string "%Y%m%d-%H%M.org")))
(find-file (concat "<some_path>" theDate))))
If I run the capture template a, emacs opens the file <some_path>theDate in a buffer and opens the capture buffer with the file.
Thus my window is split into 2 buffers showing the same content.
Can function my-visit-timestamped-file be changed somehow such that the buffer is not opened but org capture still gets the correct file pointer/file handle?

The answer by #jpkotta pointed me in the right direction.
The bug was gone, but instead the content of the capture buffer was always pasted into the buffer I was currently editing.
In an old thread on the org-mode mailing list I found the answer to my question. The function should be:
(defun my-visit-timestamped-file ()
"Visit a new file named by the current timestamp"
(interactive)
(let* (
(curr-date-stamp (format-time-string "%Y%m%d-%H%M.org"))
(file-name (expand-file-name curr-date-stamp "/some/path/")))
(set-buffer (org-capture-target-buffer file-name))
(goto-char (point-max))))
Link to mailing list thread: https://lists.gnu.org/archive/html/emacs-orgmode/2013-11/msg00676.html

You probably want find-file-noselect instead of find-file. Note that the docs of org-capture-templates say this:
(function function-finding-location)
Most general way: write your own function which both visits
the file and moves point to the right location
so you might want to add some code to go to the correct location (I'm guessing either (point-min) or (point-max)) in the file. That might look like this:
(defun my-visit-timestamped-file ()
(interactive)
(let* ((the-date (format-time-string "%Y%m%d-%H%M.org"))
(the-buffer (find-file-noselect (expand-file-name the-date "/some/path/"))))
(with-current-buffer the-buffer
(goto-char (point-min)))
the-buffer))

Related

Return link type property from org-follow-link-hook

I would like org-mode to automatically narrow to subtree, when I follow an internal link. So, following this thread I added this code to my init.el file:
(add-hook 'org-follow-link-hook (lambda () (org-narrow-to-subtree)))
This works great. But... it also narrows to the current subtree when I follow an external link (like a URL of a web page).
Is there some way to return the link type property (internal v. external) so that I can limit this behavior?
Something like this...
Org-follow-link hook
If link type==internal
Narrow
End if
Thanks!
You could wrap the function and check whether you're in a different
location before narrowing.
(defun my-org-open-at-point ()
(interactive)
(let ((starting-loc (point)))
(call-interactively #'org-open-at-point)
(when (not (equal starting-loc (point)))
(org-narrow-to-subtree))))
(define-key org-mode-map [remap org-open-at-point] 'my-org-open-at-point)
This works for internal links, but I thought it'd fail for links to
other files within Emacs (which could be fixed using markers). This
doesn't happen though because the current buffer stays the same.
(defun test-org-open-at-point-which-buffer ()
(interactive)
(message "before call: %s" (current-buffer))
(call-interactively #'org-open-at-point)
(message "after call: %s" (current-buffer)))
I haven't looked at the source of org-open-at-point to figure out why
that is.
By the way, to get the link type, you can use
(org-element-property :type (org-element-context))
when the point is on a link.

how to create an empty buffer in elscreen?

When I open a new tab in elscreen, it will open the scratch buffer. So if I copy something to a scratch buffer, and then open a new tab, that tab will show the same thing in the previous tab. But actually I need an empty buffer. HOw to do that?
A simple elisp function that calls elscreen-create and opens a new dummy buffer:
(defun my-elscreen-create ()
(interactive)
(elscreen-create)
(switch-to-buffer (concat "test-" (number-to-string (elscreen-get-number-of-screens))) ;; make it unique
))
If you're using the version of elscreen here or here then this might help you (There is a elscreen-create-hook but further buffer manipulation takes place after it's called, so I've been using this advice for the last year or so to do something similar):
(defadvice elscreen-create (after new-blank-buffer)
(let* ((new-buffer (generate-new-buffer "blank"))
(blank-buffer-name (buffer-name new-buffer)))
(switch-to-buffer blank-buffer-name)))
(ad-activate 'elscreen-create)

Emacs org-mode: textual reference to a file:line

I am using org-mode in Emacs to document my development activities. One of the tasks which I must continuously do by hand is to describe areas of code. Emacs has a very nice Bookmark List: create a bookmark with CTRL-x r m, list them with CTRL-x r l. This is very useful, but is not quite what I need.
Org-mode has the concept of link, and the command org-store-link will record a link to the current position in any file, which can be pasted to the org-file. The problem with this is two-fold:
It is stored as an org-link, and the linked position is not directly visible (just the description).
It is stored in the format file/search, which is not what I want.
I need to have the bookmark in textual form, so that I can copy paste it into org-mode, end edit it if needed, with a simple format like this:
absolute-file-path:line
And this must be obtained from the current point position. The workflow would be as simple as:
Go to the position which I want to record
Call a function: position-to-kill-ring (I would bind this to a keyboard shortcut)
Go to the org-mode buffer.
Yank the position.
Edit if needed (sometimes I need to change absolute paths by relative paths, since my code is in a different location in different machines)
Unfortunately my lisp is non-existent, so I do not know how to do this. Is there a simple solution to my problem?
(defun position-to-kill-ring ()
"Copy to the kill ring a string in the format \"file-name:line-number\"
for the current buffer's file name, and the line number at point."
(interactive)
(kill-new
(format "%s:%d" (buffer-file-name) (save-restriction
(widen) (line-number-at-pos)))))
You want to use the org-create-file-search-functions and org-execute-file-search-functions hooks.
For example, if you need the search you describe for text-mode files, use this:
(add-hook 'org-create-file-search-functions
'(lambda ()
(when (eq major-mode 'text-mode)
(number-to-string (line-number-at-pos)))))
(add-hook 'org-execute-file-search-functions
'(lambda (search-string)
(when (eq major-mode 'text-mode)
(goto-line (string-to-number search-string)))))
Then M-x org-store-link RET will do the right thing (store a line number as the search string) and C-c C-o (i.e. M-x org-open-at-point RET) will open the file and go to this line number.
You can of course check for other modes and/or conditions.
An elisp beginner myself I though of it as a good exercise et voila:
Edit: Rewrote it using the format methode, but I still think not storing it to the kill-ring is less intrusive in my workflow (don't know about you). Also I have added the capability to add column position.
(defvar current-file-reference "" "Global variable to store the current file reference")
(defun store-file-line-and-col ()
"Stores the current file, line and column point is at in a string in format \"file-name:line-number-column-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d:%d" (buffer-file-name) (line-number-at-pos) (current-column))))
(defun store-file-and-line ()
"Stores the current file and line oint is at in a string in format \"file-name:line-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d" (buffer-file-name) (line-number-at-pos))))
(defun insert-file-reference ()
"Inserts the value stored for current-file-reference at point."
(interactive)
(if (string= "" current-file-reference)
(message "No current file/line/column set!")
(insert current-file-reference)))
Not tested extensively but working for me. Just hit store-file-and-line or store-file-line-and-col to store current location and insert-file-reference to insert the stored value at point.
BTW, if you want something better than FILE:LINE, you can try to use add-log-current-defun (in add-log.el) which should return the name of the current function.
;; Insert a org link to the function in the next window
(defun insert-org-link-to-func ()
(interactive)
(insert (with-current-buffer (window-buffer (next-window))
(org-make-link-string
(concat "file:" (buffer-file-name)
"::" (number-to-string (line-number-at-pos)))
(which-function)
))))
This func generates link with the function name as the description.
Open two windows, one is the org file and the other is src code.
Then M-x insert-org-link-to-func RET

In Emacs, how can I open a buffer/file in "ibuffer/dired mode" in another frame?

When I am working with multiple frames, I want to decide in which frame Emacs should open files/buffers.
You can't do that out of the box.
There are find-buffer-other-frame and the like but they open a new frame.
What you can do is write your own function like this:
(defun find-file-in-frame ()
(interactive)
(call-interactively 'select-frame-by-name)
(call-interactively 'find-file))
This switches frame and then asks for the file, if you want to do it otherwise you have to do more work.
Edit: Here the version that asks in the current frame and opens the file in the other window:
(defun find-file-in-frame (noselect)
(interactive "P")
(let ((current-frame (selected-frame))
(frame (completing-read "Frame: " (make-frame-names-alist)))
(buffer (save-window-excursion
(call-interactively 'find-file))))
(select-frame-set-input-focus (assoc-default frame
(make-frame-names-alist)
nil current-frame))
(switch-to-buffer buffer)
(when noselect
(select-frame-set-input-focus current-frame))))
If you are just referring to find-file, then I would suggest that with the combination of windmove and framemove, switching to the frame in which you wish to open the file is so trivial and fast that you probably don't need anything fancier.
OTOH if you want to be able to select a frame whenever a file is to be opened by any means, this obviously doesn't apply.
Not sure what you're really asking. But as to opening a file from Dired in another frame, just use C-o or M-mouse-2 in Dired+. Those are bound to these commands, in case you do not want to load Dired+ for some reason:
(defun diredp-find-file-other-frame () ; Bound to `C-o'
"In Dired, visit this file or directory in another frame."
(interactive)
(find-file-other-frame (file-name-sans-versions (dired-get-filename nil t) t)))
(defun diredp-mouse-find-file-other-frame (event) ; Bound to `M-mouse-2'
"In Dired, visit file or directory clicked on in another frame."
(interactive "e")
(let ((pop-up-frames t)) (dired-mouse-find-file-other-window event)))

File path to clipboard in Emacs

What is the most simple way to send current full file name with file path to clipboard?
What I am using now is messages buffer: I copy file name that appears there after saving a file. But, I suppose, there should be much more simple way.
Why no one tell the simple solution.
Just go to your dired buffer then press 0 w or C-u 0 w.
This will call dired-copy-filename-as-kill which gives you full path of a file. If you want current dir, just delete the file at the end of it or you can use the function below, then bind it to any key you like.
(defun my/dired-copy-dirname-as-kill ()
"Copy the current directory into the kill ring."
(interactive)
(kill-new default-directory))
PS: personally I go to current directory from file buffer using dired-jump
I use this:
(defun my-put-file-name-on-clipboard ()
"Put the current file name on the clipboard"
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(when filename
(with-temp-buffer
(insert filename)
(clipboard-kill-region (point-min) (point-max)))
(message filename))))
In Emacs Prelude I use:
(defun prelude-copy-file-name-to-clipboard ()
"Copy the current buffer file name to the clipboard."
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(when filename
(kill-new filename)
(message "Copied buffer file name '%s' to the clipboard." filename))))
If you want to write the name/path of the current buffer you can type C-u M-: and then either (buffer-file-name) - for the full path - or (buffer-name) for the buffer name.
That is:
M-: + ellisp expression evaluates an ellisp expression in the mini-buffer
C-u write the output to the current buffer
Does not exactly answer to the question but could be useful if someone use this or other function sporadically, and prefers to not initialize the function at every startup.
In the Spacemacs distribution, you can press Spacefyy to display the buffer name in the minibuffer and copy it to the kill ring.
The function spacemacs/show-and-copy-buffer-filename seems to originate from this blog post: Emacs: Show Buffer File Name.
(defun camdez/show-buffer-file-name ()
"Show the full path to the current file in the minibuffer."
(interactive)
(let ((file-name (buffer-file-name)))
(if file-name
(progn
(message file-name)
(kill-new file-name))
(error "Buffer not visiting a file"))))
There's a buffer-extension - and it has copy-buffer-file-name-as-kill function. It even asks You what to copy: name, full name or a directory name.
Edit:
I use modified version of copy-buffer-file-name-as-kill from buffer-extension.el:
(defun copy-buffer-file-name-as-kill (choice)
"Copyies the buffer {name/mode}, file {name/full path/directory} to the kill-ring."
(interactive "cCopy (b) buffer name, (m) buffer major mode, (f) full buffer-file path, (d) buffer-file directory, (n) buffer-file basename")
(let ((new-kill-string)
(name (if (eq major-mode 'dired-mode)
(dired-get-filename)
(or (buffer-file-name) ""))))
(cond ((eq choice ?f)
(setq new-kill-string name))
((eq choice ?d)
(setq new-kill-string (file-name-directory name)))
((eq choice ?n)
(setq new-kill-string (file-name-nondirectory name)))
((eq choice ?b)
(setq new-kill-string (buffer-name)))
((eq choice ?m)
(setq new-kill-string (format "%s" major-mode)))
(t (message "Quit")))
(when new-kill-string
(message "%s copied" new-kill-string)
(kill-new new-kill-string))))
If you use Doom Emacs, it can be done with SPC f y.
To paste the current file path in the buffer, the most simple way I see is to do: C-u M-! pwd (this might not work on Windows systems though).
Alternatively, you can use C-x C-b to show the file paths of all opened buffers.
This is what has worked for me on MacOS 10.15.7, GNU Emacs 27.1
(defun copy-current-buffer-file-name ()
(interactive)
(shell-command (concat "echo " (buffer-file-name) " | pbcopy")))
set keybinding to "C-x M-f":
(global-set-key (kbd "C-x M-f") 'copy-current-buffer-file-name)
FYI: For a real beginner reading this, you need to add those lines to your init.el file.
Lots of good answers here, though I think for the "most simple way", as described in the question, there's room for improvement. Here's what I came up with (with thanks to other answers for the bits and pieces):
M-: (kill-new (buffer-file-name)) RET
This does precisely what you asked for -- takes the filename of the current buffer, and puts it in the "kill ring" and, depending on your settings, also the system clipboard. (See emacswiki/CopyAndPaste for more details on that part.)
If you want to do this regularly, then setting up a function like listed in the other answers, and binding it to an available key sequence, would make it easier to do frequently. But the above works with no prior setup, which I'm interpreting to be more "simple".