automatic header for org-mode in emacs - emacs

I am using define-auto-insert '("\.org\'" . "org skeleton") macro to insert the header. This is working perfectly fine whenever I'm creating any new org file. But it is also inserting the auto header to the other empty orgs file (created using touch command for some particular reason). Can someone please suggest me any way such that the macro does not add the header in already created empty files when I open them in emacs?

Autoinsert isn't really designed to make this distinction. The simplest way of achieving this is to wrap the auto-insert function with another function and register that with the new file hook instead. For example:
(defun auto-insert-guard ()
"Prevent auto-insertion for files that exist already"
(interactive)
(unless (file-exists-p (buffer-file-name))
(auto-insert)))
Use this for the hook instead:
(add-hook 'find-file-hook 'auto-insert-guard)
It can obviously be more sophisticated than this, where you might only want to have this guard for certain types of file.
Sometimes you see similar things done with defadvice, but this is often more fragile and can be difficult to understand when you come back to it later.

Related

emacs > org mode > agenda - always use current buffer

I am using many different org mode files for various projects, and I rev them by adding the date to the filename eg filename-2020-09-17.org. I realize I could use version control but in this case that is not possible, due to needing to share the file with others who are not using VC.
I would like the Agenda to always show just the items for the current file/buffer.
When I save eg the file with filename-2020-09-16.org to filename-2020-09-17.org, then the agenda still shows the old file name unless I remove it from the agenda file list and add the new file.
I realize that I can use C-c a < a but I am lazy and would rather not have to type S-, each time to get the <.
I looked at
Agenda view of the current buffer
And the OP says the solution was simple but he/she/they did not provide the solution - at least I don't see it and I tried the posted code but it no works.
I also found https://www.reddit.com/r/orgmode/comments/bxwovd/agenda_for_current_buffer/ but that did not seem to meet my need.
Specifically I would like to put something in .emacs so that this would apply to all files all the time.
I also looked into a keystroke macro programs but this does not seem ideal.
Any help is appreciated.
Thanks ahead of time.
Here's a simple function to do what you want, but there is no error checking to make sure e.g. that you are invoking it from a buffer that is visiting an Org mode file. The idea is that you set the org-agenda-files list to contain just the file which the buffer is visiting and then you call the regular org-agenda function. Binding the modified function to the C-c a key may or may not be what you want to do, but you can try and decide for yourself:
(defun org-agenda-current-buffer ()
(interactive)
(let ((org-agenda-files (list (buffer-file-name (current-buffer)))))
(org-agenda)))
(define-key global-map (kbd "C-c a") #'org-agenda-current-buffer)

Set emacs comment style based on file extension

There are multiple questions along these lines but I haven't been able to find what I need from start to finish. I have been using emacs for a few years but am not used to its customization.
I have a unique file type, identified by its extension, which emacs is not configured for. Its comment style is
<!-- text -->
and I would like to set the variables comment-start and comment-end to the relevant values (which I am assuming will allow me to use comment-region). I don't know the correct way to do this so that it will always be configured when I open this file type but won't affect the default behavior of emacs.
Do I need to make a new major mode for this file type, and then set the variables, or is there an easier way of doing it? An example of the complete requirements for my .emacs file would be much appreciated!
See here. I think this will work:
(add-to-list 'auto-mode-alist
'("\\.extension\\'" . (lambda ()
(setq-local comment-start "<!--")
(setq-local comment-end "-->"))))
Alternatively, if this file extension is well known (or if these files are close enough to a well known syntax), you may be able to just find a major-mode online that does what you want. For example, NXML Mode may just give you the comment syntax you want along with some other useful features.

How to load yasnippets on condition

I am doing some Drupal programming and have created yasnippets for common functions: db_select, db_delete etc.
I want to load them only when I am working on a Drupal file and not for a common php file. The task is this:
given a list of yasnippet files and the path of the currently opening file
if(path contains '/sites/all/modules')
{
load all the snippets from the list
}
How to do it?
You have two options:
Create a new mode called drupal-mode that inherits everything from php-mode and then define snippets for that mode, check out Derived Modes in the Emacs Manual on how to do that
Add a function to the find-file hook that checks to see if the current buffer is in a particular directory and then load the snippets you want
Either way you'll be doing things based on the current directory or filename.
find-file-hook example
I haven't tested this code but the concept is there. You'll probably have to check the yas/load-directory-1 function to something else or mess around with parameters.
(defun load-drupal-snippets ()
(when (string-match-p "/sites/all/modules" (file-name-directory (buffer-file-name)))
(yas--load-directory-1 "/path/to/drupal/snippets/" 'php-mode 'text-mode)))
(add-hook 'find-file-hook 'load-drupal-snippets)

Trying to edit init.el to customize emacs

So I'm relatively new in trying to customize emacs. But I really need to customize is asap. Tabs are a pain in emacs as they are two spaces, and the text is all messed up when it is opened with any other editor after that.
Currently, I only have few lines in my ~/emacs.d/init.el file:
(setq load-path (concat (getenv "HOME") "/.emacs.d/"))
(set-scroll-bar-mode 'right)
(require 'linum)
(global-linum-mode t)
I get an error while starting up emacs:
Loading encoded-kb...done
An error has occurred while loading `/Users/mycomp/.emacs.d/init.el':
Symbol's function definition is void: set-scroll-bar-mode
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
I tried srtating it with the --debug-init option, but my lisp knowledge is not enough to help me figure out what's wrong. Any help on how to get this working or redirecting me to some GOOD tutorials on editing init.el files will be really helpful (yes i did google tutorials on editing the initialization file, but every one of them was terrible).
I'm assuming my code for getting line numbers on the left is also wrong. Could someone please help me with this? Thanks a lot.
I think this line may be the problem:
(setq load-path (concat (getenv "HOME") "/.emacs.d/"))
First of all, I don't think this is required to load ~/emacs.d/init.el. Secondly, if you do want to add a directory to your load-path, you should probably be doing it like this instead:
(add-to-list 'load-path "~/.emacs.d/")
This code adds the directory to the load-path, your code just clobbers it with the single directory.
Use 'M-x apropos' and 'M-x customize-apropos'. For now, those will make your life much easier when you want to customize things.
For instance, to customize things to do with scrolling, 'M-x customize-apropos RET scroll RET' will give you a list of all things that you can customize that have 'scroll' in them. You can look around and find the things that you want by searching the buffer. If you find a particular thing that you want, there's usually a group that it belongs to. You can click on that, and just customize those particular values. Make sure you save the settings.
It might take you a while to figure out what things are called. If you've got an idea, try the apropos search. If that doesn't turn up anything, google can probably sort it out for you.
For now, don't worry about hacking the elisp. This method will write values to your startup file (probably the .emacs?) and you can look and check the syntax later if you're really interested. I customize most of my stuff this way; I only bother actually modifying the file by hand when I'm trying to write my own hooks or functions.

How do I make an emacs shortcut to switch between related files?

For example, I have the file model/user.py open and I want to have a shortcut that opens controller/user.py. Or I want to switch to test/model/testUser.py (contrived example)
I'd like to make an emacs shortcut which given a file currently open, opens files related in various ways.
If the "related files" follow some kind of pattern, I think it's trivial to write some elisp functions to do the task. Let's say you have a model and need to open his associated controller, you will need to do something like this:
(defun my-open-related-controller ()
(interactive)
(let* ((name (buffer-file-name))) ;gets the filename of the current buffer
;; Of course, this is only an example. The point here is that you need
;; to "discover" the name of the related file based on the current one.
(setf name (replace-regexp-in-string "model" "controller" name))
;; Now you will open the file(if it isn't open already) and switch to it
(find-file name)))
Then you can bind the function to, say, F5:
(define-key name-of-the-mode-map [f5] 'my-open-related-controller)
If you want to crate this binding globally, use:
(global-set-key [f5] 'my-open-related-controller)
Of course, this is just a crude example(since you didn't give many specific details), but should be enough to get you started. Hope it helps!
If you don't fancy writing this yourself and would rather customize an exisiting library, you may like to look at toggle.el. It's designed to do what you're asking for.
There is also the jump.el that rinari uses for this purpose (except for Ruby on Rails projects). I gave the second link, because rinari.el in this project contains settings that manage jumps from one place to another (controller to view, model, migrations, etc.).
It looks like you can get jump.el to jump to a particular method in a file - but that may take a bit of effort.