【问题标题】:How to mark (within a function), do stuff (that sets other marks), and then return to the original mark如何标记(在函数内),做事(设置其他标记),然后返回原始标记
【发布时间】:2014-11-23 11:46:46
【问题描述】:

谁能给我一个函数的例子,它会设置一个标记,然后做一些事情,在缓冲区的其他地方设置额外的标记,然后返回到函数开头标记的原始位置。

transient-mark-mode 默认启用。我尝试使用(activate-mark) 设置标记,然后使用(deactivate-mark) 将标记推入mark-ring,然后我的函数在缓冲区中移动,归档待办事项并执行一些组织工作并暂停read-event(在新的待办事项存档的位置)让我看到一切都正确完成,然后我使用(set-mark-command t)回到一切开始的地方。但是,(set-mark-command t) 并没有让我回到函数开头的原始标记。相反,(set-mark-command t) 将我带到了另一个标记,该标记是在函数运行时无意中设置的。

(defun none (&optional default-heading)
(interactive)
(beginning-of-visual-line)
(activate-mark)
(deactivate-mark)
    (let ((lawlist-item default-heading)
            result)
        (unless lawlist-item
          (condition-case nil
              (progn 
                (org-back-to-heading t)
                (setq lawlist-item (elt (org-heading-components) 4)))
            )
         )
    (when (search-forward-regexp ":event\\|event:" (line-end-position) t)
      (replace-match "")
        (when (and (looking-at ":$\\|: ") (looking-back " "))
          (delete-char 1)))
    (org-todo "None")
    (org-priority ?E)
    (org-schedule 'remove)
    (org-deadline 'remove)
    (org-set-property "ToodledoFolder" "DONE")
    (setq org-archive-save-context-info nil)
    (setq org-archive-location "/Users/HOME/.0.data/*TODO*::* DONE")
    (org-archive-subtree)
    (goto-char (point-min))
    (re-search-forward "^\* DONE" nil t)
       (condition-case err
           (progn
             (org-sort-entries t ?a)
             (lawlist-org-cleanup) )
         (error nil))
    (re-search-forward lawlist-item nil t)
    (message (format "%s -- Finished!" lawlist-item))
    (beginning-of-visual-line)
    (org-cycle-hide-drawers 'all)
    (read-event)
    (set-mark-command t)
  ))

【问题讨论】:

标签: emacs elisp


【解决方案1】:

这也可能有帮助:您可能确实想在 Lisp 代码中设置标记。

这就是 Elisp 手册中关于 set-mark 的内容:

 -- Function: set-mark position

 This function sets the mark to POSITION, and activates the mark.
 The old value of the mark is _not_ pushed onto the mark ring.

 *Please note:* Use this function only if you want the user to see
 that the mark has moved, and you want the previous mark position to
 be lost.  Normally, when a new mark is set, the old one should go
 on the `mark-ring'.  For this reason, most applications should use
 `push-mark' and `pop-mark', not `set-mark'.

 Novice Emacs Lisp programmers often try to use the mark for the
 wrong purposes.  The mark saves a location for the user's
 convenience.  An editing command should not alter the mark unless
 altering the mark is part of the user-level functionality of the
 command.  (And, in that case, this effect should be documented.)
 To remember a location for internal use in the Lisp program, store
 it in a Lisp variable.  For example:

      (let ((beg (point)))
        (forward-line 1)
        (delete-region beg (point))).

这就是 set-mark-commandpush-mark 的文档字符串所说的:

Novice Emacs Lisp programmers often try to use the mark for the wrong
purposes.  See the documentation of `set-mark' for more information.

【讨论】:

  • 好吧,那就是我(新手)。 . .今晚我会试一试——我假设beg 是我的起点。本质上,我想设置beg,然后做一堆不相关的事情,然后在我完成后返回beg
  • 在这种情况下,只需使用save-excursion:您不需要跟踪起点并返回它。见C-h f save-excursion
  • 嗯,学习了一个概念(point) 和一个需要学习的概念(save-excursion . . .)——你的beg 样本工作得很好——我刚刚把我现有的let 变成let* 和添加(beg (point)),然后在函数(goto-char beg) 的末尾。非常感谢 - 非常感谢!!!!。
  • 出于某种原因,(save-excursion . . .) 返回到上一段的末尾,而不是它开始的确切位置。 (point) 是对的。这可能与从(save-excursion . . .) 开始的位置移动的段落有关。
  • 你的最后一句话让我觉得是这样的。
【解决方案2】:

听起来save-excursion 就是您要查找的内容——它将您的位置(连同其他信息)保存在缓冲区中,执行主体,然后返回到原始位置。

【讨论】:

  • 谢谢 - 我今晚会看看这个,看看它是否有助于我设置一个位置(例如,第 3 行,第 5 列),记住它,然后稍后再回到那个位置。
  • 第二个概念几乎学会了——即(save-excursion . . . ) 我对它与使用(point) 之间的明显区别感到有些困惑。当使用(point) 时,它是正确的——即第 17 行第 1 列。出于某种原因,(save-excursion . . . ) 将我返回到上一段的末尾(第 15 行的末尾)而不是确切的位置(save-excursion . . .) 开始(第 17 行,第 1 列)。或许(save-excursion . . .)默认是上一段的结尾? (save-excursion . . .) 开始的段落被移到更下方的位置。
  • 另一种可能是组织模式下的hs-hide-all(org-cycle-hide-drawers 'all) 影响(save-excrusion . . .),而它们不影响(point)。 . . .不,我只是尝试过,结果没有改变——它仍然返回到上一段的末尾。嗯...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多