emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-table-mark-field
@ 2016-05-13 23:15 Kaushal Modi
  2016-05-13 23:38 ` org-table-mark-field Kaushal Modi
  0 siblings, 1 reply; 3+ messages in thread
From: Kaushal Modi @ 2016-05-13 23:15 UTC (permalink / raw)
  To: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1892 bytes --]

Hi all,

At times I need to copy content of the current field or few fields around
the field in the current row. I realized that we do not have a function
that selects fields. We have function that blanks a field but not one that
selects. Please point me to the right function if I missed it.

But as I couldn't find anything like that, I came up with the below. Please
advise if there's a better way to do the same or if I this could be added
to org.

=====
(defun org-table-mark-field (n)
  "Mark the current table field.

If N is negative, select (- N) fields to the left of the current field,
including the current field.
If N >= 2, select (1- N) fields to the right of the current field,
including the current field.
If N is 0 or 1 (default), only the current field is selected."
  (interactive "p")
  (let ((bol-point (save-excursion
                     (beginning-of-line)
                     (point)))
        (bof-arg 1)
        (eof-arg 1)
        (p (point))
        bof-p)

    ;; Check if the point is already at the beginning of the current field.
    (when (looking-back "|\\s-*" bol-point)
      (setq bof-p t))

    ;; When selecting current field plus fields to the right
    (when (>= n 2)
      (setq eof-arg n))
    ;; When selecting current field plus fields to the left
    (when (<= n -1)
      (setq bof-arg (- n)))

    (org-table-beginning-of-field bof-arg)
    (when bof-p
      (org-table-next-field))
    (set-mark-command nil)
    (goto-char p)
    (org-table-end-of-field eof-arg)
    (exchange-point-and-mark)))
====

Using bind-key (use-package), I bind the above to S-SPC only as long as the
point is in a table. More on context-aware key bindings[1].

=====
(bind-keys
 :map org-mode-map
 :filter (org-at-table-p)
  ("S-SPC" . org-table-mark-field))
=====

[1]: http://endlessparentheses.com/define-context-aware-keys-in-emacs.html
-- 

-- 
Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 2761 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: org-table-mark-field
  2016-05-13 23:15 org-table-mark-field Kaushal Modi
@ 2016-05-13 23:38 ` Kaushal Modi
  2016-05-14  7:10   ` org-table-mark-field Kaushal Modi
  0 siblings, 1 reply; 3+ messages in thread
From: Kaushal Modi @ 2016-05-13 23:38 UTC (permalink / raw)
  To: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1412 bytes --]

Here's a minor update with a bug fix (to handle the case when the point is
at the end of a table field initially):

(defun org-table-mark-field (n)
  "Mark the current table field.

If N is negative, select (- N) fields to the left of the current field,
including the current field.
If N >= 2, select (1- N) fields to the right of the current field,
including the current field.
If N is 0 or 1 (default), only the current field is selected."
  (interactive "p")
  (let ((bol-point (save-excursion
                     (beginning-of-line)
                     (point)))
        (bof-arg 1)
        (eof-arg 1)
        (p (point))
        bof-p eof-p)

    ;; Check if the point is already at the beginning of the current field.
    (when (looking-back "|\\s-?" bol-point)
      (setq bof-p t))
    ;; Check if the point is already at the end of the current field.
    (when (looking-at "\\s-?|")
      (setq eof-p t))

    ;; When selecting current field plus fields to the right
    (when (>= n 2)
      (setq eof-arg n))
    ;; When selecting current field plus fields to the left
    (when (<= n -1)
      (setq bof-arg (- n)))

    (org-table-beginning-of-field bof-arg)
    (when bof-p
      (org-table-next-field))
    (set-mark-command nil)
    (goto-char p)
    (when eof-p
      (org-table-beginning-of-field 1))
    (org-table-end-of-field eof-arg)
    (exchange-point-and-mark)))
-- 

-- 
Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 2061 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: org-table-mark-field
  2016-05-13 23:38 ` org-table-mark-field Kaushal Modi
@ 2016-05-14  7:10   ` Kaushal Modi
  0 siblings, 0 replies; 3+ messages in thread
From: Kaushal Modi @ 2016-05-14  7:10 UTC (permalink / raw)
  To: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1219 bytes --]

The solution was unnecessarily over-complicated.

Below one seems simpler and more intuitive. It requires the hydra package.

(defun org-table-mark-field ()
  "Mark the current table field."
  (interactive)
  ;; Do not try to jump to the beginning of field if the point is already
there
  (when (not (looking-back "|\\s-?"))
    (org-table-beginning-of-field 1))
  (set-mark-command nil)
  (org-table-end-of-field 1))

(defhydra hydra-org-table-mark-field
  (:body-pre (org-table-mark-field)
   :color pink
   :hint nil)
  "
   ^^      _p_     ^^
 _b_  selection  _f_           | org table mark ▯field▮ |
   ^^      _n_     ^^
"
  ("x" exchange-point-and-mark "exchange point/mark")
  ("f" (lambda (arg)
         (interactive "p")
         (when (eq 1 arg)
           (setq arg 2))
         (org-table-end-of-field arg)))
  ("b" (lambda (arg)
         (interactive "p")
         (when (eq 1 arg)
           (setq arg 2))
         (org-table-beginning-of-field arg)))
  ("n" next-line)
  ("p" previous-line)
  ("q" nil "cancel" :color blue))

(bind-keys
 :map org-mode-map
 :filter (org-at-table-p)
  ("S-SPC" . hydra-org-table-mark-field/body))

-- 

-- 
Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 1988 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-05-14  7:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-13 23:15 org-table-mark-field Kaushal Modi
2016-05-13 23:38 ` org-table-mark-field Kaushal Modi
2016-05-14  7:10   ` org-table-mark-field Kaushal Modi

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).