Emacs list of parent modes - emacs

In emacs is it possible to get a list of parent modes for a derived mode?
I found derived-mode-p, but this only allows me to test if a mode is derived from another.

Every symbol has a property list and for derived modes, this list includes the component derived-mode-parent which indicates which mode it was derived from. It seems to be just a single symbol.
(define-derived-mode nst-mode text-mode "Testing")
=> nst-mode
(get 'nst-mode 'derived-mode-parent)
=> text-mode
I don't see how there could be more than one parent, ever.
Here is a demo of a transitive chain of parents.
(define-derived-mode rope nil "Victim of mouse")
(define-derived-mode mouse rope "Victim of cat")
(define-derived-mode cat mouse "Victim of dog")
(define-derived-mode dog cat "Enough already")
(let ((mode 'dog) parents)
(while mode
(setq parents (cons mode parents)
mode (get mode 'derived-mode-parent)))
(reverse parents))
=> (dog cat mouse rope)
... or, just for fun, a recursive version:
(defun derived-mode-parents (mode)
(and mode
(cons mode (derived-mode-parents
(get mode 'derived-mode-parent)))))
(derived-mode-parents 'dog)
=> (dog cat mouse rope)

Related

ess-eval-line-visibly-and-step splits window and shows in both

I've noticed that when I execute ess-eval-line-visibly-and-step inside an R-mode buffer and I don't already have an active R session, it will split the screen (good), however it will show both sides of the split into the R session (bad). I would expect it to split the screen and show the R session on one side (ideally the right) and leave the R-mode buffer on the other side (ideally the left).
The only bit of my init.el referencing ESS is
(use-package ess
:ensure t
:defer t
:config
(setq ess-ask-for-ess-directory nil ;; just run R wherever
;; the file lives
ess-history-file nil ;; don't save history
ess-eval-visibly-p nil ;; when running R, don't
;; show code, just output
;; (greatly speeds
;; running)
inferior-R-args
"--no-restore --no-save --quiet" ;; R startup conditions
ess-style 'RStudio ;; better indenting
comint-scroll-to-bottom-on-input t ;; force ESS to scroll R
;; to the bottom after
;; running code
comintq-scroll-to-bottom-on-output t
comint-scroll-show-maximum-output t
comint-move-point-for-output t))
I've commented out all the setq and it doesn't fix the issue.

How to make `C-x b RET` switch to previous buffer even if it's already shown in another frame?

Edit: What the poster calls a "window", Emacs calls a "frame". I fixed the title.
Concisely, the question is: in a window, how do I switch quickly to a buffer previously visited in that window, even if it's already opened in another window?
A more detailed description follows.
Normally, in order to switch window to previous buffer one just types C-x b RET. That is, the default argument to switch-to-buffer (or ido-switch-buffer) is the previous buffer.
This is not, however, the case when that (previous) buffer is already shown in another window. That's exactly what bugs me.
Let's consider an example. Suppose I have three buffers (A, B and C) and two windows showing buffers A and B (C is not visible at this point).
Then I open buffer A in the second window, too. So, now I have buffer A shown in both windows. Then I switch (C-x b RET) to B again. After that, C-x b RET will bring me not to A, but to C because A is already shown in the other window.
How do I make C-x b RET behave more consistently?
Update
After this problem had been solved, I realized I needed more: namely, for point position to be remembered per-window, not per buffer. Luckily, there're ready-made solutions:
winpoint
per-window-point
They're quite similar; for a discussion of differences see here.
I've found a fix for switch-to-buffer. It eventually calls
(other-buffer (current-buffer))
while in order to fix your problem, the call needs to look like this:
(other-buffer (current-buffer) t)
i.e. the visible-ok argument needs to be t.
Here's an advice to have it always at t. Hopefully it won't break other stuff that uses other-buffer:
(defadvice other-buffer (around fix-switch-to-buffer
(&optional buffer visible-ok frame) activate)
(setq visible-ok t)
ad-do-it)
Note that ido-switch-to-buffer uses a different machinery, so a different method is needed to fix it.
update: fix for ido-switch-to-buffer
I needed to re-define ido-make-buffer-list:
(defun ido-make-buffer-list (default)
(let* ((ido-current-buffers (list (buffer-name (current-buffer))))
(ido-temp-list (ido-make-buffer-list-1 (selected-frame) ido-current-buffers)))
(if ido-temp-list
(nconc ido-temp-list ido-current-buffers)
(setq ido-temp-list ido-current-buffers))
(if default
(setq ido-temp-list
(cons default (delete default ido-temp-list))))
(if (bound-and-true-p ido-enable-virtual-buffers)
(ido-add-virtual-buffers-to-list))
(run-hooks 'ido-make-buffer-list-hook)
ido-temp-list))
The diff is just one line, but it's too messy to advice it.
update: use new advice system for other-buffer
The old stuff should still work for quite a while, but here's the new approach:
(defun other-buffer-advice (orig-fun &optional buffer visible-ok frame)
(funcall orig-fun buffer t frame))
(advice-add 'other-buffer :around #'other-buffer-advice)
;; (advice-remove 'other-buffer :around #'other-buffer-advice)
Instead of advising the built-in function other-buffer, you can pre-select visible buffers using a package.
1 Using Ivy
If you're using Ivy, you can use abo-abo's approach to override the lower-use function ivy-switch-buffer.
(defun user/ivy-switch-buffer ()
"Switch to another buffer with visible-ok preselection."
(interactive)
(ivy-read "Switch to buffer: " #'internal-complete-buffer
:keymap ivy-switch-buffer-map
:preselect (buffer-name (other-buffer (current-buffer) t))
:action #'ivy--switch-buffer-action
:matcher #'ivy--switch-buffer-matcher
:caller 'ivy-switch-buffer))
(advice-add 'ivy-switch-buffer :override #'user/ivy-switch-buffer)
2 Using Ido mode
2.1 Switching to a buffer shown in another frame
If by "window" you really mean "frame" (i.e., you'd like to ido-switch-buffer to a buffer that is currently shown in another frame), then ido-mode gives you the behavior you're looking for when you change ido-default-buffer-method from its default value of raise-frame to selected-window:
(setq ido-default-buffer-method 'selected-window)
Emacs constructs an independent buffer list for each frame, so the only thing you have to do is to configure Ido to avoid jumping to another frame when you switch buffers.
2.2 Switching to a buffer that is shown in another window inside the same frame
To get this behavior across windows within the same frame, you should hook a function that reorders the buffer list onto ido-make-buffer-list-hook.
From ido.el:
;; Changing the list of files
;; --------------------------
;; By default, the list of current files is most recent first,
;; oldest last, with the exception that the files visible in the
;; current frame are put at the end of the list. A hook exists to
;; allow other functions to order the list. For example, if you add:
;;
;; (add-hook 'ido-make-buffer-list-hook 'ido-summary-buffers-to-end)
;;
;; then all files matching "Summary" are moved to the end of the
;; list. (I find this handy for keeping the INBOX Summary and so on
;; out of the way.) It also moves files matching "output\*$" to the
;; end of the list (these are created by AUCTeX when compiling.)
;; Other functions could be made available which alter the list of
;; matching files (either deleting or rearranging elements.)

Looking forward a way to make cursor blinks like a heartbeat in Emacs

How can I make the cursor in Emacs blink like a heartbeat. Like the LED on the front panel of laptop when the computer is suspended.
There is variable blink-cursor-alist that control the blink of cursor, but I do not know how to use it to satisfy my requirement.
Is it possible?
This simple minor mode implements a heartbeat-style blinking cursor. You can tweak heartbeat-cursor-colors to get different hue or variations thereof.
The code is tested in Emacs 24.2.1, but would be easy to port to older Emacsen.
(require 'cl)
(require 'color)
(defvar heartbeat-fps 16)
(defvar heartbeat-period 5)
(defun heartbeat-range (from to cnt)
(let ((step (/ (- to from) (float cnt))))
(loop for i below cnt collect (+ from (* step i)))))
(defun heartbeat-cursor-colors ()
(let ((cnt (* heartbeat-period heartbeat-fps)))
(mapcar (lambda (r)
(color-rgb-to-hex r 0 0))
(nconc (heartbeat-range .2 1 (/ cnt 2))
(heartbeat-range 1 .2 (/ cnt 2))))))
(defvar heartbeat-cursor-timer nil)
(defvar heartbeat-cursor-old-color)
(define-minor-mode heartbeat-cursor-mode
"Change cursor color with the heartbeat effect."
nil "" nil
:global t
(when heartbeat-cursor-timer
(cancel-timer heartbeat-cursor-timer)
(setq heartbeat-cursor-timer nil)
(set-face-background 'cursor heartbeat-cursor-old-color))
(when heartbeat-cursor-mode
(setq heartbeat-cursor-old-color (face-background 'cursor)
heartbeat-cursor-timer
(run-with-timer
0 (/ 1 (float heartbeat-fps))
(lexical-let ((colors (heartbeat-cursor-colors)) tail)
(lambda ()
(setq tail (or (cdr tail) colors))
(set-face-background 'cursor (car tail))))))))
You guess that the option has something to do with the word "blink".
So you press C-h a (for apropos) and type "blink".
On my emacs, I get two options: blink-cursor-mode and blink-matching-open.
The first one looks right. The description says: "Toggle blinking cursor mode".
The shortcut on my emacs says: <menu-bar> <options> <blink-cursor-mode>.
So I guess that the option is somewhere in the menu, maybe under "options".
I open the Options menu, and there it is: "Blinking Cursor" with a tick box.
This also sounds like an option that can be customised.
So I type M-x customize-option and then blink-cursor-mode. This allows me to toggle the value and also save it for future sessions.
EDIT: To set the interval between ON and OFF for the cursor, there is a variable called blink-cursor-interval. You can use M-x customize-variable and then blink-cursor-interval to set the interval. The variable blink-cursor-alist matches an OFF state cursor type to the ON state cursor type, and is not related to the speed of blinking.
EDIT2: As far as I know there is no way to make the cursor gradually turn off and on, because the shape of the cursor for the ON state could be different to the shape in the OFF state (so a gradual change in shape will be required).

Emacs caps lock minor mode?

Is there a command in Emacs to turn on what might be described as "caps lock minor mode"? I'm looking to do something like M-x toggle-caps-mode, then every letter I type in the buffer is a capital letter until I do M-x toggle-caps-mode again.
Note: I'm NOT looking for directions on how to swap caps and control. In reality this is because I have already done that. I am generally quite happy with it, but occasionally I'm editing code where there are a bunch of constants that are in all caps, and it gets to be a strain holding down the shift key. I'm aware of the various upcase conversion functions; I'd rather not have to type the word, select it, then run upcase-region.
If it matters, I'm using Aquamacs 2.2 w/ Emacs 23.3.1.
You don't need to type the word then select it. If you want to upcase the last word, press M-b M-u or ESC b u. Ok, you'll need to press b several times if it's a word_with_underscores.
If you really want a caps lock minor mode, try John Paul Wallington's lockcaps.el.
You can try something like this:
(define-minor-mode caps-lock-mode
"caps-lock mode"
;; The initial value.
nil
;; The indicator for the mode line.
" CAPS-LOCK"
;; The minor mode bindings.
'(("a" . (lambda () (interactive) (insert-char ?A 1)))
("b" . (lambda () (interactive) (insert-char ?B 1)))
;;etc
("A" . (lambda () (interactive) (insert-char ?a 1)))
("B" . (lambda () (interactive) (insert-char ?b 1)))
;;etc
))

How to move a subtree to another subtree in org-mode emacs

I'd like to perform one simple task but there seems to be no clear solution for it:
I have a structure of subtrees like this:
* Tree A
** Subtree A1
** Subtree A2
* Tree B
** Subtree B1
** Subtree B2
I'd like to set up one key shortcut to move subtrees from Tree A to Tree B. Moving to the archive seems to be easy, but do you have any clue how to do it within one file?
Any help appreciated! Thanks!
You could try to refill the subtree with C-c C-w (it work also to move the subtree to another file).
Another way is to fold the subtree, then to kill it with C-k C-k and to paste it where it should be.
The third solution I use some time is to change the level of the subtree with M-left, then to move it with M-up and M-down, then M-right put it again on the correct level. This third way have the shortcoming that some time it make other part of the tree to move.
I also wanted a way for org-refile to refile easily to a subtree, so I wrote some code and generalized it so that it will set an arbitrary immediate target anywhere (not just in the same file).
Basic usage is to move somewhere in Tree B and type C-c C-x C-m to mark the target for refiling, then move to the entry in Tree A that you want to refile and type C-c C-w which will immediately refile into the target location you set in Tree B without prompting you, unless you called org-refile-immediate-target with a prefix arg C-u C-c C-x C-m.
Note that if you press C-c C-w in rapid succession to refile multiple entries it will preserve the order of your entries even if org-reverse-note-order is set to t, but you can turn it off to respect the setting of org-reverse-note-order with a double prefix arg C-u C-u C-c C-x C-m.
(defvar org-refile-immediate nil
"Refile immediately using `org-refile-immediate-target' instead of prompting.")
(make-local-variable 'org-refile-immediate)
(defvar org-refile-immediate-preserve-order t
"If last command was also `org-refile' then preserve ordering.")
(make-local-variable 'org-refile-immediate-preserve-order)
(defvar org-refile-immediate-target nil)
"Value uses the same format as an item in `org-refile-targets'."
(make-local-variable 'org-refile-immediate-target)
(defadvice org-refile (around org-immediate activate)
(if (not org-refile-immediate)
ad-do-it
;; if last command was `org-refile' then preserve ordering
(let ((org-reverse-note-order
(if (and org-refile-immediate-preserve-order
(eq last-command 'org-refile)) nil org-reverse-note-order)))
(ad-set-arg 2 (assoc org-refile-immediate-target (org-refile-get-targets)))
(prog1 ad-do-it
(setq this-command 'org-refile)))))
(defadvice org-refile-cache-clear (after org-refile-history-clear activate)
(setq org-refile-targets (default-value 'org-refile-targets))
(setq org-refile-immediate nil)
(setq org-refile-immediate-target nil)
(setq org-refile-history nil))
;;;###autoload
(defun org-refile-immediate-target (&optional arg)
"Set current entry as `org-refile' target.
Non-nil turns off `org-refile-immediate', otherwise `org-refile'
will immediately refile without prompting for target using most
recent entry in `org-refile-targets' that matches
`org-refile-immediate-target' as the default."
(interactive "P")
(if (equal arg '(16))
(progn
(setq org-refile-immediate-preserve-order
(not org-refile-immediate-preserve-order))
(message "Order preserving is turned: %s"
(if org-refile-immediate-preserve-order
"on" "off")))
(setq org-refile-immediate (unless arg t))
(make-local-variable 'org-refile-targets)
(let* ((components (org-heading-components))
(level (first components))
(heading (nth 4 components))
(string (substring-no-properties heading)))
(add-to-list 'org-refile-targets
(append (list (buffer-file-name))
(cons :regexp
(format "^%s %s$"
(make-string level ?*)
string))))
(setq org-refile-immediate-target heading))))
(define-key org-mode-map "\C-c\C-x\C-m" 'org-refile-immediate-target)
It was hard to find a key that was free on the C-c C-x prefix, so I used m with the mnemonic i*mm*ediate
I use M-left, M-up/down and then M-right if the structure of the file is not too complex. But keep in mind that if you try to move Subtree A1 to Tree B using this method, you will lose its child Subsubtree A1.1, unless you move it with M-S-left/right:
* Tree A
** Subtree A1 <=== move this heading with M-S-left
*** Subsubtree A1.1 <=== or you will leave this heading behind
** Subtree A2
* Tree B
** Subtree B1
** Subtree B2
You can refile the subtree using C-c C-w, depending on how you have refile set up. Depending on the depth of your destination you may not be able to use it as a valid destination.
You can kill or copy a subtree without having to fold the structure using the kill/copy subtree commands: C-c C-x C-w and C-c C-x M-w respectively, yanking a subtree is either C-c C-x C-y or simply C-y
Moving a subtree within the same buffer can be done with org-refile. While moving, you can even change hierarchy of the original (number of stars). However, it might not work out of the box primarily because the buffer you are working with is not in the org-refile-targets. You could add the file in the refile targets by using C-[, then run org-refile with C-c C-w and finally remove the file from the refile targets with C-]. This might be too much (and might fail if your maxlevel setting is too low - which you can change anyway). Alternatively, to automate this procedure in one swoop, you will first have to define a function like this:
(defun refile-in-current ()
"refile current item in current buffer"
(interactive)
(let ((org-refile-use-outline-path t)
(org-refile-targets '((nil . (:maxlevel . 5)))))
(org-refile)))
The nil in there means "current buffer". The let redefines your variables locally only. You will then have to add a shortcut to run the function. Here is one way to bind it to C-c m:
(add-hook 'org-mode-hook (lambda ()
(local-set-key "\C-c m" 'refile-in-current)
(require 'org-archive)
(setq org-archive-save-context-info nil)
(setq org-archive-location "::* Archived Tasks")
run (org-archive-subtree) at subtree which you want to move, it will be moved into "* Archived Tasks" heading