0

We can create a timestamp (e.g., <2018-01-11 Thu>) by C-c . which runs the command org-time-stamp. How can we highlight (i.e., create a region) the whole timestamp when the cursor is on it?

  • It already does that out of the box, right? The mouse-face property is what causes this to work. – lawlist Jan 12 '18 at 7:14
  • I need to do shift + movement in order to highlight the timestamp. – AhLeung Jan 12 '18 at 7:18
  • Try with emacs -q -- i.e., no user configuration whatsoever. You will see that the mouse-over highlights the time-stamp out of the box. No need to press the shift key. – lawlist Jan 12 '18 at 7:19
  • Oh, I was not clear. Yes, I also see the mouse-over highlights. But I would like to create a highlighted region, so that I can copy or cut the region (i.e., the timestamp). – AhLeung Jan 12 '18 at 7:31
1

The original poster has indicated (in a comment) a desire "to create a highlighted region, so that [he/she] can copy or cut the region (i.e., the timestamp)". The following function returns the timestamp at point without permanently moving point to that location. The user can modify this function to do things like (kill-new ts) to put the timestamp into the kill-ring, or the function can be modified to delete-region using (match-beginning 0) and (match-end 0), or the function can be modified to use kill-region, or .... There is no reason that I can see to actually select the region (since it already gets highlighted on mouse-over), but the user can modify this function to do that too.

Be aware that org-at-timestamp-p has only one argument in org-mode version 8.2.10 that ships with Emacs 25.3. It has additional arguments in org-mode version 9, such as 'lax.

(require 'org)

(org-defkey org-mouse-map [C-down-mouse-3] 'ignore)

(org-defkey org-mouse-map [C-mouse-3] 'org-timestamp)

(defun org-timestamp (event)
"Return the timestamp underneath the mouse."
(interactive (list last-nonmenu-event))
  (save-excursion
    (let* ((tsr org-ts-regexp3)
           (pos (posn-point (event-start event)))
           (dummy-one (goto-char pos))
           (dummy-two (or (looking-at tsr)
                          (progn
                            (skip-chars-backward "^[<\n\r\t")
                            (if (> (point) (point-min)) (backward-char 1))
                            (and (looking-at tsr)
                                 (> (- (match-end 0) pos) -1)))))
           (ts (match-string 0)))
      (if (org-at-timestamp-p 'inactive-ok)
        (progn
          (message "TIMESTAMP: %s" ts)
          ts)
        (message "You are not at a valid timestamp!")
        nil))))
  • I used a combination of yours and xuchunyang answer in the end. Thanks. – AhLeung Mar 16 '18 at 15:58
1

Org mode adds the org-date face to the timestamps, so you should be able to use the following to select the timestamp at point.

(defun your-mark-org-timestamp ()
  (interactive)
  (when (eq (get-text-property (point) 'face) 'org-date)
    (let ((beg (previous-property-change (point) nil (line-beginning-position)))
          (end (next-property-change (point) nil (line-end-position))))
      (push-mark beg t t)
      (goto-char end))))

There are other ways. For example, if you have a regexp for org timestamp, you can search for it with thing-at-point-looking-at. And you can also take a look at the code of org mode, maybe you can reuse some code of it.

  • I found that previous-property-change doesn't work in some edge cases, so I used char search to find the beg and end. i.e., I used a combination of yours and lawlist's answer in the end. Thanks. – AhLeung Mar 16 '18 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.